开发者

Problem setting up gettext()

I am working on a php application. Now I am trying to translate it into different languages.

To do it, I am using gettext(). But I have a problem on the configuration. I have the library correctly installed. I have gettext(textToTranslate) around the 开发者_C百科code and I have created with no problems .mo and .po files.

The configuration on my index.php is (working on xampp, Ubuntu):

// Language
$lang = 'es_ES';

// Domain
$text_domain = 'project';

putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang);

bindtextdomain($text_domain, './locale' );

bind_textdomain_codeset($text_domain, 'UTF-8');

textdomain($text_domain);

and my structure of files is:

/opt/lampp/htdocs/blanca/gettext/locale/es_ES/LC_MESSAGES/project.po 
/opt/lampp/htdocs/blanca/gettext/locale/es_ES/LC_MESSAGES/project.mo

But I am still seeing the code in English, qhich is the default language. Could anybody help me on this?? Thanks in advance

EDIT

@ubuntu:~$ locale -a
C
es_AR.utf8
es_BO.utf8
es_CL.utf8
es_CO.utf8
es_CR.utf8
es_DO.utf8
es_EC.utf8
es_ES.utf8
es_GT.utf8
es_HN.utf8
es_MX.utf8
es_NI.utf8
es_PA.utf8
es_PE.utf8
es_PR.utf8
es_PY.utf8
es_SV.utf8
es_US.utf8
es_UY.utf8
es_VE.utf8
POSIX

EDIT

Running a little php script under strace comand

<?php
// Idioma

$lang = 'es_ES.utf8';

// Dominio

$text_domain = 'blanca';

// Dependiendo de tu OS putenv/setlocale configurarán tu idioma.

putenv('LC_ALL='.$lang);

setlocale(LC_ALL, $lang);



// La ruta a los archivos de traducción

bindtextdomain($text_domain, './gettext/locale' );
// El codeset del textdomain

bind_textdomain_codeset($text_domain, 'UTF-8');

// El Textdomain
textdomain($text_domain);

// Print a test message
echo gettext("User");


// Or use the alias _() for gettext()
echo _("User");
?> 

Command: strace -e trace=file -o test.txt php prog.php

getcwd("/opt/lampp/htdocs/blanca", 4096) = 25 lstat("/opt/lampp/htdocs/blanca/prog.php", {st_mode=S_IFREG|0644, st_size=521, ...}) = 0 lstat("/opt/lampp/htdocs/blanca", {st_mode=S_IFDIR|0777, st_size=4096, ...}) = 0 lstat("/opt/lampp/htdocs", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat("/opt/lampp", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat("/opt", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 getcwd("/opt/lampp/htdocs/blanca", 4096) = 25 lstat("/opt/lampp/htdocs/blanca/./locale", 0x7fffb2c1a670) = -1 ENOENT (No such file or directory) open("/usr/share/locale/locale.alias", O_RDONLY) = 3 open("/usr/share/locale/es_ES.utf8/LC_MESSAGES/blanca/gettext.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale/es_ES/LC_MESSAGES/blanca/gettext.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale/es.utf8/LC_MESSAGES/blanca/gettext.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale/es/LC_MESSAGES/blanca/gettext.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale-langpack/es_ES.utf8/LC_MESSAGES/blanca/gettext.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale-langpack/es_ES/LC_MESSAGES/blanca/gettext.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale-langpack/es.utf8/LC_MESSAGES/blanca/gettext.mo", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/share/locale-langpack/es/LC_MESSAGES/blanca/gettext.mo", O_RDONLY) = -1 ENOENT (No such file or directory)

prog.php is located in my application directory.


I suspect the locales you're trying to set via setlocale aren't available on your system. What does the command locale -a (run from the shell) return? Make sure the values of the installed locales match the locales you're trying to set. If your Spanish locales are missing, the thing to do probably still is dpkg-reconfigure locales.

I don't know the reason, but the fact is (or possibly was, on a Debian or Ubunto from around 2008 which I worked in 2009) that setlocale will fail if the locale isn't installed on your system. Which is not immediately obvious, as you're supplying the message files with your application, and don't appear to depend on any system locale files or settings - but you know, there may be a reason.


Try setlocale(LC_MESSAGES, "es_ES");


I'm not sure how to do this in php, but in bash

export LC_MESSAGES="LL_CC"

where LL - locale, CC - country. smth like en_US or en_US.UTF8. I was fighting this problem for a half of my day and only exporting LC_MESSAGES worked. So, I suggest, in php it woult be

putenv('LC_MESSAGES='.$lang);


I've made an attempt helping those who is still looking for this here: PHP gettext doesn't works

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜