41

i've been googling for the last hour or so trying to find a complete working example of gettext in php - all source files, po files, with proper paths, etc. Just a "hello world" with all the required files.

I was wondering if anybody has such a working example. thanks in advance.

Sherif Buzz
  • 1,218
  • 5
  • 21
  • 38
  • 3
    My personal, subjective opinion is that PHP's gettext functions suck *ass*. [Zend_Translate](http://framework.zend.com/manual/en/zend.translate.html) is much better and [supports gettext files](http://framework.zend.com/manual/en/zend.translate.adapter.html) as well – Pekka Oct 31 '10 at 11:08
  • There are plenty of guides online talking about how to write PHP code that uses gettext, but a lot of them are missing set-up information (e.g. installing the right locales on your Linux machines, setting up gettext on Window, etc.). If you've got the PHP code ready, but gettext still doesn't work, I've compiled a list of issues (and solutions) in an article while setting up gettext: https://blog.terresquall.com/2020/09/troubleshooting-php-gettext/ – John Doe Dec 31 '20 at 06:33

3 Answers3

4

I've read these other guides posted for this question, but none of them was complete enough for me and also they seem to be rushing into the subject.

THIS GUIDE is divided in many parts, you'll learn from a simple Hello World to a complex plural management. It not just explain what every function does, but also the values used for these functions.

As a bonus, this guide teaches how to use Poedit which is a very helpful toll for working with gettext internationalization and a must have if you are dealing with a huge project.

The OP may change the accepted answer to this one if he also think that guide is better. So this answer can get more attention and more gettext beginners like me can be helped.

cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
  • When you add a new locale to a server (for testing use sudo locale-gen xx_XX) ensure that you restart the WWW server, otherwise you'll get errors. Also, server locales using "sudo locale-gen es_ES" won't actually install es_ES but es_ES.ISO88591 ("sudo locale-gen es_ES.utf8" will install es_ES.utf8) Additionally, double check the string being used for setlocale(), it has to match EXACTLY the locale on the server. For example, setlocale(LC_ALL, 'es_ES'); won't work on Ubuntu when running from a terminal "locale -a" shows "es_ES.utf8". setlocale(LC_ALL, 'es_ES.utf8'); will now work. – bnoeafk Aug 30 '17 at 21:20
4

There is an outstanding five part tutorial at Sitepoint.com called Localizing PHP Applications “The Right Way”. I highly recommend reviewing all five parts!

Jabari
  • 5,359
  • 3
  • 26
  • 32
0

I have created this xgettext command line for my project, it can be used by you as an example:

xgettext --files-from=source_files_list.txt --from-code=UTF-8 -L PHP -o mvap.po -d mvap -p languages --keyword=__:1 --keyword=_e:1 --keyword=_x:1,2c --keyword=_n:1,2 --keyword=_nx:1,2,4c --keyword=_n_noop:1,2 --keyword=_nx_noop:1,2,3c --keyword=_ex:1,2c --keyword=esc_html_e:1 --keyword=esc_html__:1

In this example the source_files_list.txt should be a text file containing the name of the files to be analyzed by xgettext separated by new lines.

the "--keyword=..." options are for recognizing the good functions used for translating strings (in this functions there are all wordpress translation functions I found)

it outputs "mvap.po" in the "./languages" folder with domain "mvap"

Nicolas Bourdon
  • 115
  • 1
  • 8