問題描述
我正在開發(fā)一個 PHP 應(yīng)用程序,我想知道為其他國家/地區(qū)的用戶提供多語言支持的最佳方式.
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,但從未開發(fā)過支持其他語言的任何東西.
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...');
然后我可以調(diào)用一個函數(shù)并自動傳入正確的語言.
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.
-- morristhebear.
推薦答案
奇怪.人們似乎忽略了顯而易見的解決方案.您對特定于語言環(huán)境的文件的想法很好.有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.');
...
然后,假設(shè)您有一個全局配置/常量文件(出于多種原因,我建議這樣做),請使用如下代碼:
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';
}
您可以定義數(shù)字和貨幣顯示、比較/排序(即德語、法語和英語都有不同的整理方法)等功能.
You can define functions for number and currency display, comparison/sorting (ie German, French and English all have different collation methods), etc.
人們常常忘記 P??HP 是一種動態(tài)語言,所以像這樣:
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.
這篇關(guān)于編程多語言 PHP 應(yīng)用程序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!