编写多语言 PHP 应用程序
我正在开发一个 PHP 应用程序,我想知道为其他国家/地区的用户提供多语言支持的最佳方式.
I'm developing a PHP application and I'm wondering about the best way to include multi-language support for users in other countries.
我精通 PHP,但从未开发过任何支持其他语言的东西.
I'm proficient with PHP but have never developed anything with support for other languages.
我正在考虑将语言放入带有常量的 PHP 文件中,例如:
I was thinking of putting the language into a PHP file with constants, example:
en.php 可能包含:
en.php could contain:
define('HZ_DB_CONN_ERR', 'There was an error connecting to the database.');
并且 fr.php 可能包含:
and fr.php could contain:
define('HZ_DB_CONN_ERR', 'whatever the french is for the above...');
然后我可以调用一个函数并自动传入正确的语言.
I could then call a function and automatically have the correct language passed in.
hz_die('HZ_DB_CONN_ERR', $this);
这是一个好方法吗?
-- 莫里斯熊.
推荐答案
奇怪.人们似乎忽略了明显的解决方案.您对特定于语言环境的文件的想法很好.有en.php:
Weird. People seem to be ignoring the obvious solution. Your idea of locale-specific files is fine. Have en.php:
define('LOGIN_INCORRECT','Your login details are incorrect.');
...
然后,假设您有一个全局配置/常量文件(我建议有很多原因),有这样的代码:
Then, assuming you have a global config/constants file (which I'd suggest for many reasons), have code like this:
if ($language == 'en') {
reqire_once 'en.php';
} else if ($language == 'de') {
require_once 'de.php';
}
您可以定义数字和货币显示、比较/排序(即德语、法语和英语都有不同的排序方法)等功能.
You can define functions for number and currency display, comparison/sorting (ie German, French and English all have different collation methods), etc.
人们经常忘记 PHP 是一种动态语言,所以像这样:
People often forget PHP is a dynamic language so things like this:
if ($language == 'en') {
function cmp($a, $b) { ... }
} else if ($language == 'de') {
function cmp($a, $b) { ... }
}
实际上是完全合法的.使用它们.
are in fact perfectly legal. Use them.
相关文章