PHP 和 Gettext 在我的服务器上不起作用

发布于 2024-09-03 06:04:56 字数 900 浏览 10 评论 0原文

我有一个网站。我正在尝试让 gettext 正常工作,以便我的英语、瑞典和挪威网站可以出现。我无法让它工作。我做错了什么?

这是我的配置代码:

// define constants ( defualt - danish )
$lang = 'da_DA';
$lang_short = '';
$lang_prefix = 'da';

if ( isset( $_GET['lang'] ) )
{
    switch( $_GET['lang'] )
    {
        case 'en':
            $lang = 'en_EN';
            $lang_short = 'en/';
            $lang_prefix = 'en';
            break;
        case 'se':
            $lang = 'se_SE';
            $lang_short = 'se/';
            $lang_prefix = 'se';
            break;
        case 'no':
            $lang = 'no_NO';
            $lang_short = 'no/';
            $lang_prefix = 'no';
            break;
    }
}
define( 'LANG', $lang_short );
define( 'LANG_PREFIX', $lang_prefix );

putenv("LC_ALL=". $lang );
bindtextdomain('messages', ROOT .'lang/');

我的路径是 /var/www/rssbot.dk/lang/。我应该正确设置 chmod,还是......?

I have a website. I'm trying to get gettext to work so that my English, Sweden and Norway sites can come up. I can't get it to work. What have I done wrong?

This is my config code:

// define constants ( defualt - danish )
$lang = 'da_DA';
$lang_short = '';
$lang_prefix = 'da';

if ( isset( $_GET['lang'] ) )
{
    switch( $_GET['lang'] )
    {
        case 'en':
            $lang = 'en_EN';
            $lang_short = 'en/';
            $lang_prefix = 'en';
            break;
        case 'se':
            $lang = 'se_SE';
            $lang_short = 'se/';
            $lang_prefix = 'se';
            break;
        case 'no':
            $lang = 'no_NO';
            $lang_short = 'no/';
            $lang_prefix = 'no';
            break;
    }
}
define( 'LANG', $lang_short );
define( 'LANG_PREFIX', $lang_prefix );

putenv("LC_ALL=". $lang );
bindtextdomain('messages', ROOT .'lang/');

And my path is /var/www/rssbot.dk/lang/. Should I make chmod right, or...?

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

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

发布评论

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

评论(2

十二 2024-09-10 06:04:56

我发现某些 gettext 安装需要为您要使用的每个区域设置运行 locale-gen 。我发现对于 Ubuntu 尤其如此。运行 locale-gen 后,您可能需要重新启动 PHP (apache)。

sudo locale-gen se_SE
sudo locale-gen no_NO

我有一个 测试设置(带有工作区域设置文件),可以确定是否你得到的文本设置正在工作。

<?php

//Depending on your OS, putenv/setlocale/both will set your language.
putenv('LC_ALL=es_MX');
setlocale(LC_ALL, 'es_MX');

bindtextdomain( "su", "./locale" ); //set the locale folder for a textdomain
bind_textdomain_codeset( "su", "UTF-8" ); //set the codeset for a textdomain
textdomain( "su" ); //choose a textdomain

if( gettext("Hello World!") === "Hola a todos!" ) {
    print "We translated it correctly";
} else {
    print "Gettext setup isn't working";
}

?>

I've found that some gettext installs need to have locale-gen run for each locale you want to use. I found this to be true for Ubuntu in particular. You might need to restart PHP (apache) after running locale-gen.

sudo locale-gen se_SE
sudo locale-gen no_NO

I've have a test setup (with working locale files) that can determine if you gettext setup is working.

<?php

//Depending on your OS, putenv/setlocale/both will set your language.
putenv('LC_ALL=es_MX');
setlocale(LC_ALL, 'es_MX');

bindtextdomain( "su", "./locale" ); //set the locale folder for a textdomain
bind_textdomain_codeset( "su", "UTF-8" ); //set the codeset for a textdomain
textdomain( "su" ); //choose a textdomain

if( gettext("Hello World!") === "Hola a todos!" ) {
    print "We translated it correctly";
} else {
    print "Gettext setup isn't working";
}

?>
沧笙踏歌 2024-09-10 06:04:56

有一些事情可能会出错。

1- 要适应大多数系统,您需要以下行:

<?php 
$newlocale = setlocale(LC_MESSAGES, "sv_SE"); 
putenv("LANG=$newlocale");
?>

2- 在 Linux 上,当使用 setlocale 与 LC_ALL 而不是 LC_MESSAGES 时,您需要将区域设置安装在服务器!

可以使用这样的命令进行安装(对于 Ubuntu)

aptitude install language-pack-sv

或者只是使用这样的命令重新配置

sudo locale-gen sv_SE

3- 指定 .mo 文件的名称和区域设置目录

<?php
// Use default.mo located at ./locale/LC_MESSAGES/default.mo
bindtextdomain( "domain", "./locale" );
?>

4- 在使用 gettext() 或 _() 时混合单引号和双引号时,您将需要使用两个绑定文本域!

<?php
// Double quote _("Hello world") is matched
bindtextdomain( "domain", "./locale" );
// Single quote _('Hello world') is matched
bindtextdomain( 'domain', "./locale" );
?>

5- 编码在很多地方可能是一个问题。如果您的 .mo 文件与 PHP 脚本的编码不同(例如 utf-8),则它可能不匹配!

There are a few things that might go wrong.

1- To accomodate most systems, you need the following lines:

<?php 
$newlocale = setlocale(LC_MESSAGES, "sv_SE"); 
putenv("LANG=$newlocale");
?>

2- On Linux, when using setlocale with LC_ALL instead of LC_MESSAGES, you will need to have the locale installed on the server!

It can be installed with a command like this one (for Ubuntu)

aptitude install language-pack-sv

Or just re-configured with a command like this one

sudo locale-gen sv_SE

3- Specify the name of .mo files and locale directory

<?php
// Use default.mo located at ./locale/LC_MESSAGES/default.mo
bindtextdomain( "domain", "./locale" );
?>

4- When mixing single and double quotes while using gettext() or _(), your will need to use two bindtextdomain!

<?php
// Double quote _("Hello world") is matched
bindtextdomain( "domain", "./locale" );
// Single quote _('Hello world') is matched
bindtextdomain( 'domain', "./locale" );
?>

5- Encoding might be an issue in many places. If your .mo file is not in the same encoding (utf-8 for example) than your PHP script, it might not match!

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