如何开发一个易于本地化的webapp系统?
我正在构建一些网络应用程序,我正在考虑为其添加一些本地化,以便人们可以轻松地在语言之间切换,也许在未来 - 能够为应用程序添加他们自己的本地化,就像 Facebook 的一样语言或用户声音。
我知道我应该有一个模板,例如:
<element>[string1]</element>
基本上, [string1] 将被替换为正确的字符串,但是我将它存储在哪里?数据库?静态文件?每次从数据库获取字符串数据不是“昂贵”吗?我该如何处理它?使用输出缓冲区,或者是否有一些我不知道的更好的方法。
如果有人能以正确的思维方式指导我,我将不胜感激。
I am in the midst of building some web-app, and I am thinking of adding some localization to it, so people could switch easily between languages, and perhaps in the future - be able to add their own localization for the app, like Facebook's languages or uservoice.
I get it that I should have a templates, something like:
<element>[string1]</element>
and basically, that [string1] will be replaces with the correct string, but where do I store it? database? static file? isn't it "expensive" to get the string's data every time from the database? and how do I process it? using output buffer, or is there some better method that I am not aware of.
If anyone could direct me in the correct way of thinking, I will appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有许多可用的开源翻译套件系统可以让您更好地理解该做和不该做的事情(例如potttle,poedit,virtaal)。
如果您决定走自己的路 - 我建议将所有翻译键和值存储在数据库中。当涉及到编辑、搜索、获取缺失的翻译等时,您的情况会更好。
您可以每 24 小时或在每个新/编辑的翻译之后生成完整的翻译列表,然后将其存储在 JSON 或 PHP 序列化的静态文件中格式。这将使您免于 MYSQL 选择。
我建议的另一件事是选择“密钥+密钥翻译”的格式。应在第一次调用时将 KEYS 插入数据库以获取翻译。这将导致您看到“缺少翻译”,这是使用第三方或您的网站访问者进行本地化翻译的好方法。
There is a number of open-source translation kit systems available which should give you a better understaning of the DOs and DON'Ts (ex. pottle, poedit, virtaal).
If you decide to go your way - I'd suggest storing all the translation keys and values on the database. You'll be better off when it comes to editing, searching, getting missing translations etc.
You can generate full list of translations every 24 hours or after each new/edited translation, which would then be stored on static file in JSON or PHP serialised format. This will save you from MYSQL selects.
Another thing I would suggest is choosing a format of KEYS + KEY TRANSLATIONS. KEYS should be inserted to database on the first call to get the translation. This will lead you to "Missing translations" which is a good way of using 3rd parties or your site visitors to do the localised translations.
当我必须本地化我的应用程序时,我通常会创建一个 translations/ 目录,然后为每种区域性创建一个子目录。例如,我可以在其中包含以下文件夹:
然后我以这种方式获取区域设置:
请注意,您应该对 $_SERVER['HTTP_ACCEPT_LANGUAGE'] 变量执行检查,以避免 LFI(本地文件包含)尝试。只需检查语言环境是否包含在 languages/ 目录中即可。
When I have to localize my application, I usually create a translations/ directory, and then a subdirectory for each culture. For example, I can have the following folders inside:
Then I get the locale this way:
Note that you should perform a check on the $_SERVER['HTTP_ACCEPT_LANGUAGE'] variable to avoid LFI (Local File Inclusion) attempts. Just check the locale is contained in the languages/ directory.
在 PHP 中执行此操作的常用方法是将所有字符串存储在 PHP 文件内的 PHP 数组中。
The usual way to do this in PHP is by storing all your strings in a PHP array, inside a PHP file.
我建议你看一下 Gettext for PHP。 gettext 函数实现了 NLS(本机语言支持)API,可用于国际化 PHP 应用程序。
Gettext 是 Linux/Unix 系统本地化的事实上的标准,并且也广泛用于 PHP 应用程序(例如,Wordpress 就使用它)。如需更深入的解释、示例等,请查看上面 PHP 手册中的 Gettext 部分,或者您可以访问 原始 gettext 手册,对 gettext 工具、绑定等进行更广泛的讨论。
I suggest you take a look at Gettext for PHP. The gettext functions implement an NLS (Native Language Support) API which can be used to internationalize your PHP applications.
Gettext is a de-facto standard for localization on linux/unix systems, and is widely used in PHP applications too (Wordpress, for example, uses it). For more in-depth explanation, examples, etc., take a look at the Gettext section in the PHP manual above, or you can visit the original gettext manual for a broader discussion of gettext tools, bindings, and more.
Wordpress 使用 gettext 库,可能这对您有用。
Wordpress uses gettext library, may be this would be useful to you.