Php gettext directory structure in different environments

I use gettext for internationalization for my php files. I have two servers; sandbox server and release server. in a sandbox server, a directory like locale/LC_MESSAGES/en does not work, and instead I have to use locale/LC_MESSAGES/en_GB . But with "en_GB" it does not work on my production server, and "ru" works fine. for some languages, such as Portuguese, I have pt_PT and pt_BR (Brazilian Portuguese). Therefore, I prefer to use the structure "A_B".

I have no idea how gettext detects these folders. Is there a standard way to use the same folder structure?

+4
source share
1 answer

If you use your code on Linux, gettext only works with locales already installed on the OS. This means that if you set the language standard to en_GB , then if only the installed language standard is en_GB.utf8 or en_US , then you will not receive translations.

Try this in both environments and compare the results:

 locale -a 

It gives you a list of all installed locales:

 en_US en_US.ISO8859-1 en_US.ISO8859-15 en_US.US-ASCII en_GB en_GB.utf8 de_DE de_DE.utf8 C POSIX 

Now you need to make sure that both environments have the same locales; If you need en_US.utf8 , en_AU and en_AU.utf8 , you can create missing locales based on the existing one (read the localedef manpages for details):

 sudo localedef -c -i en_US -f UTF-8 en_US.utf8 sudo localedef -c -i en_GB -f UTF-8 en_AU sudo localedef -c -i en_GB -f UTF-8 en_AU.utf8 

In addition, it follows that the common best practice for using gettext in PHP is:

 <?php // Set language to German putenv('LC_ALL=de_DE.utf8'); setlocale(LC_ALL, 'de_DE.utf8'); // Specify location of translation tables bindtextdomain("myPHPApp", "./locale"); // Choose domain textdomain("myPHPApp"); // Translation is looking for in ./locale/de_DE.utf8/LC_MESSAGES/myPHPApp.mo now // Print a test message echo gettext("Welcome to My PHP Application"); // Or use the alias _() for gettext() echo _("Have a nice day"); ?> 

Although you can simply drop the encoding and just de_DE , it’s good practice to type characters in a locale, as in some specific cases you may need to support content in character sets other than Unicode. See below

 <?php // Set language to German written in Latin-1 putenv('LC_ALL=de_DE.ISO8859-1'); setlocale(LC_ALL, 'de_DE.ISO8859-1'); ?> 
+4
source

Source: https://habr.com/ru/post/1497708/


All Articles