我正在尝试使用gettext()函数来检索存储在.mo和.po文件中的数据。我遇到的问题是gettext()似乎只返回我传递给它的任何值作为参数,似乎没有使用.mo文件。我在codeigniter控制器中调用该函数。
class HomeController extends CI_Controller
{
public function index()
{
global $LANG;
$language = 'en_US';
$lang_path = FCPATH.APPPATH.'language/locales';
putenv('LANG='.$language.'.UTF-8');
setlocale(LC_ALL, $language.'.UTF-8');
bindtextdomain('lang', $lang_path);
textdomain('lang');
$test = gettext('Simpletest');
echo View::load('layouts/home.html', array(
'language' => $LANG->getLanguage(),
'map' => $LANG->get('map'),
'global' => $LANG->get('global'),
'page' => $LANG->get('home'),
'test' => $test
));
}
}
我在'application'目录下有以下目录结构。
./language/locales/en_US/LC_MESSAGES/lang.mo
./language/locales/en_US/LC_MESSAGES/lang.po
./language/locales/fr_CA/LC_MESSAGES/lang.mo
./language/locales/fr_CA/LC_MESSAGES/lang.po
en_US中的lang.po文件如下所示:
msgid ""
msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
msgid "Simpletest"
msgstr "A small sentence in english"
正如我所说,无论我传递给gettext的'key',它只返回参数,即gettext('foo')返回'foo'。