Programming Multi-Language PHP applications(编写多语言 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.
这篇关于编写多语言 PHP 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:编写多语言 PHP 应用程序


基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01