Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    ats
    ats is offline
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Language - Cookie

    I have inherited a website that is both English and French. On the header of every page is some text that says "View this page in: <language>". The default right now is French. I want the default to always be English.

    Here is the script to set the cookie:

    $(document).ready(function(){
    var currentlanguage = Get_Cookie('Language');
    if(currentlanguage==''){
    Set_Cookie('Language','English','', '/', '', '' );
    currentlanguage=Get_Cookie('Language');
    }

    else if(currentlanguage=='English'){
    $('.frenchContent').hide();
    $('.englishContent').show();
    $('#changeLanguage').text('Français');
    $('#selectTopic').html('');
    $('#selectTopic').html($('.genQuestion').html());
    $('#selectCategory').html('');
    $('#selectCategory').html($('.byCategory').html());
    }
    else{
    $('.englishContent').hide();
    $('.frenchContent').show();
    $('#changeLanguage').text('English');
    $('#selectTopic').html('');
    $('#selectTopic').html($('.genQuestionfr').html());
    $('#selectCategory').html('');
    //alert($('#selectCategory').html());
    //alert($('.byCategoryfr').html())
    $('#selectCategory').html($('.byCategoryfr').html());
    }

    });


    THEN....there is this HTML on the header of every page:

    <div style="float:right;">See this page in: <a href="#" id="changeLanguage">Fran&ccedil;ais</a></div>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,081
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Get_Cookie() and Set_Cookie() are functions of your program, what you posted is useless without them. You probably have a default somewhere that uses Set_Cookie() to set the Language to french. Find that and set it to English.

  • #3
    ats
    ats is offline
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    This is the first part that I posted. It appears to me that it looks to see if the cookie is empty. If so, it's setting the language to English. Correct?

    $(document).ready(function(){
    var currentlanguage = Get_Cookie('Language');
    if(currentlanguage==''){
    Set_Cookie('Language','English','', '/', '', '' );
    currentlanguage=Get_Cookie('Language');
    }

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,081
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Yes, that's the way it looks to me.

  • #5
    ats
    ats is offline
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am checking the cookie on the pageload and I am not setting the Language variable successfully.

    Can anyone spot if this is incorrect?

    $(document).ready(function(){
    var currentlanguage = Get_Cookie('Language');
    if(currentlanguage==''){
    Set_Cookie('Language','English','', '/', '', '' );
    currentlanguage=Get_Cookie('Language');
    }

    I need the script to do the following:

    Check if there is a Language variable in the cookie.
    If not, then set it to English

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    The logic is fine, but we don't know what Get_Cookie and Set_Cookie do since you didn't post their implementation.

    My guess would be that Get_Cookie, if the cookie isn't set, doesn't return the empty string but something else.

  • #7
    ats
    ats is offline
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Airblader View Post
    The logic is fine, but we don't know what Get_Cookie and Set_Cookie do since you didn't post their implementation.

    My guess would be that Get_Cookie, if the cookie isn't set, doesn't return the empty string but something else.
    function Set_Cookie( name, value, expires, path, domain, secure )
    {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    else {
    expires = new Date(today.getTime() + 1000 * 60 * 60 * 24 * 7);
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
    }

    function Get_Cookie( name ) {

    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
    return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
    }
    $(document).ready(function(){
    var currentlanguage = Get_Cookie('Language');
    if(currentlanguage==''){
    Set_Cookie('Language','English','', '/', '', '' );
    currentlanguage=Get_Cookie('Language');
    }

    else if(currentlanguage=='English'){
    $('.frenchContent').hide();
    $('.englishContent').show();
    $('#changeLanguage').text('Français');
    $('#selectTopic').html('');
    $('#selectTopic').html($('.genQuestion').html());
    $('#selectCategory').html('');
    $('#selectCategory').html($('.byCategory').html());
    }
    else{
    $('.englishContent').hide();
    $('.frenchContent').show();
    $('#changeLanguage').text('English');
    $('#selectTopic').html('');
    $('#selectTopic').html($('.genQuestionfr').html());
    $('#selectCategory').html('');
    //alert($('#selectCategory').html());
    //alert($('.byCategoryfr').html())
    $('#selectCategory').html($('.byCategoryfr').html());
    }

    });

  • #8
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    See, your Get_Cookie returns null rather than an empty string. So try using

    Code:
    var currentlanguage = Get_Cookie('Language');
    if(currentlanguage==null){
        Set_Cookie('Language','English','', '/', '', '' );
        currentlanguage=Get_Cookie('Language');
    }
    Please use [ code ]-Tags to post your code in the future, otherwise it gets really hard to read it.

  • Users who have thanked Airblader for this post:

    ats (05-13-2013)

  • #9
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    I just noticed that your logic also has a problem, so use this:

    Code:
    $(document).ready(function () {
        var currentlanguage = Get_Cookie('Language');
        if (currentlanguage == null) {
            Set_Cookie('Language', 'English', '', '/', '', '');
            currentlanguage = Get_Cookie('Language');
        }
    
        if (currentlanguage == 'English') {
            $('.frenchContent').hide();
            $('.englishContent').show();
            $('#changeLanguage').text('Français');
            $('#selectTopic').html('');
            $('#selectTopic').html($('.genQuestion').html());
            $('#selectCategory').html('');
            $('#selectCategory').html($('.byCategory').html());
        } else {
            $('.englishContent').hide();
            $('.frenchContent').show();
            $('#changeLanguage').text('English');
            $('#selectTopic').html('');
            $('#selectTopic').html($('.genQuestionfr').html());
            $('#selectCategory').html('');
            //alert($('#selectCategory').html());
            //alert($('.byCategoryfr').html())
            $('#selectCategory').html($('.byCategoryfr').html());
        }
    
    });

  • Users who have thanked Airblader for this post:

    ats (05-13-2013)

  • #10
    ats
    ats is offline
    New to the CF scene
    Join Date
    May 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks! What was the problem?

  • #11
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    The first one was that your Get_Cookie returns null if the cookie (value) wasn't set, but your logic compared against the empty string -- so this case was never caught.

    The second mistake was that the fallback logic was connected to the logic loading the corresponding logic. If no cookie was set, it would go into the first if-case, where it would correctly set "English" as a default now. However, the block loading English content is the else-if-block, which would now not be executed as "else if" will never be executed after the if has already been executed.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •