设置 gettext() 时出现问题

发布于 2024-11-07 06:24:36 字数 3310 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泛滥成性 2024-11-14 06:24:36

我怀疑您尝试通过 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 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.

旧时光的容颜 2024-11-14 06:24:36

尝试 setlocale(LC_MESSAGES, "es_ES");

Try setlocale(LC_MESSAGES, "es_ES");

轻许诺言 2024-11-14 06:24:36

我不知道如何在 php 中执行此操作,但在 bash 中

export LC_MESSAGES="LL_CC"

,其中 LL - 语言环境,CC - 国家/地区。例如 en_US 或 en_US.UTF8。我花了半天的时间来解决这个问题,并且只导出 LC_MESSAGES 有效。所以,我建议,在 php 中它会是

putenv('LC_MESSAGES='.$lang);

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);
深居我梦 2024-11-14 06:24:36

我尝试帮助那些仍在寻找此内容的人: PHP gettext 不起作用

希望有帮助。

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

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文