PHP本地化 [英] PHP localization

查看:92
本文介绍了PHP本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个小型项目,该项目由后端的注册,登录,密码重置和用户管理组成.我必须创建用于不同语言的翻译文件,而不是使用诸如gettext之类的东西(我一无所知),我决定为每个语言文件使用一个静态数组来实现一个非常简单的方法,如下所示:

I am working on a small project that consists of registration, login, password reset and user management on the back-end. I have to create translation files for different languages and instead of using something like gettext (which I know nothing about), I decided to implement a very simple method using a static array for each language file like this:

function plLang($phrase) {
    $trimmed = trim($phrase);
    static $lang = array(
    /* -----------------------------------
    1. REGISTRATION HTML
    ----------------------------------- */
    'LNG_1'     => 'some text',
    'LNG_2'     => 'some other text',
    etc. ...
    );

    $returnedPhrase = (!array_key_exists($trimmed,$lang)) ? $trimmed : $lang[$trimmed];
    echo $returnedPhrase;
}

它运行良好,在此阶段非常快,但是我的标记现在到处都是php语言标记,我不确定我是否做出了正确的决定.我以前从未做过此事,所以我不知道自己期待什么.看来,当我完成所有工作后,此文件将长一英里.

It works fine, it is very fast at this stage but my markup now is littered with php language tags and I'm not sure I've made the right decision. I've never done this before so I have no idea what I'm looking forward to. It also seems that by the time I am all done, this file is going to be a mile long.

这是这样做的好方法吗?有什么更好的建议吗?

Is this a good way of doing this? Is there a better way you could suggest?

谢谢!

推荐答案

这是我在cms中正在做的事情:

this is what im doing in my cms:

    我开发的
  • 用于each插件/程序/实体(您为它命名),创建一个/translations文件夹.
  • 我把所有的翻译都放在了这里,例如 el.txt,de.txt,uk.txt 等.所有语言
  • 我将翻译数据存储在 JSON 中,因为它易于存储,易于阅读并且最容易发布给所有人.
  • 文件可以很容易地以UTF8编码在文件中,而不会与数据库打乱,从而可以在文件模式下读取它们. (只需JSON.parse他们)
  • 在安装此类插件时,我只是遍历所有翻译并将其放入数据库中,每种表行每种语言. (等a data column of TEXT datatype)
  • 对于每个页面渲染,我只需查询数据库一次以获取这一行所选语言,然后对整个结果调用json_decode()即可得到一次;然后将其放在 $ _ SESSION 中,以便下次为当前选择的语言获取快速翻译的字符串.
  • for each plugin/program/entity (you name it) i develop, i create a /translations folder.
  • i put there all my translations, named like el.txt, de.txt, uk.txt etc. all languages
  • i store the translation data in JSON, because its easy to store to, easy to read from and easiest for everyone to post theirs.
  • files can be easily UTF8 encoded in-file without messing with databases, making it possible to read them in file-mode. (just JSON.parse them)
  • on installation of such plugins, i just loop through all translations and put them in database, each language per table row. (etc. a data column of TEXT datatype)
  • for each page render i just query once the database for taking this row of selected language, and call json_decode() to the whole result to get it once; then put it in a $_SESSION so the next time to get flash-speed translated strings for current selected language.

整个开发过程我都想到了性能和兼容性.

the whole thing was developed having i mind both performance and compatibility.

在您的情况下,此类文件中的一行可能是:

in your case a row in such file could be like:

en.txt

{"id":"LNG_1","str":"My word"}

de.txt

{"id":"LNG_1","str":"Mein Wort"}

当前语言可以存储在会话中,例如 $ _ SESSION ["language"]; 并使用 以此为起点. 然后,您可以阅读以下翻译:

The current language could be stored in session like $_SESSION["language"]; and use that as starting point. Then, you can read translations like:

lang("LNG_1");

这篇关于PHP本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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