Language localization in PHP

蓝咒 提交于 2020-03-05 07:19:04

问题


I need comments on my solution of a website localization system.

Table Content:

Field   Type    Null    Key Default Extra
id  int(11) NO  PRI NULL    auto_increment
Page    varchar(30) YES     NULL     
Locale  varchar(10) YES     NULL     
Data    longtext    YES     NULL

My thought is to store each page and each locale into the Content table. For example:

index.php en_GB {data}
index.php de_DE {data}

The Data field would consist of a json_encoded array in which the text of each of the page elements would be stored. For example:

$data['Headline1'] = 'The Headline';
$data['Welcome'] = 'The welcome text.';

I have yet to implement it, but I plan on using memcached to improve performance - and from the little I know this is the perfect kind of data to use it on since it is not prone to be updated very often.

Is this good or bad, should it all be scrapped? Comments please!


回答1:


Some notes:

  1. Don't store translations per page, this would cause alot of redundant data as some translations will occur om multiple pages.
  2. I advice against throwing all translations into one field in the database, add an entry for each translated sentence.
  3. Replace locale (varchar) with an unsigned tinyint and create an index on it, please the locales in an locale table.

Something along the likes of:

translationId | localeId | translationKey | translationText



回答2:


If you want to do localization in PHP use gettext and you will have time do implement other features too, instead of building your own localization system.

Do not worry about speed, php gettext extension runs faster than using strings arrays.



来源:https://stackoverflow.com/questions/6303857/language-localization-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!