0

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

Michael Tichoň
  • 281
  • 2
  • 10

3 Answers3

3

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

Community
  • 1
  • 1
bretterer
  • 5,693
  • 5
  • 32
  • 53
  • I have looked to taht gettext function but I do not certainly udnerstand how does it work - how to make those .mo files etc Could you tell me more? – Michael Tichoň Mar 03 '13 at 08:10
  • We prefer answers that stand on their own rather than just containing links to other content. Also if you're just linking to another SO post then the question is probably a duplicate and maybe should be flagged as such. Thanks. – Kev Mar 03 '13 at 13:30
2

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.

Cups
  • 6,901
  • 3
  • 26
  • 30
1

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.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Be careful with this option as you need to do some parsing of the `$_SERVER['HTTP_ACCEPT_LANGUAGE']` to get what you expect. When I print mine out, it gives me `en-US,en;q=0.8`; – bretterer Mar 03 '13 at 08:06
  • @bretterer - Good point. I am sure the poster can refine the solution. – Ed Heal Mar 03 '13 at 08:08