简体   繁体   English

通过数据库或平面文件进行本地化?

[英]Localization via database or flat file?

For multi-language PHP application assume labels/phrases will be translated into multiple languages. 对于多语言PHP应用程序,假设标签/短语将被翻译成多种语言。 Those labels can be placed in language specific files (eg one file per language) or can be loaded into a database, so that the app can access them when needed. 这些标签可以放在特定语言的文件中(例如,每种语言一个文件),也可以加载到数据库中,以便应用程序可以在需要时访问它们。

The question is, from the performance standpoint, what is the better approach? 问题是,从绩效的角度来看,更好的方法是什么?

To me it appears that if the labels are in the database it is less data to load (I can only request labels needed for a single page) and can more easily built admin tools for translations. 对我来说,如果标签在数据库中,则加载的数据较少(我只能请求单个页面所需的标签),并且可以更轻松地构建用于翻译的管理工具。 However, it appears that many apps and frameworks out there use flat files for that purpose (eg phpMyAdmin, CakePHP, etc.) 但是,似乎很多应用程序和框架都使用平面文件(例如phpMyAdmin,CakePHP等)

Loading data from a file is way faster than loading from a database. 从文件加载数据比从数据库加载的方式更快。 You just shave many layers of abstraction between your data and your application. 您只需在数据和应用程序之间划分多层抽象。 If you profile your application for performance, you will see that database access is usually one of the slowest operation. 如果您对应用程序的性能进行概要分析,您将看到数据库访问通常是最慢的操作之一。

If you don't want to load all your localization string every time you display something, you always have the options to put them in differents files. 如果您不想在每次显示内容时加载所有本地化字符串,则始终可以选择将它们放在不同的文件中。 For example, a "global" file for string displayed everywhere, and a localization file specific to the page/section your on. 例如,在任何地方显示字符串的“全局”文件,以及特定于您的页面/部分的本地化文件。

That said, as with anything concerning performance, don't take my word for it, but mesure it yourself. 也就是说,就像有关表演的任何事情一样,不要相信我的话,但要自己弄清楚。 Maybe in your particuliar context, with your particuliar application, a database will do just fine. 也许在您特定的环境中,通过您的特定应用程序,数据库可以正常运行。

Database allows you to select only the record you're interested in... 数据库允许您只选择您感兴趣的记录...
But : 但是:

  • You need to insert /update the data to the database each time you want to change a translation -- it's harder than with a flat-file. 每次要更改转换时,都需要将数据插入/更新到数据库 - 这比使用平面文件更难。
  • YOu'll need a lot of selects, to build a fully-translated page -- which is not quite good for performances (and means you'll have to put some caching mecanism in place) 你需要很多选择,建立一个完全翻译的页面 - 这对于表演来说并不是很好(并且意味着你必须将一些缓存机制放在​​适当位置)


Out of curiosity, why not take a look at something else -- something which goal is translations ? 出于好奇,为什么不看看别的东西 - 目标是翻译?

Here, I'm thinking about Gettext , which is widely used -- and there is a Gettext extension for PHP (I should add there are other classes available -- in Zend Framework, for example) . 在这里,我想Gettext的 ,它被广泛应用于-并有一个Gettext的扩展库(我要补充还有其他的课程-在Zend框架,例如)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM