简体   繁体   English

在 php 中为多语言站点使用 Gettext 不起作用

[英]Using Gettext for multilingual site in php doesn't work

I'm trying to make a multilingual site in PHP using gettext .我正在尝试使用gettext在 PHP 中创建一个多语言站点。

I'm using PHP7.1 (no way of upgrading), and have enabled gettext.so in my php.ini .我正在使用 PHP7.1(无法升级),并且在我的php.ini 中启用了gettext.so

See: http://corbeauperdu.ddns.net/phpinfo.php见: http : //corbeauperdu.ddns.net/phpinfo.php

I've read the documentation and followed examples, but for some reason, it won't translate at all to my French language.我已经阅读了文档并遵循了示例,但由于某种原因,它根本无法翻译成我的法语。

My PO file and test.php files are as follows:我的PO文件和test.php文件如下:

/prestadesk/include/locales/fr/LC_MESSAGES/prestadesk.po:
msgid ""
msgstr ""
"Language: fr\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

msgid "This page will show the dashboard"
msgstr "Cette page affichera le tableau de bord"


/prestadesk/templates/test.php:
<?php
$lang='fr';
$domain = 'prestadesk';
$codeset = 'UTF-8';
$locales_dir = '../include/locales'; // need to go up on directory from here to get into the include/locales

// here we define the global system locale given the found language
putenv('LANG='.$lang);

// this might be useful for date functions (LC_TIME) or money formatting (LC_MONETARY), for instance
setlocale(LC_ALL, $lang);

// this will make Gettext look for $locales_dir/<lang>/LC_MESSAGES/prestadesk.mo
bindtextdomain($domain, $locales_dir);

// indicates in what encoding the file should be read
bind_textdomain_codeset($domain, $codeset);

// here we indicate the default domain the gettext() calls will respond to
textdomain($domain);

// test translate
echo gettext("This page will show the dashboard");
?>

Well, I found the problem:嗯,我发现了问题:

The server the website resides on NEEDS to have the locale installed for the desired $lang !!!网站所在的服务器需要为所需的 $lang 安装语言环境!!!

On the server, check the installed locales with:在服务器上,使用以下命令检查已安装的语言环境:

locale -a

If your desired locale is not listed, you must edit /etc/locale.gen and uncomment yours.如果未列出您想要的语言环境,您必须编辑/etc/locale.gen并取消注释您的语言环境。 Example:例子:

fr_FR.UTF-8 UTF-8 
fr_FR ISO-8859-1
fr_FR@euro ISO-8859-15

Then, generate and install that locale:然后,生成并安装该语言环境:

sudo locale-gen

Once this is done, you'll be able to use gettext() properly.完成此操作后,您将能够正确使用 gettext()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM