Hướng dẫn php-gettext install

  • Introduction
  • Installing/Configuring
    • Requirements
    • Installation
    • Runtime Configuration
    • Resource Types
  • Predefined Constants
  • Gettext Functions
    • bind_textdomain_codeset — Specify or get the character encoding in which the messages from the DOMAIN message catalog will be returned
    • bindtextdomain — Sets or gets the path for a domain
    • dcgettext — Overrides the domain for a single lookup
    • dcngettext — Plural version of dcgettext
    • dgettext — Override the current domain
    • dngettext — Plural version of dgettext
    • gettext — Lookup a message in the current domain
    • ngettext — Plural version of gettext
    • textdomain — Sets the default domain

marioandrea dot petruccelli at gmail dot com

4 years ago

How to use gettext on Windows.

If you use Linux start from the step 2 and consider cmd as linux shell.

1] First you have to download and install this
      //mlocati.github.io/articles/gettext-iconv-windows.html

       Check all options during the installation and go on.

2] Create a index.php file into your website directory with this code inside:

    

3] Open cmd and move into your website folder using cd

4] Now create the .mo files directly from the php files using this command
    xgettext -n index.php

       Use xgettext -help to see how to include more php files.

5] Once finished will be generate a file called messages.mo. Now you have to set the language and the charset. Open messages.mo with notepad and edit the lines:

- "Language: \n" BECOMES "Language fr\n"
- "Content-Type: text/plain; charset=CHARSET\n" BECOMES "Content-Type: text/plain; charset=UTF-8\n"

6] Remember to translate every line where is present msgstr "", translating the instance msgid into language you have chosen. For instance:

   #: index.php:2
  msgid "Good Morning"
  msgstr "Bonjour"

7] Once finished open cmd and move into your website folder using cd and then type
     msgfmt messages.po

This will make a file called messages.mo . Is a binary version of the messages.po file

8] Now you have to create a folder structure like this into your website folder. Do this for each language you want to add.

      website/locale/fr_FR/LC_MESSAGES

9] Move messages.mo and messages.po in locale/fr_FR/LC_MESSAGES

                10] Now edit the index.php as follows

                                       

11] Open index.php in your browser and if you will see "Bonjour" it means everything is okay. If not,  start from the step 2 again.

If it was usefull for you vote up!

Visit my github profile //github.com/TheoRelativity/

jpatokal at iki dot fi

13 years ago

Warning for Linux [Ubuntu] users!  Your system will *only* support the locales installed on your OS, in the *exact* format given by your OS.  [See also the PHP setlocale man page.]  To get a list of them, enter locale -a, which will give you something like this:

C
en_US.utf8
ja_JP.utf8
POSIX

So this machine only has English and Japanese!  To add eg. Finnish, install the package:

sudo apt-get install language-pack-fi-base

Rerun locale -a, and "fi_FI.utf8" should appear.  Make sure you're using the same name in your PHP code:

setlocale[LC_ALL, "fi_FI.utf8"];

Adjust your po paths so that they match, e.g. "./locale/fi_FI.utf8/LC_MESSAGES/messages.po".

Now restart Apache, and it should finally work.  Figuring this out took quite a while...

sasq1 at go2 dot pl

13 years ago

And what about pgettext and npgettext? They are there in the gettext documentation, but there aren't in PHP. They're very useful if you have the same messages for translation, but in different contexts, so they should be translated separately and probably differently.

Fortunately, there is a simple work-around, which may help:
From the gettext.h header one can find out that pgettext[] is only a macro calling dcgettext[] internally with properly mangled parameters - msgctxt and msgid are glued together with use of ASCII char 4 [EOT, End Of Text]. The same way they're written in .mo files, so it's possible to refer them this way.

Here's my "emulated" pgettext[] function:



By default, xgettext doesn't support pgettext function for PHP source files. But there is a parameter which can work-around it. Here's how I call xgettext:

   xgettext --force-po --keyword="pgettext:1c,2" -c -o messages.po sourceFile.php

In sourceFile.php I use the following test code:

   pgettext['menu', 'Open'];  //Substitute "Otwórz"
   pgettext['forum', 'Open'];  //Substitute "Otwarty", different context

Generated .po file fragment:

   msgctxt "menu"
   msgid "Open"
   msgstr "Otwórz"

      msgctxt "forum"
   msgctxt "Open"
   msgstr "Otwarty"

I've tested it out and everything works fine :-] If anyone have some further suggestions or fixes, please write ;-]

rainwalker at seznam dot cz

13 years ago

My PHP app was made for UTF-8 but i had a problem that gettext always returned all texts in ISO-8859-2 instead of UTF-8.

Then i found out that you have to set locale in PHP exactly to encoding you request. So when i wanted czech UTF-8 i used:

setlocale[LC_ALL, "cs_CZ.UTF-8"];

Now it works...

yuricardenas at gmail dot com

13 years ago

*An important thing to keep in mind*:
Do not forget to set the charset in .po file!
For example:

"Content-Type: text/plain; charset=UTF-8\n"

Then PHP will be able to find the .mo file you generated, using msgfmt, from the .po file WITH CHARSET SET.

Because of this I've wasted a lot of time debugging my code, testing every single little changes people suggested over this manual and Internet:



As well as what locale directory name to set:
./locale/pt_BR.UTF8/LC_MESSAGES/mydomain.mo
or
./locale/pt_BR/LC_MESSAGES/mydomain.mo
or
./locale/pt/LC_MESSAGES/mydomain.mo

Finally, the code which brought the right translated strings [also with the correct charset] was:

Chủ Đề