本文介绍了在PHP中保留与语言环境相关的字符串资源的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,您正在构建一个多语言的Web应用程序,在该应用程序中,所有界面文本都应移至与语言相关的资源,并在需要时进行加载.字符串资源可能非常庞大:假设您翻译了数千个字符串.在窗口环境(Windows,OS X,X11)中,通常具有由OS或某些API提供的用于执行此操作的机制,它们通常称为字符串资源.那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?

不过请记住,这里必须认真考虑性能,因为PHP会根据每个用户请求编译并执行所有模块.

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

我可以想到几种可行的方法.但是首先,我将有一个全局变量$ LANGUAGE,可以将其设置为'en','de','fr'等.我将使用此变量在每个请求中包括特定于语言的模块

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)在每个语言模块中将所有字符串定义为全局变量,例如

(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)相同,但定义为一个巨大的数组,例如

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

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

可读性差,主代码中的可用性降低(涉及更多键入),代码也更加混乱.这样会比较慢,因为这些不是简单的分配而是assoc.数组分配:与(1)相比,此处将有更多要对VM执行的指令.

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+:定义为常量,可能在类或名称空间中

(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!
    ...
}

...并将它们用作str :: signin等.很好,尽管其中也有一些小缺点,但我还是最喜欢的:仅PHP 5.3+.不能使用表达式,只能使用单个值(在您的情况下可能合适,也可能不合适);不能在$扩展中使用双引号的字符串(或者可以吗?).

... 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)数据库:将所有内容放入表中并通过某个ID(例如str_get(STR_SIGNIN).丑陋,缓慢,需要将代码中的ID与数据库ID进行同步,但是,当您所有页面需要的只是几个字符串时,就不需要加载所有内容.老实说不能说这是否是一个好的解决方案.

(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!

推荐答案

Zend Framework具有一个名为Zend_Translate的组件,该组件非常有用,它们的手册页上写得很好,即使您决定不使用ZF组件,也可以使用不同的方式来存储字符串.

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是性能最高和最佳的解决方案.如果您正在与翻译公司合作,他们很可能希望使用CSV并来回发送.

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.

这篇关于在PHP中保留与语言环境相关的字符串资源的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:23
查看更多