douhao3562 2010-10-30 18:49
浏览 25
已采纳

在PHP中保留依赖于语言环境的字符串资源的最佳方法?

Let's say you are building a multilingual web application in which all interface text should be moved to language-dependent resources and loaded when needed. The string resource may be huge: say you have several thousands of strings translated. In windowed environments (Windows, OS X, X11) you usually have a mechanism provided by the OS or some API for doing just that and they are usually called string resources. What about PHP then?

Remember though, performance must be considered seriously here as PHP compiles and executes all your modules with each user request.

I can think of several possible ways of doing it. But first of all, I'm going to have a global variable $LANGUAGE which may be set to 'en', 'de', 'fr' etc. I'll use this variable to include a language-specific module with each request as

require_once "lang-$LANGUAGE.inc.php"

So some of the possible solutions include:

(1) Defining all strings as global vars in each language module, e.g.

$str_signin = 'Sign in';
$str_welcome_user = 'Welcome, %s'!;
...

Very simple, easy to read and relatively easy to work on for non-technical people (translators, that is). There is some global space pollution though which will slow down your global variable lookup a bit.

(2) Same but defined as one huge array, e.g.

$str['signin'] = 'Sign in';
$str['welcome_user'] = 'Welcome, %s'!;
...

Less readable, a bit less usable in your main code (more typing involved) also clutters your code a bit more. This would be slower because these are not simple assignments but assoc. array assignments: there will be more instructions to execute here for the VM compared to (1).

(3) PHP 5.3+: define as constants, possibly in a class or namespace

class str {
    const signin = 'Sign in';
    const welcome_user = 'Welcome, %s'!;
    const signin_to_a = self::signin . ' to area A'; // can't do this!
    ...
}

... and use them as str::signin etc. Nice, I like this most of all, although there are a few minor disadvantages as well: PHP 5.3+ only; can't use expressions, only single values (which may or may not be fine in your case); can't use in $-expansion in double-quoted strings (or can you?).

(4) Database: put everything into a table and retrieve by some ID, e.g. str_get(STR_SIGNIN). Ugly, slow, needs syncing of your ID's in the code with the DB ID's, however no need to load everything when all your page needs is just a few strings. Honestly can't say if this is a good solution or not.

Any other suggestions? Also, thoughts on these ones?

And please do keep in mind simplicity, elegancy and performance!

  • 写回答

4条回答 默认 最新

  • doujiang1939 2010-10-30 19:29
    关注

    Zend Framework has a component called Zend_Translate which is really useful and their manual page has a good write up on the different ways you can store strings, even if you decide not to use the ZF component.

    PHP is the most performant and the best solution if you're maintaining strings as a developer. If you're working with a translation company it's likely they'll expect to work with CSVs and send these back and forth.

    I don't know off the top of my head whether an array or constant based solution is better but my money is on the arrays. A quick benchmark will soon tell you.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。