Multi lingual sites. (internationalization)

Post all your tuts or request for tuts here.
Post Reply
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Multi lingual sites. (internationalization)

Post by Torniquet »

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

Code: Select all

$lang = array(
   "home" => "Home",
   "inventory" => "Inventory",
   ......
);
 
home.lang.php

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!",
   ......
);
 
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

Code: Select all

$lang = array(
   "home" => array(
                       "en" => "Home",
                       "fr"  => "Accueil",
                       "de" => "naar huis"),
   "inventory" => array(
                       "en" => "Inventory",
                       "fr"  => "Inventaire",
                       "de" => "inventaris"),
   ......
);
 
home.lang.php

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, 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.

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>
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
New Site Coming Soon! Stay tuned :D
Sebastian
Posts: 31
Joined: Sun Dec 11, 2011 8:21 pm

Re: Multi lingual sites. (internationalization)

Post by Sebastian »

nice1!
and you can even use it for buttons and other graphics
i have to try it
thank you!

regards
Sebastian
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Multi lingual sites. (internationalization)

Post by Ark »

Very nice tutorial thanks alot Torniquet!

I'm still confused with the spintf() function, would you please give me an idea how the last script will output please?
Orgullo Catracho
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: Multi lingual sites. (internationalization)

Post by Torniquet »

with the scriptf function, it will replace certain characters within your string.

So for example you would do something liek the following...

Code: Select all

echo 'Hello ' . $username;
 
Which would output 'Hello Ark'

in predefined strings, instead of breaking them up into peices to make a sentance, you would do the following.

Code: Select all

$string = "Hello %s";
$username = "Ark";

echo sprintf($string, $username);
 
which would output the same thing. The %s character is a predefined identifier in which replacements are made.

You can also do multiple replacements

Code: Select all

$string "Hello %s how was your %s";
$username = "ark";
$timeOfDay = "evening";

echo sprintf($string, $username, $tomeOfDay);
 
Output: "hello Ark how was your evening"

different character sets represent different value types. %s = string, %f = float etc.

Does that clear it up for you abit?
New Site Coming Soon! Stay tuned :D
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Multi lingual sites. (internationalization)

Post by Ark »

Yea, Thanks Torniquet! :)
Orgullo Catracho
Post Reply

Return to “Tutorials”