Multi lingual sites. (internationalization)
Posted: Sun Dec 18, 2011 2:18 am
				
				I am posting this tut in response to this post http://indie-resource.com/forums/viewto ... =18&t=4210 , but keeping it in the proper place :p
I would perform this task 1 of 2 ways.
Arrays or XML
Arrays is a much easier approach so i will explain that one
Set up a 'language' php file. You can choose to have either one language file per game page or a master language page.
obviously with a single document containing all language translations, it becomes very big and you load up much uneeded stuff. however, with seperate pages, you would either need to duplicate content to cover menu items etc, or have a seperate page soly for menus etc.
Lets look at the seperate page option with a seperate menu file
menu.lang.php
home.lang.php
So we have 2 language files. Menu and home page. Both containing English translations. We use the array_merge function to combine the array set by the menu page and add additional array items into that array easily.
Now we want to add a second language, all we need to do is to open up the text into arrays to contain the various translations for each option.
So now they become...
menu.lang.php
home.lang.php
Now, each text chunk has been split into an array containing translations. So to access the translations, you would do $lang['welcome']['en']. That would output the english translation of your welcome string.
Now when it comes to your page, you would construct it like so.
So let me run over a couple of final things.
sprintf, this replaces certain parts of the text (the %s part) with variable data. Read from the link to get a better understanding lol :p
we access the lang variable and target a translation chunk, in the first case the welcome message. in the next dimension of the array, we access the translation of that element, this is best set in the user database for easy access. So by calling in the users language settings, you select the translation to display.
Then the same goes for the menu items.
The first dimension of the array is the chunk of text to display, the second is the translation to display.
This method is easy to maintain and execute.
Hope it makes sense lol, i dont think it does, but im tired, so not processing things properly lol
			I would perform this task 1 of 2 ways.
Arrays or XML
Arrays is a much easier approach so i will explain that one

Set up a 'language' php file. You can choose to have either one language file per game page or a master language page.
obviously with a single document containing all language translations, it becomes very big and you load up much uneeded stuff. however, with seperate pages, you would either need to duplicate content to cover menu items etc, or have a seperate page soly for menus etc.
Lets look at the seperate page option with a seperate menu file
menu.lang.php
Code: Select all
$lang = array(
   "home" => "Home",
   "inventory" => "Inventory",
   ......
);
 Code: Select all
$lang = array_merge($lang, array(
   "welcome" => "Welcome to xGame %s",
   "instruct" => "In order to progress through the game, you will need to do many tasks!",
   ......
);
 Now we want to add a second language, all we need to do is to open up the text into arrays to contain the various translations for each option.
So now they become...
menu.lang.php
Code: Select all
$lang = array(
   "home" => array(
                       "en" => "Home",
                       "fr"  => "Accueil",
                       "de" => "naar huis"),
   "inventory" => array(
                       "en" => "Inventory",
                       "fr"  => "Inventaire",
                       "de" => "inventaris"),
   ......
);
 Code: Select all
$lang = array_merge($lang, array(
   "welcome" => array(
                       "en" => "Welcome to xGame %s",
                       "fr"  => "Bienvenue xGame %s",
                       "de" => "Welkom op xGame %s"),
   "instruct" => array(
                       "en" => "In order to progress through the game, you will need to do many tasks!",
                       "fr"  => "Afin de progresser dans le jeu, vous devrez effectuer de nombreuses tâches!",
                       "de" => "Om de voortgang door het spel, moet u veel taken te doen!"),
   ......
);
 Now when it comes to your page, you would construct it like so.
Code: Select all
<?php
require "language/menu.lang.php";
require "language/home.lang.php";
?>
<div class="welcome text"><?php echo sprintf($lang['welcome'][$userinfo['language']], $userinfo['name']); ?></div>
<ul>
    <li><?php echo $lang['home'][$userinfo['language']]; ?></li>
    <li><?php echo $lang['inventory'][$userinfo['language']]; ?></li>
</ul>
sprintf, this replaces certain parts of the text (the %s part) with variable data. Read from the link to get a better understanding lol :p
we access the lang variable and target a translation chunk, in the first case the welcome message. in the next dimension of the array, we access the translation of that element, this is best set in the user database for easy access. So by calling in the users language settings, you select the translation to display.
Then the same goes for the menu items.
The first dimension of the array is the chunk of text to display, the second is the translation to display.
This method is easy to maintain and execute.
Hope it makes sense lol, i dont think it does, but im tired, so not processing things properly lol