Michael Tichoň
Michael Tichoň

Reputation: 291

PHP Multi languages

I am working on multi-language project. The content such as articles, product description will be shown from database. This is no problem. The problem is there are some "static" descriptions, e.g. Form labels, error messages etc.

So far I ended with this. I store language in COOKIE and I have function to write the messages in set lanugeage. I have file named _lang.php with this content:

$['empty_field']['sk'] = "Slovak description of error";
$['empty_field']['en'] = "English description of error";

And so on. Is this good solution or not ? Any other solutions ? Thanks

So I've tried the gettext() and ended with this: (output is welcome)

putenv("LANGUAGE=sk_SK");
setlocale(LC_ALL, 'sk_SK');
$dom = "roids";
bindtextdomain($dom, "www/roids/_locale");
textdomain($dom);
echo gettext('welcome');

I created folder this folder: _locale/sk_SK/roids.po

Content of roids.po:

msgid ""
msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "welcome"
msgstr "vitaj"

I' using WAMP server and I do not know whether this path: www/roids/_locale is good. Wamp is installed here: D:\wamp and ww files are in D:\wamp\www

Upvotes: 0

Views: 193

Answers (3)

Cups
Cups

Reputation: 6896

Depending on your situation instead of maintaining an array, such as

$['empty_field']['sk'] = "Slovak description of error";
$['empty_field']['en'] = "English description of error";

you could perhaps more easily manage the strings, or farm them out to others, using an ini file and PHPs parse_ini_file:

[sk]
empty_field = "Slovak description of error";
; others in here ...

[en]
empty_field = "English description of error";
; others in here ...

With a nod to the comment by Wouter J that you make the lang the first array.

Upvotes: 2

bretterer
bretterer

Reputation: 5781

I think you should take a look at a few resources. One good one is right here on the stack exchange network. PHP Localization Best Practices? gettext?

Another good resource is google. Just google php localization best practice There are a lot of good libraries out there to use with php and localization

One other good resource for this is the gettext function from php itself. This is the way I would go to do something like this.

UPDATE

This is a very VERY good tutorial from O'Reilly group on the subject. Gettext - O'Reilly Media

Upvotes: 3

Ed Heal
Ed Heal

Reputation: 59987

I would use require_one to load in a PHP file for a particular language.

i.e.

require_once('/some/path/errors_'. $_SERVER['HTTP_ACCEPT_LANGUAGE'] . '.php');

and that file defines the associated array of error messages/etc for that language.

Upvotes: 1

Related Questions