How to develop multi lingual websites in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • syamswaroop
    New Member
    • Dec 2009
    • 5

    How to develop multi lingual websites in PHP

    HI all,
    I want to develp a multi lingual website in PHP.(english and french)
    I am a novice developer in this aspect.
    Pls suggest us what are the methods used in general.
    Do we need any extra s/w.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    basicly you do it like a monolingual website, where you can decide, whether to put the english or french content in (e.g. by using templates)

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hey.

      This can be as simple as:
      1. Define an array of values in different languages, preferably each language having it's own array defined in a separate file.
      2. Create a script that figures out which language to use (by, for example, reading session, cookie or get/post values) and includes the proper file.
      3. Include said script into your pages, using the array defined in the language files to echo the text in those pages.


      Consider, if you had these files:
      [code=text]locale/
      english.php
      french.php
      set_locale.php
      index.php[/code]

      You could define each of the "locale" files as such:
      [code=php]<?php
      /*
      * File: locale/english.php
      */
      $locale = array(
      'title' => 'Title in English',
      'h1' => 'The following in in English:',
      'p1' => 'This is a sample text, in English'
      );
      ?>[/code][code=php]
      <?php
      /*
      * File: locale/french.php
      */
      $locale = array(
      'title' => 'Titre en français',
      'h1' => 'Le texte suivant en en français:',
      'p1' => 'Il s\'agit d\'un échantillon de texte, en français.'
      );
      ?>[/code]

      Then, in your "set_locale.php " file, you could select a language based on some input variable, like say; a variable named "lang" from the GET array, and include one of those files based on that value:
      [code=php]<?php
      /*
      * File: set_locale.php
      */

      // Get the language from the query string, or set a default.
      ($language = @$_GET['lang']) or $language = 'english';

      // Set up a list of possible values, and make sure the
      // selected language is valid.
      $allowed_locale s = array('english' , 'french');
      if(!in_array($l anguage, $allowed_locale s)) {
      $language = 'english'; // Set default if it is invalid.
      }

      // Inlclude the selected language
      include "locale/{$language}.php ";

      // Make it global, so it is accessible everywhere in the code.
      $GLOBALS['L'] = $locale;
      ?>[/code]

      Then, in your pages (the index.php page, in this example), include the "set_locale.php " script and use the $GLOBALS['L'] array to set language specific phrases in the HTML:
      [code=php]<?php
      /*
      * File: index.php
      */

      // Include the "set_locale " script to fetch the locale
      // that should be used.
      include_once "set_locale.php "

      // Print the HTML, using the selected locale.
      ?>
      <html>
      <head>
      <title><?php echo $GLOBALS['L']['title']; ?></title>
      </head>
      <body>
      <h1><?php echo $GLOBALS['L']['h1']; ?></h1>
      <p><?php echo $GLOBALS['L']['p1']; ?></p>
      </body>
      </html>[/code]
      See what I mean?

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        Or you could just tell your users to learn how to use Google Translate. ;)

        Comment

        • syamswaroop
          New Member
          • Dec 2009
          • 5

          #5
          thanks for your reply

          Comment

          • syamswaroop
            New Member
            • Dec 2009
            • 5

            #6
            thanks for your valuable code

            Comment

            • Bellebelle
              New Member
              • Aug 2010
              • 1

              #7
              Given the code, how would you go about coding an <a></a> tag with alternating classes?

              For example when the home page is selected, the home link looks:
              <a href="index.php ?lang=english" class="selected ">Home</a>

              but if I'm on the contact us page for example, I want the home link to be (without the class="selected "):
              <a href="index.php ?lang=english"> Home</a>

              Comment

              Working...