Ivan Ivan - 1 month ago 8
PHP Question

How to implement translation on a existing web project?

I've rescued an old project that was originally writted only for spanish audience and now I have to internationalize it.

This project has 3 kinds of text strings:


  • Strings written from PHP: For example: echo "Hello world"

  • Short strings in database fields: database fields that contain text, for example, country names lists.

  • HTML embedded strings (static files): For example a static file containing an HTML file:
    <html>...<body>...<p>Hello world</p>
    ...

  • HTML embedded strings (database): Same as above, but HTML file is inserted on a text field in the database.



First is not a problem, because there are really few strings and very localized. The second case is also very localized so it's not really a big problem. But last 2 cases are unmanegeable: hundreds of HTML files (static + database) with hundreds of strings inside.

So, I'm looking for an automatized method to extract all strings into a recognizable format, give it to translators and then have my pages in other languages.

Does exist something similar or should I create a custom solution? In this case, any idea?

Answer

Ok. The I18n (Internationalization) is a quite strange thing. You have to make many many changes to your code in order to make it multilingual.

For me, the best solution is to move it on CakePHP that already support the I18n in many layers. But this solution will make you spend a lot of time. So ....

A fast method for translating internal texts like

echo "Hello World"

or

<p>Hello World</p>

Is to create an appropriate language file with php extension for each language (ie: es.php, el.php, en.php, an so on) that will contain an array with keys and values that will looks like that :

$l = array(
   'WEB_TITLE' => 'My web site title',
   'GEN_HELLO' => 'Hello World',
   'MENU_HOME' => 'Home',
   'MENU_PRODUCTS' => 'Products',
   ...
);

Then into your web site you have to load the appropriate language file at the very beginning of each page and into your page you have to do something like that:

echo $l['GEN_HELLO'];

and

<p><?php echo $l['GEN_HELLO']; ?></p>

That is the front end side of your application. Now to make your data multilingual you have to change the database structure in some how.

Lets say you have that table

ARTICLES
id    Title    Content   Date   Active

In the above structure you have to make Title and Content I18n compatible. In order to translate that columns you have to create another table called in example ARTICLES_L that will looks like that

ARTICLES_L
ID    POST_ID    COLUMN    LANGUAGE    CONTENT

In the above table you have to store the article id, the column that the translation belongs (ie: title) the language, and the translated content.

So if you have that records in ARTICLES

ARTICLES
id    Title    Content            Date                 Active
1     Title 1  This is content 1  2011-10-01 13:28:45  1
2     Title 2  This is content 2  2011-10-01 13:28:45  1
3     Title 3  This is content 3  2011-10-01 13:28:45  1

The table ARTICLES_L will contain that records

ARTICLES_L
ID    POST_ID    COLUMN    LANGUAGE    CONTENT
1     1          title     es_ES       Título 1
2     1          content   es_ES       Este es el contenido 1
3     1          title     el_GR       Τίτλος 1
4     1          content   el_GR       Αυτό είναι το περιεχόμενο 1
5     2          title     es_ES       Título 2
6     2          content   es_ES       Este es el contenido 1
....

In reality there are many many other tasks you have to do, but this is the idea behind the solution I am giving to you :)

Good Luck

PN : Even better for the front end of your application is to use that http://php.net/manual/en/book.gettext.php This way is the way that used from WordPress and CakePHP, but in very low level :? Just take a look. This will be better than creating files for each language.