Localizing a website
Which should you rather use for a medium-sized web site (I don't know the exact number of pages, words, and so on yet):
A) PHP files with static resources:
en.php
$lang_welcome = 'Welcome';
de.php
$lang_welcome = 'Willkommen';
B) MySQL table containing translations:
+------+-----------+---------+-------------+
| lang | path | term | translation |
+------+-----------+---------+-------------+
| en | index.php | welcome | Welcome |
+------+-----------+---------+-------------+
| de | index.php | welcome | Willkommen |
+------+-------开发者_运维问答----+---------+-------------+
| en | | cancel | Cancel |
+------+-----------+---------+-------------+
| de | | cancel | Abbrechen |
+------+-----------+---------+-------------+
(The only solution I could think of off the top of my head right now.)
C.) Some other solution.
Text translation tables in the database are universally a horrible idea.
For the file translation approach, multiple variations exist. For a very tiny application, a mnemonic id->text approach is ok (typically constants or text arrays). You should however prefer gettext, if that's available and feasible. (There are emulations as native PHP script.)
The data files usually take the form:
"Welcome at" => "Willkommen bei",
Making a much more readable and maintainable code which doesn't rely on abbreviations or shortened array indexes/constants/variable names:
print _("Welcome at");
I think php files is more standard. Your text is not going to change. DB's aren't really the best choice since it's read-only data.
Use Gettext for static content, and your database schema for dynamic content.
We use flat php files with key names including the filename (topic/section) and an identifier
$lang['nav_home_link'] = 'Home';
and then a folder structure that is marked by language code
language |
|
| - en
| | - nav.php
| - ja
| | - nav.php
Of course we use our own tool to manage the files and get translations done for each of the languages we support. Check it out, might be useful to you as well.
myGengo's String
精彩评论