当 $LANG 最初未设置时 Locale::TextDomain 失败

发布于 2024-12-06 02:49:51 字数 578 浏览 0 评论 0原文

我们有一个使用 perl Locale::TextDomain/gettext 的翻译系统。 我们遇到的问题是本地化在一个系统上有效,而在另一个系统上无效。

唯一明显的区别是工作系统上的环境变量 LANG 等于“en_GB.UTF-8”,而非工作系统上未定义 LANG。非工作系统没有 /etc/default/locale

在损坏的系统上导出 LANG 使其工作,而在工作系统上取消设置会破坏它。

以下脚本演示了:

#!/usr/bin/perl

use strict;
use warnings;

use Locale::TextDomain ('appdomain', '/path/to/language/folders');
use POSIX (':locale_h');

setlocale(LC_ALL, '');
$ENV{'LANGUAGE'} = 'it';

print __('Back'), "\n";

如果我们无论如何都指定 LANGUAGE,为什么需要初始 $LANG 设置?

运行“Ubuntu 10.04.2 LTS”和 Locale::TextDomain 1.20

We have a system of translation that uses perl Locale::TextDomain/gettext.
We have an issue where the localization works on one system and not the other.

The only discernible difference is that environment variable LANG equals 'en_GB.UTF-8' on the working system and LANG is not defined on the non-working system. The non working system has no /etc/default/locale

exporting LANG on the broken system makes it work and unsetting on the working system breaks it.

The following script demonstrates:

#!/usr/bin/perl

use strict;
use warnings;

use Locale::TextDomain ('appdomain', '/path/to/language/folders');
use POSIX (':locale_h');

setlocale(LC_ALL, '');
$ENV{'LANGUAGE'} = 'it';

print __('Back'), "\n";

Why does there need to be an initial $LANG set if we're specifying the LANGUAGE anyway?

Running 'Ubuntu 10.04.2 LTS' and Locale::TextDomain 1.20

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

快乐很简单 2024-12-13 02:49:51

区域设置“”(空字符串)表示系统区域设置。 setlocale() 的所有已知 Un*x 实现都使用环境变量来设置区域设置。您在调用 setlocale()之后设置环境变量,因此它会被忽略。

Locale::TextDomain 在这里不会失败。这是一个配置错误。

有多种方法可以解决此类问题。如果您知道要使用的语言,则可以让 libintl-perl 完成繁重的工作:

use Locale::Util qw(set_locale);

set_locale(LC_MESSAGES, 'pt', 'BR', 'utf-8');

对 set_locale() 的调用将尝试所有已知的区域设置标识符约定,以将语言设置为巴西葡萄牙语“pt”(“BR”) )。它还会尝试选择 UTF-8 区域设置。请参阅http://search.cpan.org/dist/ libintl-perl/lib/Locale/Util.pm#FUNCTIONS 了解更多信息!特意选择名称 set_locale() 以避免与 POSIX.pm 中的 setlocale() 发生名称冲突。

从 libintl-perl 1.22 开始,您还可以切换到“哑” gettext 后端:

use Locale::Messages qw(select_package);

BEGIN { Locale::Messages->select_package('gettext_dumb') }

“哑”后端从不调用 setlocale() 来查找当前语言环境设置,而只检查环境变量。请参阅 http://search.cpan.org/dist/libintl -perl/lib/Locale/gettext_dumb.pm 了解这种方法的优点和缺点。最大的缺点是 C 代码不遵守这一点,因此 $!例如不会使用配置的语言。

或者,您可以切换到“gettext_pp”后端,就像上面“gettext_dumb”所述。这将强制使用 gettext 运行时的纯 Perl 实现。这样做的主要优点实际上是更容易调试。但 C 实现也存在细微差别。

附注:您应该记住,环境变量 LANGUAGE 是 GNU 扩展,可能无法在非 GNU 环境中工作。

The locale "" (the empty string) denotes the system locale. All known Un*x implementations of setlocale() use then environment variables to set the locale. You are setting the environment variable after the call to setlocale(), and it is therefore ignored.

Locale::TextDomain does not fail here. It is a configuration error.

There are several approaches for fixing such problems. If you know the language that you want to use you can let libintl-perl do the heavy liftings:

use Locale::Util qw(set_locale);

set_locale(LC_MESSAGES, 'pt', 'BR', 'utf-8');

The call to set_locale() will try all known conventions for locale identifiers for setting the language to Portuguese 'pt' for Brazil ('BR'). It will also try to select a UTF-8 locale. See http://search.cpan.org/dist/libintl-perl/lib/Locale/Util.pm#FUNCTIONS for more information! The name set_locale() was intentionally chosen to avoid a name clash with setlocale() from POSIX.pm.

Beginning with libintl-perl 1.22, you can also switch to the "dumb" gettext backend:

use Locale::Messages qw(select_package);

BEGIN { Locale::Messages->select_package('gettext_dumb') }

The "dumb" backend never bothers to call setlocale() to find the current locale settings but only inspects environment variables. Please see http://search.cpan.org/dist/libintl-perl/lib/Locale/gettext_dumb.pm for the pros and cons of this approach. The biggest disadvantage is that C code does not honor this, so that $! for example will not use the configured language.

Alternatively you can switch to the 'gettext_pp' backend just as described for 'gettext_dumb' above. This will force using the pure Perl implementation of the gettext runtime. The main advantage of this is actually that it is easier to debug. But there are also subtle differences to the C implementation.

Just a side note: You should keep in mind that the environment variable LANGUAGE is a GNU extension and may not work in a non-GNU environment.

笨笨の傻瓜 2024-12-13 02:49:51

$LANG 是大多数 Unixy 系统中使用的系统范围默认变量。 $LANGUAGE 用于更具体的目的。

现在的系统确实应该将 $LANG 设置为合理的默认值。让系统管理员将其放入 /etc/profile 中或系统范围 shell 默认值所需的任何位置。

$LANG is the system-wide default variable used across most Unixy systems. $LANGUAGE is for more specific purposes.

Systems these days really ought to have $LANG set to a sensible default. Get the sysadmin to put it in /etc/profile or wherever it need to be for system-wide shell defaults.

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