问题描述
我想在PHP中开发一个多语言页面,例如英语/德语/日语..所以当我点击德语时,页面语言会变成德语,然后我点击英语,它会变成英语。请指导我这样做。在此先感谢...。奇怪的是,许多PHP开发人员似乎认为所有自然语言都具有相同的语法。制作一个没有参数的翻译框架是不可能的。尽管字符串 $ lang ['welcomeTo'] $ sitename
可能对大多数语言(欢迎使用stackoverflow)都能正确转换,但它在土耳其文中失败,例如它应该是 $ sitename $ lang ['welcomeTo']
(stackoverflow'a hos geldiniz)或德语在任何一种方式将是可能的(Willkommen auf stackoverflow / StackoverflowheißtSie willkommen)。当你尝试用多个变量翻译句子时,情况会变得更糟。
唯一的选择是使用参数化的翻译(比如:functions)
pre $ 语言:: getCurrentLanguage() - > translate('welcomeTo',array('sitename'=> $ sitename));
翻译文件为英文(configfile-style):
welcomeTo =欢迎来到$ sitename
#土耳其文翻译文件:
welcomeTo = $ sitename'a hos geldiniz
#德语翻译文件:
welcomeTo = willkommen auf $ sitename
#或
welcomeTo = $ sitenameheißtsie willkommen
简而言之:如果您想要真正的i18n,请使用函数来检索您的翻译,而不是变量。您也可以查看php的扩展。
I would like to develop a multilanguage page in PHP, for exemple english/german/japanese.. so when i click on german the page language will change to german, then i click english it change into english. Please guide me to do this . Thanks in advance....
Strangely, so many php developers seem to think that all natural languages have the same grammar. Making a translation framework without parameters is simply not possible. While the string $lang['welcomeTo'] $sitename
might translate correctly for most languages (Welcome to stackoverflow), it fails for turkish for example where it should be $sitename $lang['welcomeTo']
(stackoverflow'a hos geldiniz) or for german where either way would be possible (Willkommen auf stackoverflow/Stackoverflow heißt Sie willkommen). And this gets worse when you try to translate sentences with multiple variables.
The one and only option is to use parameterized translations (say: functions)
Language::getCurrentLanguage()->translate('welcomeTo', array('sitename' => $sitename));
# Translation file for english (configfile-style):
welcomeTo=Welcome to $sitename
# Translation file for turkish:
welcomeTo=$sitename'a hos geldiniz
# Translation file for german:
welcomeTo=Willkommen auf $sitename
# Or
welcomeTo=$sitename heißt sie willkommen
In short: If you want real i18n, use functions to retrieve your translations, not variables. You might want to look into the intl extension of php, too.
这篇关于在PHP中创建一个包含多语言的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!