views:

71

answers:

3

We're taking our site international, .fr, .mx and soon some others.

I was thinking of creating language files like english.inc.php that would have basically a big array of every static text that we use.

For example $lang['welcome'] = 'Welcome';

I would dynamically include the correct language file based on the domain name used, and then use those arrays in the code everywhere we used to have static text.

Is that the right way to go?

Before I go too deep into it, I would appreciate any tips from people who had to this before.

Thanks!

Nathan

A: 

Use gettext().

BalusC
+4  A: 

Please research gettext for php http://php.net/manual/en/book.gettext.php, it is designed for exactly what you are doing and can handle all the various complexities of internationalization. (plurals, context, etc).

The way gettext works is each locale has a plain text file with translations and in your PHP code you do this:

<?php echo _("Welcome to my site");?>

Gettext then pulls from the appropriate locale file.

Internationalization is very complex and it's best to use a tried-and-true solution. We use gettext at Mozilla for most of our big websites and it is fast, well-known and full-featured.

Ryan Doherty
Thanks for the tip.One detail though. Would you actually create a locale file for English? Or would you use the automatic feature of php to print the string in echo _("Welcome to my site")? Because, what happens if you want to change a word or sentence in the English version?
nute
The way we deal with it is to first put the strings in our php files like I previously demonstrated, then pull the strings out using xgettext into another po file. Then you can merge that one into the en-us (english) file.
Ryan Doherty
A: 

I've always used Gettext, it's really fast and you can use external tools such as Poedit.

Keiji