PHP Gettext - No translation

不羁岁月 提交于 2019-12-05 08:09:56

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

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.

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.

Fanda

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!