complete example of gettext in php

35,898

Solution 1

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

Solution 2

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.

Share:
35,898
Sherif Buzz
Author by

Sherif Buzz

Updated on February 03, 2022

Comments

  • Sherif Buzz
    Sherif Buzz over 2 years

    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.

    • Pekka
      Pekka over 13 years
      My personal, subjective opinion is that PHP's gettext functions suck ass. Zend_Translate is much better and supports gettext files as well
    • John Doe
      John Doe over 3 years
      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: blog.terresquall.com/2020/09/troubleshooting-php-gettext
  • bnoeafk
    bnoeafk over 6 years
    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.