douchan6512 2009-09-20 01:33
浏览 82
已采纳

如何在PHP中使用gettext加载语言?

I have created a language file in poedit for the use of the PHP gettext function, now on the page I am trying to test it on, I cannot get it to find/load the .po or .mo file I created, does anyone see what I am doing wrong?

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

//my .po ad .mo file is located here /includes/locale/en/default.mo

Even though I am trying to load the default english lang file, I have changed some text in the file to make sure it is reading from a language file and currently it is only showing the default text and not doing a translation, I think I have the wrong path possibly

  • 写回答

2条回答 默认 最新

  • dongxian7194 2009-09-20 01:36
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题