www.webdeveloper.com
Results 1 to 7 of 7

Thread: if language = en the, if language = f

  1. #1
    Join Date
    Nov 2013
    Location
    Canada
    Posts
    12

    if language = en the, if language = f

    I'm trying to set my php for a bilingual Drupal website.

    Here is my code:

    <?php
    global $user;
    if ( $user->uid ) {
    print '<a href="'.base_path().'user/logout">Log out</a>';
    print '<a href="'.base_path().'user/">My Account</a>';
    }
    else {
    print '<a href="'.base_path().'user/login">Log in</a>';
    print '<a href="'.base_path().'user/register">Create Account</a>';
    }
    ?>

    How can I set it up such as if language = en, thenů., if language = fr, thenů. ?

    Your help would be really appreciated. I tried many things without success.

    Dany !!!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    One way is to define an array of sentences/phrases, then reference it by language as needed. Simple case:
    PHP Code:
    <?php
    $i18n 
    = array(
        
    'greeting' => array(
            
    'en' => 'Hello!',
            
    'fr' => 'Bonjour!'
        
    ),
        
    'name' => array(
            
    'en' => 'My name is NogDog.',
            
    'fr' => 'Je m\'appelle NogDog.'
        
    )
    );

    // later on...
    $language 'fr'

    echo 
    "<h1>{$i18n['greeting'][$language]}</h1>\n";
    echo 
    "<p>{$i18n['name'][$language]}</p>\n";
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2013
    Location
    Canada
    Posts
    12
    Finally came up with this but it's still not working.

    Anyone would know why (It's a Drupal website).

    <?php
    global $user;

    $strings = array(
    'en' => array(
    'logout' => 'Logout',
    'my-account' => 'My Account',
    'login' => 'Log In',
    'create-account' => 'Create Account'
    ),

    'fr' => array(
    'logout' => 'Deconnexion',
    'my-account' => 'Mon Compte',
    'login' => 'Connexion',
    'create-account' => 'Creer un Compte'
    )
    );

    $language = $user->lang;

    if ( $user->uid ) {
    print '<a href="'.base_path().'user/logout">'.$strings[$language]['logout'].'</a>';
    print '<a href="'.base_path().'user/">'.$strings[$language]['my-account'].'</a>';
    }else {
    print '<a href="'.base_path().'user/login">'.$strings[$language]['login'].'</a>';
    print '<a href="'.base_path().'user/register">'.$strings[$language]['create-account'].'</a>';
    }

    ?>

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Can you be more specific as to what you mean when you say it's not working?

    I don't see anything obviously wrong, but I don't know the entire context, such as why you need to specify $user as global (is that a constraint with using Drupal?). You could turn on all error reporting for now while debugging, to see if that helps:
    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'true); // set this to false in live version

    // ... rest of script ...
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Nov 2013
    Location
    Canada
    Posts
    12
    Hi NogDob. First and foremost, I'd like to say thank you for your help.

    You see, I'm learning php so I don't fully understand why I need (or not need) to put global $user. Right now I changed the code to that below and I can now see Log in Create Account. But switching to the French site doesn't translate the text (it remains Log in Create Account) .

    Any idea why ? (I tried turning on the error reporting with no success unfortunately).

    <?php

    $strings = array(
    'en' => array(
    'logout' => 'Logout',
    'my-account' => 'My Account',
    'login' => 'Log In',
    'create-account' => 'Create Account'
    ),

    'fr' => array(
    'logout' => 'Deconnexion',
    'my-account' => 'Mon Compte',
    'login' => 'Connexion',
    'create-account' => 'Creer un Compte'
    )
    );

    $language = $user->language;

    if ( $user->uid ) {
    print '<a href="'.base_path().'user/logout">'.$strings[$language]['logout'].'</a>';
    print '<a href="'.base_path().'user/">'.$strings[$language]['my-account'].'</a>';
    }else {
    print '<a href="'.base_path().'user/login">'.$strings[$language]['login'].'</a>';
    print '<a href="'.base_path().'user/register">'.$strings[$language]['create-account'].'</a>';
    }

    ?>


    Thanks for your help again !!!

  6. #6
    Join Date
    Nov 2013
    Location
    Canada
    Posts
    12
    I actually just realized that it says Log out / My account (because I was logged in) Once I log out the text disappeared.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Don't forget that you may need to refresh your browser or even clear its cache when switching between languages. If that's not the issue, then you need to inspect what the actual value of $language is to find out if maybe the problem is actually in how that is determined/set.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles