PHP Gettext - No Translation

I am trying to use the gettext PHP extension to translate some lines. All functions return the correct values, but the gettext() / _() call returns only the original string. The PO / MO files seem to be correct, and I believe that I configured the directories correctly. I am running WAMP Server with PHP 5.3.10 on Windows (also tried to work with 5.3.4 and 5.3.8 because I have installations).

First, see /new2/www/index.php :

 $locale = 'esn'; # returns Spanish_Spain.1252 in var dump putenv("LC_ALL={$locale}"); // Returns TRUE setlocale(LC_ALL, $locale); // Returns 'Spanish_Spain.1252' $domain = 'messages'; bindtextdomain($domain, './locale'); // Returns C:\wamp\www\new2\www\locale bind_textdomain_codeset($domain, 'UTF-8'); // Returns UTF-8 textdomain($domain); // Returns'messages' print gettext("In the dashboard"); // Prints the original text, not the translation. exit; 

I created the following file structure:

 www/new2/www/locale/Spanish_Spain.1252/LC_MESSAGES/messages.mo 

I also tried replacing Spanish_Spain.1252 with: es_ES , esn , esp , Spanish and Spanish_Spain .

The PO file used to generate the MO is similar (corresponding entry only):

 #: C:\wamp\www\new2/www/index.php:76 msgid "In the dashboard" msgstr "TRANSLATED es_ES DASHBOARD" 

This was generated using PoEdit. I restarted Apache after adding a new .MO file. Also note that I previously used Zend_Translate with Gettext, and it translated correctly. I want to rely on my own gettext extension, though, in part because I'm trying to create a lightweight structure of my own.

Any help would be appreciated.

Edit: Changed directory structure. Note. You can try the latest answers within 24 hours.

+6
source share
6 answers

I installed this on my XAMPP instance and figured it out.

  • Flat out setlocale does not work on Windows, so what it returns does not matter.
  • For Windows, you set the locale using standard language / country codes (in this case es_ES is Spanish, as they say in Spain).
  • In your locale directory, create es_ES / LC_MESSAGES /. This is where your messages.mo file lives.

     $locale = 'es_ES'; putenv("LC_ALL={$locale}"); // Returns TRUE $domain = 'messages'; bindtextdomain($domain, './locale'); bind_textdomain_codeset($domain, 'UTF-8'); textdomain($domain); // Returns'messages' print gettext("In the dashboard"); exit; 

I'm not sure if this has changed, but I did two things when creating the po file. In poEdit under "File β†’ Settings" I changed the line ending format on Windows. And after I created the original po with poEdit, I opened the file in Notepad ++ and switched the encoding type to UTF-8, since poEdit did not.

I hope this at least indicates that you are in the right direction.

References

Windows PHP Localization Tutorial

Country Codes

Language Codes

+3
source

Your code mentions this as a return value from bindtextdomain :

 C:\wamp\www\new2\www\locale 

With setlocale from Spanish_Spain.1252 and textdomain from messages gettext calls will look this way:

 C:\wamp\www\new2\www\locale\Spanish_Spain.1252\LC_MESSAGES\messages.mo 

But you created a file structure:

 www/new2/locale/Spanish_Spain.1252/LC_MESSAGES/messages.mo ^^ www/ missing here 

Edit

Okay, so that didn't help. I created a test script on Windows and using POEdit like you:

 $locale = "Dutch_Netherlands.1252"; putenv("LC_ALL=$locale"); // 'true' setlocale(LC_ALL, $locale); // 'Dutch_Netherlands.1252' bindtextdomain("messages", "./locale"); // 'D:\work\so\l10n\locale' textdomain("messages"); // 'messages' echo _("Hello world"); // 'Hallo wereld' 

My folder structure is as follows:

 D:\work\so\l10n\ \locale\Dutch_Netherlands.1252\LC_MESSAGES\messages.mo \locale\Dutch_Netherlands.1252\LC_MESSAGES\messages.po \test.php 

Hope this helps, although it looks almost identical to yours. A few things I found on the Internet:

  • It is important to set the character set in the .po file
  • The spaces inside the localization file may have an alternative to UTF8, so be careful when key search queries fail. Probably the best thing to check first is the keys without spaces.
+3
source

Suggestion: you may need the full locale for the .mo file. This is probably Spanish_Spain.UTF8 or esn_esn.UTF8 or esn_esp.UTF8 (and not 1252 when changing the code base).

To find out which directory it is located in, you can install Process Monitor (http://technet.microsoft.com/en-us/sysinternals/bb896645). It displays bucket loading information, but you need to know which file / directory is being viewed.

(My other thought is to check file permissions, but if you already have something similar in Zend_Translate, then this is probably not the reason, but it’s worth checking in any case).

Sorry if not good, but may give you a clue.

+2
source

Take a look here . It works for me on both windows and linux. The last values ​​in the array work for windows. A list of language names can be found here . My directories are in

 ./locales/en/LC_MESSAGES/domain.mo /cs/LC_MESSAGES/domain.mo 
+1
source

I never tried to use gettext on Windows, but every time I had problems with gettext on Linux systems, the reason was that the corresponding language pack was not installed.

+1
source

The problem is also that when changing the * .po and * .mo files, you must restart the Apache server . This can be a problem, so you can use a workaround - always rename these files to some new name and they will be reloaded.

0
source

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


All Articles