How to use po/pot files with php?
Asked Answered
G

3

6

I have a .po and a .mo file in a folder whose address is /locale/nld_nld/LC_MESSAGES/. Both the files' names are messages. I've been trying to use the following code:

try.php:

<?php
require_once("Localization.php");
echo _("Hello World!");
?>

the Localization.php goes here:

<?php
$locale = "nld_nld";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
bind_textdomain_codeset("messages", 'UTF-8');
textdomain("messages");
?>

Both the try.php and the Localization files are in the same directory. Also, I use xampp. I also implemented the phpinfo();. In the table, in front of 'GetText Support', enabled was mentioned. The messages.po and the messages.mo files are valid files which I created using poEdit. I'm a windows user. However, when I opened try.php, it simply echoed Hello World! (not its translated string. Also, I've translated the .po file 100% (according to poEdit). Still, I'm not getting the results. A little help on this would be appreciated.

Thanks in advance!

Gearwheel answered 13/1, 2011 at 16:57 Comment(1)
Don't put a GET string unfiltered to an environment variable!Eller
B
2

When looking back at code I have in place on a working site for spanish translation, I notice I also have the same lines in a different order, perhaps that makes a difference? For example:

<?php
putenv('LC_MESSAGES=es_ES');
setlocale(LC_MESSAGES, 'es_ES.utf8');
bindtextdomain('es','/full/path/here/application/locale');
textdomain('messages-es');
bind_textdomain_codeset('messages-es', 'UTF-8');
?>

For the above code to work, my .mo file is in the /full/path/here/application/locale/es_ES.utf8 folder.

Perhaps the following code I've used before might help you troubleshoot further:

<?php
function TestLang( $langCode ) {
  echo( '<b>TESTING LANG CODE: ' . strtolower( $langCode ) . '</b><br />' );
  putenv( 'LC_MESSAGES=' . strtolower( $langCode ) . '_' . strtoupper( $langCode ));
  echo( 'LC_MESSAGES: ' . getenv( 'LC_MESSAGES' ) . '<br />' );
  $localeResult = setlocale( LC_MESSAGES, strtolower( $langCode ) . '_' . strtoupper( $langCode ) . '.utf8' );
  echo( 'Locale: ' . $localeResult . '<br />' );
  $textDomain = bindtextdomain( strtolower( $langCode ), ROOT . '/' . APP_DIR . '/locale' );
  echo( 'Text Domain: ' . $textDomain . '<br />' );
  textdomain( strtolower( $langCode ));
  $codeSet = bind_textdomain_codeset( strtolower( $langCode ), 'UTF-8' );
  echo( 'Code Set: ' . $codeSet . '<br />' );
  echo( '.mo File: ' . $textDomain . '/' . $localeResult . '/LC_MESSAGES/' . $langCode . '.mo<br />' );
  echo( '<br />-- ' . _( 'Hello World!' ) . ' --<br />' );
}

TestLang( 'en' );
TestLang( 'de' );
TestLang( 'es' );
TestLang( 'fr' );
// etc..
?>
Bimestrial answered 9/5, 2011 at 15:37 Comment(2)
the textdomain() is typically the last function call (actually it might be needed before every gettext aka _() call if you work with mutiple domains. First you bind all domains, then you select one.Impressionable
In your example you use two different domain names 'es' and 'messages-es'. That might not work (and typically you do not have the language identifier in the file name as it is defined by the directory instead). So "/full/path/here/application/locale/es_ES/LC_MESSAGES/messages.mo" will be the filename of the domain "messages" and the LC_TYPE es_ES.Impressionable
P
0

The problem could be the "-" in UTF-8. There are also other lines to add for windows compatibility. Try to read this: http://www.php.net/manual/en/function.setlocale.php#89076

Pointillism answered 23/1, 2011 at 11:30 Comment(0)
I
0

Output the return value of $result=bindtextdomain($domain,$path), it will tell you if your local path is indeed the path you expect your translation files to life. And then check the file access permissions of those files if they (and the directories up to the root) accesable by the www-user. It is somewhere in ./locale/LC_MESSAGES/nld_nld/

textdomain($domain) should be the last method (before the actual gettext translations). The domain parameter of bindtextdomain and textdomain should be the same domain name (it was not in your answer) and equal to the mo files base name.

Make also sure none of your php files uses another textdomain, especially when you use "require_once".

BTW, the code you have looks for a locale identifier in the request (with no filter) and uses nld_nld as a default failback, I would put a normal language (primary site language like "en") there instead.

Impressionable answered 13/12, 2012 at 1:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.