Changes between Version 4 and Version 5 of Implementation_of_Internationalization_and_Localization


Ignore:
Timestamp:
Apr 13, 2014, 9:05:18 PM (10 years ago)
Author:
Adrián Chaves
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Implementation_of_Internationalization_and_Localization

    v4 v5  
    1 = Message Extraction System =
    2 TODO
     1This topic explains the implementation details of internationalization and localization in the game. For documentation on how to use those features to either internationalize new game content or localize the game, see [wiki:Internationalization] and [wiki:Localization].
    32
     3= The Localization Singleton =
     4
     5The heart of the internationalization and localization implementation is at `source/i18n/L10n.cpp`. The `L10n` class implemented in this file works as a [http://en.wikipedia.org/wiki/Singleton_pattern singleton], and provides many functions that can be used from anywhere in the engine C++ codebase. You can get an instance of the singleton including the `i18n/L10n.h` header file and calling `L10n::instance()`.
     6
     7== Initialization ==
     8
     9When you call the singleton for the first time, its initialization:
     10
     11    1. Creates a list of available locales based on the filenames of the PO files in the `l10n` virtual folder.
     12
     13        The `l10n` virtual folder holds the combination of the files in the `binaries/data/l10n` folder, which contains the engine PO files, and the `l10n` folder of each enabled mod. See `source/ps/GameSetup/GameSetup.cpp`.
     14
     15        You can obtain the list of available locales calculated here at any later time calling `L10n::instance().getSupportedLocaleCodes()`. This method returns an array of strings with the locale codes found here. `L10n::instance().getSupportedLocaleDisplayNames()` returns a similar vector, this one with display names instead of codes, where the name of each locale is in the language of the locale itself. The special locale "Long Strings", of code "long", is only returned if yours is a development copy (if the `config/dev.cfg` file exists in the virtual filesystem).
     16
     17    2. Determines the current locale, first looking at the configuration value `locale` and, if undefined, looking at the system locale using [http://www.icu-project.org/apiref/icu4c/classicu_1_1Locale.html#a020c6966493a8f00572616b64b5527c3 icu::Locale::getDefault()].
     18
     19        Once the locale is defined, it loads the dictionary of the target locale if it is available. The dictionary is a structure from the tinygettext library that holds the list of English strings linked to translated strings (including context and plural considerations).
     20
     21        You can change the locale later using `L10n::instance().setLocale()`. You can pass this method either a locale code as a string, such as "en_GB", or an instance of [http://www.icu-project.org/apiref/icu4c/classicu_1_1Locale.html icu::Locale]. The method reloads the dictionary if required.
     22
     23== Localization Functions ==
     24
     25The `L10n` singleton provides many localization functions that you can use from C++, some of which are also published to be used in JavaScript.
     26
     27Translation functions ([wiki:Internationalization] explains how to use them):
     28{{{
     29std::string translate(const std::string& sourceString);
     30std::string translateWithContext(const std::string& context, const std::string& sourceString);
     31std::string translatePlural(const std::string& singularSourceString, const std::string& pluralSourceString, int number);
     32std::string translatePluralWithContext(const std::string& context, const std::string& singularSourceString, const std::string& pluralSourceString, int number);
     33std::string translateLines(const std::string& sourceString);
     34}}}
     35
     36Date and time functions:
     37{{{
     38// parseDateTime() and localizeDateTime() are used to convert dates from strings
     39// into icu::UDate and the other way around. These are used in
     40// “gui/scripting/ScriptFunctions.cpp”.
     41UDate parseDateTime(const std::string& dateTimeString, const std::string& dateTimeFormat, const Locale& locale);
     42std::string localizeDateTime(const UDate& dateTime, DateTimeType type, DateFormat::EStyle style);
     43
     44// Converts a string from UNIX timestamp (in milliseconds, not seconds) into a
     45// string with the specified format. See the ‘Internationalization’ page in the
     46// documentation for more information.
     47std::string formatMillisecondsIntoDateString(int milliseconds, const std::string& formatString);
     48}}}
     49
     50Number functions:
     51{{{
     52// Returns the specified floating-point number as a string using the current
     53// locale.
     54std::string formatDecimalNumberIntoString(double number);
     55}}}
     56
     57Path functions:
     58{{{
     59// Returns the localized version of the specified path if there is one.
     60// Otherwise, it returns sourcePath.
     61// This is used for image localization (see below).
     62VfsPath localizePath(VfsPath sourcePath);
     63}}}
     64
     65== GUI ==
     66
     67=== Text ===
     68
     69TODO:
     70* source/gui/CGUI.cpp
     71
     72=== Images ===
     73
     74TODO:
     75* source/gui/GUIRenderer.cpp
     76
     77== Functions Published for JavaScript ==
     78
     79TODO:
     80* source/gui/scripting/ScriptFunctions.cpp
     81
     82== Third-Party Libraries ==
     83
     84TODO:
     85* tinygettext (third_party/tinygettext)
     86* ICU
     87
     88
     89== Message Extraction System ==
     90
     91TODO:
     92* source/tools/i18n/updateTemplates.py
     93* <mod>/l10n/messages.json
     94* source/tools/i18n/potter/
     95
     96== Long Strings Locale ==
     97
     98TODO:
     99* source/tools/i18n/generateLongStringTranslations.py
     100
     101== Transifex Integration ==
     102
     103TODO:
     104* .tx/config
     105* source/tools/i18n/pullTranslations.py
     106* source/tools/i18n/tx
     107* source/tools/i18n/txclib/
     108
     109= JavaScript =
     110
     111The following sections describe some implementation details that are specific to the JavaScript side.
     112
     113== JavaScript Translation Cache System ==
     114
     115TODO:
     116* l10n.js (only cache stuff)
     117
     118== Object Translation Helper Function ==
     119
     120TODO:
     121* l10n.js (translateObjectKeys)
     122
     123== String Formatting Function ==
     124
     125TODO:
     126* sprintf.js
     127
     128= Public Mod =
     129
     130== Language Selection Menu ==
     131
     132TODO:
     133* gui/page_locale.xml
     134* gui/locale/*
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144TODO:
    4145= File Tree =
    5146== binaries/data/mods/public/ ==
     
    15156translateObjectKeys(object, keys);
    16157}}}
    17 
    18158The first four functions are simply wrappers for the engine internationalization functions (such as `Engine.translate`). These global functions use caching to reduce the number of calls to the engine functions, because calls to engine functions require string conversions that are far from cheap.
    19159
     
    21161
    22162==== sprintf.js ====
    23 
    24 Implements the `sprintf()` function, used for [wiki:Internationalization#UsingStringFormattingInsteadofConcatenatingStringsinJavaScript string formatting].
     163Implements the `sprintf()` function, used for [wiki:Internationalization#UsingStringFormattingInsteadofConcatenatingStringsinJavaScript string formatting].
    25164
    26165=== gui/ ===