Инструменты пользователя

Инструменты сайта


limb2:en:i18n

I18N and L10N

To deal with different l10ns and i18n strings Limb makes use of 'locale' and 'strings' classes('LIMB_DIR/core/lib/locale/' directory).

  • locale class is used for date, currency and numeric data formatting. ini-files for different locale settings are stored in LIMB_DIR/core/locale directory
  • strings class is used for localized strings resolving. ini-files with localized strings could be found at:
    • PROJECT_DIR/core/strings
    • LIMB_DIR/core/strings

Locale settings

Limb uses two constants to deal with localization settings:

  • CONTENT_LOCALE_ID - current site object's locale
  • MANAGEMENT_LOCALE_ID - current user's preferred locale. It is used for localized management messages, validation errors, action names etc

Each Limb site object has its locale id (e.g 'en' or 'ru'). It is stored in the sys_site_object table. Every new object inherits from its parent locale id by default. At the moment we don't have a nice GUI for setting site object's locale id(this is planned).

It's the /core/filters/locale_definition_filter.class.php which is responsible for defining CONTENT_LOCALE_ID and MANAGEMENT_LOCALE_ID. It uses pretty much guessing and it will be simpler to show its code than trying to explain:

[...]
    if(!$node = map_request_to_node($request))
    {
      if(!defined('CONTENT_LOCALE_ID'))
        define('CONTENT_LOCALE_ID', DEFAULT_CONTENT_LOCALE_ID);
      if(!defined('MANAGEMENT_LOCALE_ID'))
        define('MANAGEMENT_LOCALE_ID', CONTENT_LOCALE_ID);
 
      $locale =& locale :: instance();
      $locale->setlocale();
 
      $filter_chain->next();
      return;
    }
 
    if(!defined('CONTENT_LOCALE_ID'))
    {
      if($object_locale_id = site_object :: get_locale_by_id($node['object_id']))
        define('CONTENT_LOCALE_ID', $object_locale_id);
      else
        define('CONTENT_LOCALE_ID', DEFAULT_CONTENT_LOCALE_ID);
    }
 
    if(!defined('MANAGEMENT_LOCALE_ID'))
    {
      $user = user :: instance();
      if($user_locale_id = $user->get_locale_id())
        define('MANAGEMENT_LOCALE_ID', $user_locale_id);
      else
        define('MANAGEMENT_LOCALE_ID', CONTENT_LOCALE_ID);
    }
[...]

User can set preferred MANAGEMENT_LOCALE_ID during logging into a site:

It is also possible to switch MANAGEMENT_LOCALE_ID using the upper-right form in the administrative part of the site:

The list of available locales is defined by global constant AVAILABLE_LOCALES in setup.php(or setup.override.php) in the PROJECT_DIR. The valid example is:

@define('AVAILABLE_LOCALES', 'en,ru');

What constants actually affect

Let's see how and where these locale constants are used:

  • MANAGEMENT_LOCALE_ID - default value used in strings :: get(…)
  • <metadata:CHARSET> renders page charset, uses MANAGEMENT_LOCALE_ID only
  • All form validation errors are rendered with MANAGEMENT_LOCALE_ID
  • Any form input tag (e.g. INPUT, BUTTON etc.) has locale_type, locale_value, locale_file attributes to set localized values, by default MANAGEMENT_LOCALE_ID
  • Tag <locale:STRING name='string_name' file='strings_file_name' locale_type='{management|content}'> by default MANAGEMENT_LOCALE_ID
  • CONTENT_LOCALE_ID - default value in locale :: instance();
  • Tag <locale:DATE_FORMAT hash_id='var_name' format='format_type' type='{stamp|string}' locale_type='{management|content}'> by default CONTENT_LOCALE_ID
  • Tag <locale:LOCALE name='locale_name' locale_type='{management|content}'> by default CONTENT_LOCALE_ID

L10N settings

You can find and l10n settings files in the 'LIMB_DIR/core/locale/' directory. There are 2 currently supported: en.ini, ru.ini. These files contain some l10n specific stuff: country name, language name, date format, week days, month names, page charset etc

I18N strings

For now we have localized strings for English and Russian languages. The common usage of the strings class is:

 echo strings :: get($string_name,[$file_name='common'],[$locale_id=null]);

if $locale_id is not specified current MANAGEMENT_LOCALE_ID is used $file_name is mapped to physical file by schema(order is preserved):

  • 'PROJECT_DIR/core/strings' . $file_name . '_' . $locale_id . '.ini'
  • 'LIMB_DIR/core/strings' . $file_name . '_' . $locale_id . '.ini'

If no file found the critical error is risen.

The common format of the localized ini file as follows:

[constants]
attribute1 = ...
attribute2 = ...
...

Localized ini files support simple inheritance with [extends] section: you have to supply 'filename' property in it to let strings class handle inheritance issues. Simple example, contents of ad_en.ini file:

[constants]
create_ad_block = Create ad block
ad_block_creation  = Ad block creation
ad_block_edition  = Ad block edition
edit_ad_block = Edit ad block

Обсуждение

Ваш комментарий. Вики-синтаксис разрешён:
   ____ ______   __ __  __  __   ___ 
  / __//_  __/  / //_/ / / / /  / _ \
 / _/   / /    / ,<   / /_/ /  / ___/
/___/  /_/    /_/|_|  \____/  /_/
 
limb2/en/i18n.txt · Последние изменения: 2010/11/10 10:02 (внешнее изменение)