Changes between Version 7 and Version 8 of Implementation_of_Internationalization_and_Localization
- Timestamp:
- Apr 16, 2014, 1:42:08 PM (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Implementation_of_Internationalization_and_Localization
v7 v8 17 17 The heart of the internationalization and localization implementation is at `source/i18n/L10n.cpp`. 18 18 19 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()`.19 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()`. 20 20 21 21 This class holds the current locale and the translations for the current locale at any given time, and its methods are used all over the C++ implementation (and published to JavaScript as well). … … 29 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`. 30 30 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).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). 32 32 33 33 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()]. … … 35 35 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 36 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.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. 38 38 39 39 === Localization Functions === … … 43 43 Translation functions ([wiki:Internationalization] explains how to use them): 44 44 {{{ 45 std::string translate(const std::string& sourceString);46 std::string translateWithContext(const std::string& context, const std::string& sourceString);47 std::string translatePlural(const std::string& singularSourceString, const std::string& pluralSourceString, int number);48 std::string translatePluralWithContext(const std::string& context, const std::string& singularSourceString, const std::string& pluralSourceString, int number);49 std::string translateLines(const std::string& sourceString);45 std::string Translate(const std::string& sourceString); 46 std::string TranslateWithContext(const std::string& context, const std::string& sourceString); 47 std::string TranslatePlural(const std::string& singularSourceString, const std::string& pluralSourceString, int number); 48 std::string TranslatePluralWithContext(const std::string& context, const std::string& singularSourceString, const std::string& pluralSourceString, int number); 49 std::string TranslateLines(const std::string& sourceString); 50 50 }}} 51 51 … … 55 55 // into icu::UDate and the other way around. These are used in 56 56 // “gui/scripting/ScriptFunctions.cpp”. 57 UDate parseDateTime(const std::string& dateTimeString, const std::string& dateTimeFormat, const Locale& locale);58 std::string localizeDateTime(const UDate& dateTime, DateTimeType type, DateFormat::EStyle style);57 UDate ParseDateTime(const std::string& dateTimeString, const std::string& dateTimeFormat, const Locale& locale); 58 std::string LocalizeDateTime(const UDate& dateTime, DateTimeType type, DateFormat::EStyle style); 59 59 60 60 // Converts a string from UNIX timestamp (in milliseconds, not seconds) into a 61 61 // string with the specified format. See the ‘Internationalization’ page in the 62 62 // documentation for more information. 63 std::string formatMillisecondsIntoDateString(int milliseconds, const std::string& formatString);63 std::string FormatMillisecondsIntoDateString(int milliseconds, const std::string& formatString); 64 64 }}} 65 65 … … 68 68 // Returns the specified floating-point number as a string using the current 69 69 // locale. 70 std::string formatDecimalNumberIntoString(double number);70 std::string FormatDecimalNumberIntoString(double number); 71 71 }}} 72 72 … … 76 76 // Otherwise, it returns sourcePath. 77 77 // This is used for image localization (see below). 78 VfsPath localizePath(VfsPath sourcePath);78 VfsPath LocalizePath(VfsPath sourcePath); 79 79 }}} 80 80 … … 91 91 === Images === 92 92 93 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.93 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 94 95 95 See [wiki:Localization#LocalizingImages Localizing Images] for more information. … … 97 97 == Functions Published for JavaScript == 98 98 99 The `source/gui/scripting/ScriptFunctions.cpp` file publishes some of the functions that the `L10n` singleton offers, as well as some custom internationalization functions based on the `L10n` functionality. 99 '''TODO''': Link to svn.wildfiregames.com/docs when #67 gets committed. 100 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. 100 102 101 103 The following functions from `L10n` are published: 102 104 {{{ 103 scriptInterface.RegisterFunction<std::string, & getCurrentLocale>("getCurrentLocale");105 scriptInterface.RegisterFunction<std::string, &GetCurrentLocale>("GetCurrentLocale"); 104 106 scriptInterface.RegisterFunction<void, std::string, &SetLocale>("SetLocale"); 105 107 … … 108 110 scriptInterface.RegisterFunction<int, &GetCurrentLocaleIndex>("GetCurrentLocaleIndex"); 109 111 110 scriptInterface.RegisterFunction<std::wstring, std::wstring, & translate>("translate");111 scriptInterface.RegisterFunction<std::wstring, std::string, std::wstring, & translateWithContext>("translateWithContext");112 scriptInterface.RegisterFunction<std::wstring, std::wstring, std::wstring, int, & translatePlural>("translatePlural");113 scriptInterface.RegisterFunction<std::wstring, std::string, std::wstring, std::wstring, int, & translatePluralWithContext>("translatePluralWithContext");114 scriptInterface.RegisterFunction<std::wstring, std::wstring, & translateLines>("translateLines");115 116 scriptInterface.RegisterFunction<std::wstring, int, std::wstring, & formatMillisecondsIntoDateString>("formatMillisecondsIntoDateString");117 118 scriptInterface.RegisterFunction<std::wstring, double, & formatDecimalNumberIntoString>("formatDecimalNumberIntoString");112 scriptInterface.RegisterFunction<std::wstring, std::wstring, &Translate>("Translate"); 113 scriptInterface.RegisterFunction<std::wstring, std::string, std::wstring, &TranslateWithContext>("TranslateWithContext"); 114 scriptInterface.RegisterFunction<std::wstring, std::wstring, std::wstring, int, &TranslatePlural>("TranslatePlural"); 115 scriptInterface.RegisterFunction<std::wstring, std::string, std::wstring, std::wstring, int, &TranslatePluralWithContext>("TranslatePluralWithContext"); 116 scriptInterface.RegisterFunction<std::wstring, std::wstring, &TranslateLines>("TranslateLines"); 117 118 scriptInterface.RegisterFunction<std::wstring, int, std::wstring, &FormatMillisecondsIntoDateString>("FormatMillisecondsIntoDateString"); 119 120 scriptInterface.RegisterFunction<std::wstring, double, &FormatDecimalNumberIntoString>("FormatDecimalNumberIntoString"); 119 121 }}} 120 122 … … 122 124 {{{ 123 125 // Returns the build time of the game formatted for the current locale. 124 scriptInterface.RegisterFunction<std::wstring, int, &GetBuildTimestamp>("GetBuildTimestamp"); 126 scriptInterface.RegisterFunction<std::wstring, int, &GetBuildTimestamp>("GetBuildTimestamp"); // in source/gui/scripting/ScriptFunctions.cpp 125 127 126 128 // Translates an array of strings, avoiding what would otherwise be many calls 127 129 // to the C++ engine from JavaScript. 128 scriptInterface.RegisterFunction<std::vector<std::wstring>, std::vector<std::wstring>, & translateArray>("translateArray");130 scriptInterface.RegisterFunction<std::vector<std::wstring>, std::vector<std::wstring>, &TranslateArray>("TranslateArray"); 129 131 130 132 // Simply returns the string that it receives. This function is required to mark … … 134 136 // below). For more information, see: 135 137 // http://www.gnu.org/software/gettext/manual/html_node/Special-cases.html 136 scriptInterface.RegisterFunction<std::wstring, std::wstring, & markToTranslate>("markToTranslate");138 scriptInterface.RegisterFunction<std::wstring, std::wstring, &MarkToTranslate>("MarkToTranslate"); 137 139 }}} 138 140 … … 143 145 === JavaScript Translation Cache System === 144 146 145 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:147 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: 146 148 147 149 {{{