开发多语言系统
这更多的是一个分析问题。
我需要知道如何最好地制作一个多语言系统,也就是用户可以更改语言的系统。该语言将存储在 cookie 或数据库中。
我过去曾为每种语言使用过不同的文件,例如:
nl.php
$lang['hi'] = 'Hoi';
$lang['howareyou'] = 'Hoe gaat het?';
en.php
$lang['hi'] = 'Hi'];
$lang['howareyou'] = 'How are you?';
index.php
include($language . '.php');
你看,这个系统既低效又不安全。有更好的方法吗? 我现在可以想到几种方法来做到这一点,但我真的不知道哪种方法最好。
谁能帮我解决这个问题吗?请不要只说“这样做!”,还要告诉我为什么这是一个好方法。
This is more of an analytical question.
I need to know how best to make a multilingual system, a.k.a. a system where the user can change the language. The language will be stored in a cookie or a database.
I've worked in the past with different files for each language, for example:
nl.php
$lang['hi'] = 'Hoi';
$lang['howareyou'] = 'Hoe gaat het?';
en.php
$lang['hi'] = 'Hi'];
$lang['howareyou'] = 'How are you?';
index.php
include($language . '.php');
As you can see, this system is both inefficient and insecure. Is there a better way to do it?
I can think of a few ways to do this this instant, but I don't really know which one would be good.
Can anyone help me with this? Please don't just say "Do it like this!", also tell me why it is a good way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果您不需要提供通过 Web 界面更改本地化文本的功能,您可以这样做:
include/locales.php
index.php
这是最快的方法,因为用哈希解析单个包含文件是非常快的操作。
如果您需要提供通过 Web 界面修改本地化字符串的能力,那么您将需要将本地化字符串存储在数据库中,并在每次显示页面时从那里读取它们......这种方法要慢得多。
Well, if you don't need to provide ability to change localization texts via web interface, you can just do it like this:
include/locales.php
index.php
This is the fastest approach, because parsing single include file with hash is very fast operation.
If you need to provide ability to modify localization strings via web interface, then you will need to store localization strings in DB and read em from there each time you show a page... This approach is way more slow.
首先,您不需要在文件中包含更多语言。将每种语言保存在不同的文件中。
不要忘记您可以在语言文件中保留其他设置,例如:日期格式、数字格式等/
First of all you don't need to have more language in a file. Keep each language in distinct file.
Don't forget you may keep and other setting in language files like : date format, number formatting,etc/