编程多语言PHP应用程序 [英] Programming Multi-Language PHP applications

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

问题描述

我正在开发一个PHP应用程序,我想知道在其他国家/地区为用户提供多语言支持的最佳方法。



我精通使用PHP,但从来没有开发任何支持其他语言的东西。



我正在考虑将语言放入带有常量的PHP文件中,例如:



en.php可以包含:

  define('HZ_DB_CONN_ERR','有一个错误连接到数据库。 

和fr.php可能包含:

  define('HZ_DB_CONN_ERR','无论法语为上述...'); 

然后我可以调用函数并自动传递正确的语言。

  hz_die('HZ_DB_CONN_ERR',$ this); 

这是一个很好的方式吗?



- morristhebear。

解决方案

很奇怪人们似乎忽视了明显的解决方案。您的具体地区特定文件的想法很好。有en.php:

  define('LOGIN_INCORRECT','你的登录信息不正确'); 
...

然后,假设你有一个全局配置/常量文件'd建议有很多原因),有这样的代码:

  if($ language =='en'){
reqire_once'en.php';
} else if($ language =='de'){
require_once'de.php';
}

您可以定义数字和货币显示,比较/排序(即德语,法语和英语都有不同的排序规则方法)等。



人们经常忘记PHP是一种动态语言,所以这样的事情:

  if($ language =='en'){
function cmp($ a,$ b){...}
} else if($ language =='de'){
function cmp($ a,$ b){...}
}

其实是完全合法的。使用它们。


I'm developing a PHP application and I'm wondering about the best way to include multi-language support for users in other countries.

I'm proficient with PHP but have never developed anything with support for other languages.

I was thinking of putting the language into a PHP file with constants, example:

en.php could contain:

define('HZ_DB_CONN_ERR', 'There was an error connecting to the database.');

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

Is this a good way of going about it?

-- morristhebear.

解决方案

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.

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.

这篇关于编程多语言PHP应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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