Vincent Reniers | From PHP Guru to Designer!

TAG | Languages

Sep/09

23

Internationalization in PHP

Why have such a function?

While working at my CMS I was thinking on what would be the best way to manage the various languages and still make it easy to write the apps. After some thinking and taking the advice of a collegue into consideration I came to the conclusion that it would be best to let all strings go through a function. The internationalization function __(”); why this function? Because it’s pretty much becoming a standard in other CMS. And I don’t want to make it hard for other devs if they need to understand my script.

Now what exactly will this function do?

First of all we want the string that we enter via this function to be translated into other languages. Now so we first need to know some information and ask yourself a few questions before programming. Like what languages has the user set? Are these languages active? Where do we find the translation? What if the translation is incorrect, how can the user change them?

So we could just make the translations hardcoded, but that wouldn’t be quite usable for a normal administrator to change these translations. Thats why we will also put them into the a DB Table. And of course this table will just be a reference for the backend since we do not want to go fetch a translation from the database all the time. Thats why you should store it in a file, this way it will be a lot faster. Ok yes you can cache it, but still if the cache is turned off retrieving them from files is a lot faster than running multiple queries. Now how to save them in files could be done in various ways. They could be stored in an .ini or .txt file and a line can be added each time there is a new translation. But storing them in a .php file would be a lot faster since the .ini, .txt files would have to be parsed each time. While when you would put the translations in an array the data is instantly available for use.

Now if there is a better way to store them in a PHP file then please let me know. But we’re just going to with this method. What the array could look like is this:

array(
'english' => array(
'hello' => 'hello',
'how are you?' => 'how are you?',
),
'french' => array(
'hello' => 'bonjour',
'how are you?' => 'comment ça va?',
),
);

Now when adding a new translation to the PHP file via fwrite it would be hard to put it on X amount of locations ( X = languages ) and than write it back again. Now that would be possible but it’s probably going to coss more load. Now a better (but temporary) way to do it I think could be by simply adding $lang[$language][$text] = $ translation; at the bottom. And then you could create a cronjob that will sort out these language files properly at the end of every day or make a script in the admin area for an administrator to do it. Now adding them directly to the array by finding the correct location could also work well.

Now how would you let the function create the translations for the other languages than the one you wrote it in? One way would be just duplicate the text and wait for an admin to change the values with the correct translations in the DB Table and then let it go to the file again. But a smart way would be to let it go through a Translator API and the generated translation would be ok 90% of the time.

What if the function already found a translation?

In this case you can just select the array of the language you’re currently working and than see if the string has been translated and go fetch it. Otherwise the function would have to do the steps described above.
Anyways, these were my thoughts on how to create a good internationalization function. Now there could definitely be some easier ways to create it, and if you found one please let me know! I hope you learnt a bit on how to create it and I’m going to try and put this idea into code very soon =).

, , , , Hide

Find it!

Theme Design by devolux.org