简体   繁体   中英

Localization via database or flat file?

For multi-language PHP application assume labels/phrases will be translated into multiple languages. 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.)

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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