Question

I have installed poedit and running my file through it, it creates .po and .mo files for them. But I have a problem to load and use these files for translating my text. I don't know how to load or open the translated files and to show the translated content. Can anyone help me about this. I tried every possible source but not succeeded.

Was it helpful?

Solution

First of all you need to inform PHP which locale and domain you are using.

putenv("LANG=da_DK"); 
setlocale('LC_ALL', "da_DK"); 
bindtextdomain("mycatalog", "./locale/");  
textdomain("mycatalog");

In this case I'm having a Danish translation and a file called mycatalog.mo (and .po). These files are placed (from your root) here: locale/da_DK/LC_MESSAGES/mycatalog.mo/po

In order to show your translation, you will do this:

echo _("Hello world");   // Which would become "Hej verden"

_(); is an alias of gettext(); The smart thing about gettexts is that if there's no translation you will not have an ugly language code like "MSG_HELLO_WORLD" in your UI, but instead a better alternative: Simply the plain English text.

In the messages.po file you must have all the messages (case-sensitive and also with respect to used commas, dots, colons, etc.) on this form:

msgid "Hello world!"
msgstr "Hej verden!"

When you have added this to your .po file, you open this file in poedit, hit "Save" and it will generate a .mo file. This file is uploaded to the same directory as the .po file (typically something like \locale\da_DK\LC_MESSAGES\ from the script root)

To translate dynamic/variable content you can use - among other things - sprintf, in this manner:

echo sprintf(_("My name is %s"), $name);

In this case the %s will occur in the .po file; When you have the translated string (which contains the %s), sprintf will make sure to replace the %s with the variable content. IF the variable must be translated too, you can do this:

echo sprintf(_("The color of my house is %s"), _($color));

Then you don't need a full sentence for every color, but you still get the colors translated.

It is important to note that the first time a .mo is run on the server it is cached - and there is no way of removing this file from the cache without restarting (Apache or the like itself should be enough). This means that any changes you make to the .mo after the first time it is used, will not be effective. There are a number of hacks to work around this, but honestly, they are mostly not very pretty (they include copying the .mo, add the time() behind it and then import and cache it again). This last paragraph is only of importance if you aren't going to translate the whole thing at once, but in chunks.

If you want to create your own translation tool at some point, this tool helps you convert .po to .mo using PHP:

http://www.josscrowcroft.com/2011/code/php-mo-convert-gettext-po-file-to-binary-mo-file-php/

OTHER TIPS

See (and explore) http://php.net/manual/en/book.gettext.php. There are user-comments on that page that should give you an idea on how to procede.

Also, your question is a duplicate of Get translations from .po or .mo file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top