How to load language with gettext in PHP?

淺唱寂寞╮ 提交于 2019-11-30 10:03:21
JasonDavis

I finally got it to work, I am posting here so maybe it can help someone else, it is somewhat confusing the way it is documented I think.

I will use FR for french for this demo

$locale = "fr_fr";
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("default", "/includes/locale");
textdomain("default");

In the bindtextdomain function above, the first param is the name of the .po language file minus the extension. The second param is the path to the folder, this is where is gets a little tricky/undocumented... In the folder you set here you have to create a folder for each language with it's 2 digit country code, then inside this country code folder I had to add this folder named LC_MESSAGES, inside LC_MESSAGES is where your default.po file goes.

This was the only way I could get it to work, kind of confusing since the path to the lang folder does not mention anything about the bold part here:

langfolder/fr_FR/LC_MESSAGES/default.po

It is still really strange though, default.po works for me but if I change it to something else in the code above and then change the filename of the .po and .mo files to match it will no longer work, only works with default name for me, I am starting to think maybe gettext does some sort of behind the scenes caching

I suspect it's your path.

Is "/includes/locale" the correct directory on your host? Or is that relative to something? Note: you need the full pathname, not relative to your home directory or the install directory. so that might be something like:

  • C:/xampp/htdocs/include/locale
  • /var/www/include/locale
  • /home/username/include/locale
  • etc

What is the full directory where it's found?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!