开发者

How to develop a webapp system which will be easy to localize?

开发者 https://www.devze.com 2023-04-12 03:55 出处:网络
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 l

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 wil开发者_Python百科l appreciate it.


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.


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:

  • en-US/ - Contains the translations for users from the USA.
  • en-GB/ - Contains the translations for users from Great Britain.
  • en/ - Contains fallback, generic english translations.

Then I get the locale this way:

<?php
$default_locale = 'en';

$parts     = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$sub_parts = explode('-', $parts[0]);

if (is_dir("languages/{$parts[0]}")) {
    $locale = $parts[0];
}
elseif (is_dir("languages/{$sub_parts[0]}")) {
    $locale = $sub_parts[0];
}
else {
    $locale = $default_locale;
}

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.


The usual way to do this in PHP is by storing all your strings in a PHP array, inside a PHP file.


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 uses gettext library, may be this would be useful to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号