Changes between Version 8 and Version 9 of Implementation_of_Internationalization_and_Localization
- Timestamp:
- Apr 19, 2014, 10:31:58 AM (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Implementation_of_Internationalization_and_Localization
v8 v9 4 4 5 5 = Internationalization = 6 7 6 == Third-Party Libraries == 8 9 7 The internationalization and localization implementation fo the game relies on two libraries: 10 8 11 * '''[http://site.icu-project.org/ ICU]''' is a common internationalization library that provides structures to handle locales, easily obtain the default system locale, handle date and number representations on each locale, or provide the localized names of locales to show them in a language combo box.9 * '''[http://site.icu-project.org/ ICU]''' is a common internationalization library that provides structures to handle locales, easily obtain the default system locale, handle date and number representations on each locale, or provide the localized names of locales to show them in a language combo box. 12 10 13 * '''[http://code.google.com/p/tinygettext tinygettext]''' is a small library that we forked. This library provides the structures and methods that we use to load PO files in memory.11 * '''[http://code.google.com/p/tinygettext tinygettext]''' is a small library that we forked. This library provides the structures and methods that we use to load PO files in memory. 14 12 15 13 == The Localization Singleton == 16 17 14 The heart of the internationalization and localization implementation is at `source/i18n/L10n.cpp`. 18 15 … … 22 19 23 20 === Initialization === 24 25 21 When you call the singleton for the first time, its initialization: 26 22 27 23 1. Creates a list of available locales based on the filenames of the PO files in the `l10n` virtual folder. 28 24 29 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`.25 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` . 30 26 31 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).27 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). 32 28 33 29 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()]. 34 30 35 31 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). 36 32 37 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.33 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. 38 34 39 35 === Localization Functions === 40 41 36 The `L10n` singleton provides many localization functions that you can use from C++, some of which are also published to be used in JavaScript. 42 37 43 38 Translation functions ([wiki:Internationalization] explains how to use them): 39 44 40 {{{ 45 41 std::string Translate(const std::string& sourceString); … … 49 45 std::string TranslateLines(const std::string& sourceString); 50 46 }}} 47 Date and time functions: 51 48 52 Date and time functions:53 49 {{{ 54 50 // parseDateTime() and localizeDateTime() are used to convert dates from strings … … 63 59 std::string FormatMillisecondsIntoDateString(int milliseconds, const std::string& formatString); 64 60 }}} 61 Number functions: 65 62 66 Number functions:67 63 {{{ 68 64 // Returns the specified floating-point number as a string using the current … … 70 66 std::string FormatDecimalNumberIntoString(double number); 71 67 }}} 68 Path functions: 72 69 73 Path functions:74 70 {{{ 75 71 // Returns the localized version of the specified path if there is one. … … 78 74 VfsPath LocalizePath(VfsPath sourcePath); 79 75 }}} 76 == GUI == 77 === Text === 78 The `source/gui/CGUI.cpp` file implements the parsing of the GUI XML localization elements: 80 79 81 == GUI == 82 83 === Text === 84 85 The `source/gui/CGUI.cpp` file implements the parsing of the GUI XML localization elements: 86 * `attribute` (along with `keep` and `translate`) 87 * `translatableAttribute`. 80 * `attribute` (along with `keep` and `translate`) 81 * `translatableAttribute`. 88 82 89 83 See [wiki:Internationalization#InternationalizingGUIFiles Internationalizing GUI Files] to see how to use them. 90 84 91 85 === Images === 92 93 86 The `source/gui/GUIRenderer.cpp` file, responsible for rendering images on the GUI, is configured to use `L10n.Instance().LocalizePath()`, which loads localized versions of images if available. 94 87 … … 96 89 97 90 == Functions Published for JavaScript == 98 99 91 '''TODO''': Link to svn.wildfiregames.com/docs when #67 gets committed. 100 92 101 The `source/i18n/scripting/JSI _L10n.cpp` file publishes some of the functions that the `L10n` singleton offers, as well as some custom internationalization functions based on the `L10n` functionality.93 The `source/i18n/scripting/JSInterface_L10n.cpp` file publishes some of the functions that the `L10n` singleton offers, as well as some custom internationalization functions based on the `L10n` functionality. 102 94 103 95 The following functions from `L10n` are published: 96 104 97 {{{ 105 98 scriptInterface.RegisterFunction<std::string, &GetCurrentLocale>("GetCurrentLocale"); … … 120 113 scriptInterface.RegisterFunction<std::wstring, double, &FormatDecimalNumberIntoString>("FormatDecimalNumberIntoString"); 121 114 }}} 115 The following custom functions are also published: 122 116 123 The following custom functions are also published:124 117 {{{ 125 118 // Returns the build time of the game formatted for the current locale. … … 138 131 scriptInterface.RegisterFunction<std::wstring, std::wstring, &MarkToTranslate>("MarkToTranslate"); 139 132 }}} 140 141 133 == JavaScript-Side Implementation == 142 143 134 The following sections describe some implementation details that are specific to the JavaScript side. 144 135 145 136 === JavaScript Translation Cache System === 146 147 137 Although you can access the C++ functions listed above from JavaScript simply prefixing them with `Engine`, as in `Engine.Translate()`, the `binaries/data/mods/public/globalscripts/l10n.js` file defines the following equivalent global internationalization functions: 148 138 … … 153 143 translatePluralWithContext(context, singularMessage, pluralMessage, number); 154 144 }}} 155 156 145 These global functions are basically wrappers for the engine internationalization functions, however they are not just that. These global functions use JavaScript-side caching to reduce the number of calls to the engine functions, because calls to engine functions require string conversions that are far from cheap. 157 146 … … 159 148 160 149 === Object Translation Helper Function === 161 162 150 The `binaries/data/mods/public/globalscripts/l10n.js` file defines a function, `translateObjectKeys`, which is a helper function that can translate specific properties of a !JavaScript object: 163 151 … … 165 153 translateObjectKeys(object, keys); 166 154 }}} 167 168 155 The `keys` parameter is an array of strings with the names of the object properties to translate. 169 156 170 157 === String Formatting Function === 171 172 158 The `binaries/data/mods/public/globalscripts/sprintf.js` file defines a function, `sprintf`, that can be used for string formatting. To learn how to use it, see [wiki:Internationalization#UsingStringFormattingInsteadofConcatenatingStringsinJavaScript Using String Formatting Instead of Concatenating Strings in JavaScript]. 173 159 174 160 = Message Extraction = 175 176 161 Message extractions is the process of parsing the source files searching for strings that need to be translated, and generating a translation template file (POT) from them that translators can use. 177 162 … … 185 170 186 171 = Localization = 187 188 172 == Transifex Integration == 189 190 173 Currently, the translation of the game happens in Transifex, at https://www.transifex.com/projects/p/0ad/ 191 174 … … 197 180 198 181 == Long Strings Locale == 199 200 182 The `source/tools/i18n/generateLongStringTranslations.py` is a special script that generates a PO file for an artificial language with code "long". This PO file consists of the longest strings (as in number of characters) of every available language for each message. 201 183 … … 203 185 204 186 = Language Selection Menu = 187 The dialog box that you open when you select '''Options → Language '''in the main menu is defined in the following files: 205 188 206 The dialog box that you open when you select '''Options → Language '''in the main menu is defined in the following files: 207 * `gui/page_locale.xml` 208 * `gui/locale/locale.js` 209 * `gui/locale/locale.xml` 189 * `gui/page_locale.xml` 190 * `gui/locale/locale.js` 191 * `gui/locale/locale.xml`