如何在PHP中使用gettext加载语言? [英] How to load language with gettext in PHP?

查看:61
本文介绍了如何在PHP中使用gettext加载语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在poedit中创建了一个语言文件以使用PHP gettext函数,现在在我要对其进行测试的页面上,我找不到它来查找/加载我创建的.po或.mo文件,有人看到我在做什么错吗?

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

即使我尝试加载默认的英语lang文件,我也更改了文件中的某些文本以确保它是从语言文件中读取的,并且当前它仅显示默认文本而不进行翻译,我认为我可能走错了路

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

推荐答案

我终于开始使用它了,我将其发布在这里,以便它可以对其他人有所帮助,这使我认为它的记录方式有些混乱.

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.

在此演示中,我将使用FR表示法语

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");

在上面的 bindtextdomain函数中,第一个参数是.po语言文件的名称减去扩展名.第二个参数是文件夹的路径,在这里有点棘手/未记录...在这里设置的文件夹中,您必须为每种语言创建一个带有2位数字的国家/地区代码的文件夹,然后在该国家/地区代码中文件夹我必须添加一个名为 LC_MESSAGES 的文件夹,在LC_MESSAGES内部是您的default.po文件所在的位置.

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.

这是我使其工作的唯一方法,这有点令人困惑,因为lang文件夹的路径在此处未提及任何粗体部分:

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

langfolder/fr_FR/LC_MESSAGES/default.po

虽然仍然很奇怪,但是default.po对我有用,但是如果我在上面的代码中将其更改为其他内容,然后更改.po和.mo文件的文件名以匹配它,将不再起作用,仅为我使用default名称,我开始认为gettext可能在后台进行了某种缓存

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

这篇关于如何在PHP中使用gettext加载语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆