设置 gettext() 时出现问题
我正在开发一个 php 应用程序。现在我正在尝试将其翻译成不同的语言。
为此,我使用 gettext()。但我在配置上遇到了问题。我已经正确安装了库。我在代码周围有 gettext(textToTranslate) ,并且我已经毫无问题地创建了 .mo 和 .po 文件。
我的 index.php
上的配置是(在 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);
我的文件结构是:
/opt/lampp/htdocs/blanca/gettext/locale/es_ES/LC_MESSAGES/project.po
/opt/lampp/htdocs/blanca/gettext/locale/es_ES/LC_MESSAGES/project.mo
但我仍然看到英文代码,qhich 是默认语言。有人可以帮我解决这个问题吗?预先感谢
编辑
@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
编辑
在 strace
comand 命令下运行一个小 php 脚本
<?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");
?>
: 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(没有这样的 文件或目录) 打开(“/usr/share/locale/locale.alias”, O_RDONLY) = 3 打开(“/usr/share/locale/es_ES.utf8/LC_MESSAGES/blanca/gettext.mo”, O_RDONLY) = -1 ENOENT(没有这样的文件或 目录) 打开(“/usr/share/locale/es_ES/LC_MESSAGES/blanca/gettext.mo”, O_RDONLY) = -1 ENOENT(没有这样的文件或 目录) 打开(“/usr/share/locale/es.utf8/LC_MESSAGES/blanca/gettext.mo”, O_RDONLY) = -1 ENOENT(没有这样的文件或 目录) 打开(“/usr/share/locale/es/LC_MESSAGES/blanca/gettext.mo”, O_RDONLY) = -1 ENOENT(没有这样的文件或 目录) 打开(“/usr/share/locale-langpack/es_ES.utf8/LC_MESSAGES/blanca/gettext.mo”, O_RDONLY) = -1 ENOENT(没有这样的文件或 目录) 打开(“/usr/share/locale-langpack/es_ES/LC_MESSAGES/blanca/gettext.mo”, O_RDONLY) = -1 ENOENT(没有这样的文件或 目录) 打开(“/usr/share/locale-langpack/es.utf8/LC_MESSAGES/blanca/gettext.mo”, O_RDONLY) = -1 ENOENT(没有这样的文件或 目录) 打开(“/usr/share/locale-langpack/es/LC_MESSAGES/blanca/gettext.mo”, O_RDONLY) = -1 ENOENT(没有这样的文件或 目录)
prog.php is located in my application directory.
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑您尝试通过
setlocale
设置的区域设置在您的系统上不可用。命令locale -a
(从 shell 运行)返回什么?确保已安装的区域设置的值与您尝试设置的区域设置匹配。如果您的西班牙语语言环境丢失,则可能仍然需要使用dpkg-reconfigure locales
。我不知道原因,但事实是(或者可能曾经,在 2008 年左右的 Debian 或 Ubunto 上,我在 2009 年工作过),
setlocale
将失败,如果您的系统上未安装该语言环境。这并不是很明显,因为您在应用程序中提供消息文件,并且似乎不依赖于任何系统区域设置文件或设置 - 但您知道,可能有一个原因。I suspect the locales you're trying to set via
setlocale
aren't available on your system. What does the commandlocale -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 isdpkg-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.尝试 setlocale(LC_MESSAGES, "es_ES");
Try setlocale(LC_MESSAGES, "es_ES");
我不知道如何在 php 中执行此操作,但在 bash 中
,其中 LL - 语言环境,CC - 国家/地区。例如 en_US 或 en_US.UTF8。我花了半天的时间来解决这个问题,并且只导出 LC_MESSAGES 有效。所以,我建议,在 php 中它会是
I'm not sure how to do this in php, but in bash
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
我尝试帮助那些仍在寻找此内容的人: PHP gettext 不起作用
希望有帮助。
I've made an attempt helping those who is still looking for this here: PHP gettext doesn't works
Hope that helps.