PHP Gettext - No translation
Asked Answered
P

6

7

I am trying to use the PHP gettext extension in order to translate some strings. All functions appear to return the correct values but calling gettext()/_() returns the original string only. The PO/MO files seem correct and I believe I have set the directories up correctly. I am running WAMP Server with PHP 5.3.10 on Windows (also tried running 5.3.4 and 5.3.8 because I have the installations).

Firstly, 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 have created the following file structure:

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

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

The PO file used to generate the MO is like so (only the relevant entry given):

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

This was generated using PoEdit. I have restarted Apache after adding any new .MO file. Also note that I was previously using Zend_Translate with Gettext and it was translating correctly. I wish to rely on the native gettext extension, though, in part because I am attempting to create a lightweight framework of my own.

Any help would be appreciated.

Edit: Amended directory structure. Note - will be able to try recent answers within 24hrs.

Paperweight answered 7/5, 2012 at 18:4 Comment(2)
Shouldn't your locale and directory be es_ES.UTF-8?Prune
@Prune I've tried that locale and directory but setlocale() then returns false.Paperweight
M
3

I set this up on my XAMPP instance and figure it out.

  • Flat out setlocale does not work on Windows, so what it returns is irrelevant.
  • For Windows you set the locale using the standard language/country codes (in this case es_ES is Spanish as spoken in Spain)
  • Under your locale directory create es_ES/LC_MESSAGES/. This 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 am not sure if this made a different, but I did two things when creating the po file. In poEdit under File -> Preferences I changed the Line ending format to Windows. And after I created the initial po with poEdit I opened the file in Notepad++ and switched the encoding type to UTF-8 as poEdit did not do this.

I hope this at least points you in the right direction.

References

PHP Localization Tutorial on Windows

Country Codes

Language Codes

Marmite answered 15/5, 2012 at 23:24 Comment(0)
F
3

Your code mentions this as the return value from bindtextdomain:

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

With the setlocale of Spanish_Spain.1252 and textdomain of messages, calls to gettext will look in this path:

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

But you created the file structure of:

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

Edit

Okay, so that didn't help. I've 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 like this:

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

Hope it helps, although it looks almost identical to yours. A few things I found online:

  • It's important to set the character set in .po file
  • Spaces inside the localization file might have a UTF8 alternative, so be wary of key lookups failing. Probably the best thing to test first is keys without spaces at all.
Fatimafatimah answered 16/5, 2012 at 3:26 Comment(1)
Ahh, I'm sorry - I copied that incorrectly. I have actually created C:\wamp\www\new2\www\locale\Spanish... etc. I will amend my question now. Thanks for spotting that, though!Paperweight
F
2

A 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 (not 1252, as you change the code base).

To track what directory it's looking for, you can install Process monitor (http://technet.microsoft.com/en-us/sysinternals/bb896645). It spews out bucket loads on info, but you should be able to find out which file/directory is being looked for.

(My other thought is to check file permissions - but if you already had something similar in Zend_Translate, then probably not the cause, but worth checking anyway).

Sorry if not good - but might give you a clue.

Fluorocarbon answered 14/5, 2012 at 11:36 Comment(1)
Thank you - I'll have a look at the Process monitor later and see if it helps me out. Could be exactly what I'm looking for.Paperweight
B
1

Look here. It works for me on windows and on linux also. The last values in the array works for windows. List of languages names can be found here. My catalogs are in

./locales/en/LC_MESSAGES/domain.mo
         /cs/LC_MESSAGES/domain.mo
Blubbery answered 9/5, 2012 at 19:30 Comment(2)
@FandaI've tried using the Windows locale names (including esn and esp - as in my question). I've just tried several examples directly from the post you linked to, setting the locale to array('de_DE.UTF-8', 'de_DE', 'de', 'german'). I created a locale/[VALUE-FROM-ARRAY]/LC_MESSAGES/messages.mo file for each of these values and restarted Apache before trying again - no luck. All functions returning correctly, though. Any further suggestions?Paperweight
Did you try to rename lang subdir to 2 letters code? (locales/de/LC... ?Blubbery
M
1

I have never tried using gettext on Windows, but each time I had problems with gettext on linux systems, the reason was that an appropriate language pack was not installed.

Materials answered 16/5, 2012 at 0:2 Comment(0)
S
0

Problem can be also that when you change your *.po and *.mo files, you have to restart the Apache Server. This can be problem, so you can use workaround - always rename these files to some new name and they will be reloaded.

Skyrocket answered 12/5, 2012 at 9:37 Comment(1)
@jasirPlease read my original question; I have restarted Apache whenever my .MO files have changed. Thanks.Paperweight

© 2022 - 2024 — McMap. All rights reserved.