3

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.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
Raheel
  • 177
  • 1
  • 2
  • 14
  • They are another more easy solution for translating PHP websites. See http://www.phpcs.com/codes/SITE-MULTILINGUE-AVEC-DETECTION-LANGUE-EXPLORATEUR_30657.aspx This is the best solution – janhsh Jun 16 '13 at 12:33

2 Answers2

15

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/

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Jens
  • 1,302
  • 1
  • 13
  • 20
  • Sorry to say that still i didn't solved my problem. :( here is my code of which is in test.php
    
    
    
    
    and here is at same root my locale folder which has de_DE/LC_MESSAGES/ folders and it contains messages.po and messages.mo files which is created automaticaly by the poedit. when i hit root/test.php it shows me same text 'This is my english text.'.
    – Raheel Jan 11 '12 at 09:36
  • I'm using xampp and microsoft windows xp. – Raheel Jan 11 '12 at 09:39
  • what u mean with look of .po file?? when i double click on that file it opened in poedit and show my string which is echo in gettext function.nothing more... – Raheel Jan 11 '12 at 13:27
  • please give any idea. i did not understand what happning with it.. After lot of googling did not find any solution. I uninstalled xampp 5.2.9 and installed now 5.3.8 but still not solved problem. – Raheel Jan 12 '12 at 21:15
  • You can code a .po file too - Can you show us how your .po file looks? – Jens Jan 13 '12 at 07:05
  • how can i show you .po file? it has just string which is in gettext function for translation. i mean just 'This is my english text.' Nothing else.. – Raheel Jan 13 '12 at 10:17
  • Open the .po file your code editor (or notepad) and post it here. – Jens Jan 13 '12 at 13:01
  • msgid "" msgstr "" "Project-Id-Version: rab\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2012-01-13 21:03+0500\n" "PO-Revision-Date: 2012-01-13 21:03+0500\n" "Last-Translator: Muhammad Raheel\n" "Language-Team: rab\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "X-Poedit-KeywordsList: _;gettext;gettext_noop\n" "X-Poedit-Basepath: c:/xampp/htdocs/multi-lang/\n" "X-Poedit-Language: German\n" "X-Poedit-Country: GERMANY\n" "X-Poedit-SourceCharset:iso-8859-1" "X-Poedit-SearchPath-0:." #: test.php:19 msgid "Hello world." msgstr "" – Raheel Jan 13 '12 at 16:06
  • This is my .po file looks. i give "Hello world." to gettext() and it prints as it is. – Raheel Jan 13 '12 at 16:07
  • Hey Mark can u explain me steps of how to translate texts well and what tools and softwares that i need to translate strings?? – Raheel Jan 13 '12 at 17:45
  • As I thought - the string "This is my english text." is not in your .po file :) Add it, or translate the "Hello world." string into german on the following line "msgstr". And instead use gettext("Hello world."); in your PHP script. That should do the trick. Take note that gettext is (as I remember) case-sensitive, and there's also a difference between "Hello world" and "Hello world." (the dot). – Jens Jan 14 '12 at 11:28
  • Thanks for your comments Mark. How to add or translate Hello world? One thing i need to be clear that i will add translation of any string that i passed in gettext function myself or gettext will translate it automatically? what is the process behind this? can u explain please? – Raheel Jan 16 '12 at 08:30
  • gettext does not translate automatically, you need to provide translations for it. What gettext does it basically just looking in the .mo (binary .po) for the msgid, and if it finds something it inserts the respective msgstr where the gettext is. Gettext is kind of like an extended arm of the echo function that makes it able to translate stuff. But for gettext("Hello world") to work, "Hello world" must exist in the .po as msgid "Hello world"msgstr "Gutentag" (or how you do it in German hehe) – Jens Jan 16 '12 at 13:49
  • Thanks again for your comment and editing answer. I understand most of points related gettext but just one point that i need to be cleared that how translation of strings should be done? I mean we need to translate strings by our self or gettext translate strings automatically? I'm using gettext() and running my test.php using poedit it creates .mo and .po files but not translating the strings then i by myself creating translation against every string. Is am i doing right or wrong? please clear this point ASAP. Thanks!! – Raheel Jan 16 '12 at 15:44
  • i'm translating strings using translate.google.com. is it right or not?? – Raheel Jan 16 '12 at 16:06
  • You have to translate yourself. You would typically handle English and your native language on your own; I always write my code with English texts, then I make the Danish translation myself, and if I need more translations I will need people to do it for me. I instruct them in how to use poedit and then they send me their finished .po and .mo's and I put them into their respetive locale directories. – Jens Jan 17 '12 at 13:15
  • You can use Google Translate, but it's not as accurate as a person. Also to my experience Bing translator is more accurate (and I say that as a person who prefers Google way over Microsoft, and Apple for the matter). But a person who is skilled in the written version of a language is always better. – Jens Jan 17 '12 at 13:16
  • just one thing would u like to tell me that how to translate a variable in gettext? – Raheel Jan 17 '12 at 13:38
  • and what is the meaning of Requirements: To use these functions you must download and install the GNU gettext package from » http://www.gnu.org/software/gettext/gettext.html here in http://us2.php.net/manual/en/gettext.requirements.php – Raheel Jan 17 '12 at 13:50
  • You can check if you have installed gettext by using if (function_exists("gettext")){ echo "I'm good to go" }else{ echo "Extra stuff must be installed"; } – Jens Jan 17 '12 at 14:42
  • I'll add translation of dynamic content to the answer – Jens Jan 17 '12 at 14:42
  • You're welcome - As a "thank you" you can vote up my answer haha :D – Jens Jan 18 '12 at 09:09
  • HI Mark Hünermund Jensen, can u explain me how to translate dynamic content please?? You told in your comment but did not explain how to add translation of dynamic content. – Raheel Apr 13 '13 at 06:48
0

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

Community
  • 1
  • 1
simshaun
  • 21,263
  • 1
  • 57
  • 73
  • This question is not a duplicate, because it asks also how to generate a whole translation, and the link you give tells only about all translation strings (but not a final document). – Yaroslav Nikitenko Apr 16 '20 at 08:55