Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
|
Singleton for internationalization and localization. More...
#include <L10n.h>
Public Types | |
enum | DateTimeType { DateTime , Date , Time } |
Types of dates. More... | |
Public Member Functions | |
L10n () | |
Creates an instance of L10n. More... | |
~L10n () | |
Handles the destruction of L10n. More... | |
const icu::Locale & | GetCurrentLocale () const |
Returns the current locale. More... | |
std::string | GetCurrentLocaleString () const |
Returns the code of the current locale. More... | |
std::vector< std::string > | GetAllLocales () const |
Returns a vector of locale codes supported by ICU. More... | |
bool | SaveLocale (const std::string &localeCode) const |
Saves the specified locale in the game configuration file. More... | |
bool | SaveLocale (const icu::Locale &locale) const |
std::vector< std::string > | GetSupportedLocaleBaseNames () const |
Returns an array of supported locale codes sorted alphabetically. More... | |
std::vector< std::wstring > | GetSupportedLocaleDisplayNames () const |
Returns an array of supported locale names sorted alphabetically by locale code. More... | |
std::string | GetLocaleLanguage (const std::string &locale) const |
Returns the ISO-639 language code of the specified locale code. More... | |
std::string | GetLocaleBaseName (const std::string &locale) const |
Returns the programmatic code of the entire locale without keywords. More... | |
std::string | GetLocaleCountry (const std::string &locale) const |
Returns the ISO-3166 country code of the specified locale code. More... | |
std::string | GetLocaleScript (const std::string &locale) const |
Returns the ISO-15924 abbreviation script code of the specified locale code. More... | |
bool | UseLongStrings () const |
Returns true if the current locale is the special "Long Strings" locale. More... | |
std::vector< std::wstring > | GetDictionariesForLocale (const std::string &locale) const |
Returns an array of paths to files in the virtual filesystem that provide translations for the specified locale code. More... | |
std::wstring | GetFallbackToAvailableDictLocale (const icu::Locale &locale) const |
std::wstring | GetFallbackToAvailableDictLocale (const std::string &locale) const |
std::string | GetDictionaryLocale (const std::string &configLocaleString) const |
Returns the code of the recommended locale for the current user that the game supports. More... | |
void | GetDictionaryLocale (const std::string &configLocaleString, icu::Locale &outLocale) const |
Saves an instance of the recommended locale for the current user that the game supports in the specified variable. More... | |
void | ReevaluateCurrentLocaleAndReload () |
Determines the best, supported locale for the current user, makes it the current game locale and reloads the translations dictionary with translations for that locale. More... | |
bool | ValidateLocale (const icu::Locale &locale) const |
Returns true if the locale is supported by both ICU and the game. More... | |
bool | ValidateLocale (const std::string &localeCode) const |
std::string | Translate (const std::string &sourceString) const |
Returns the translation of the specified string to the current locale. More... | |
std::string | TranslateWithContext (const std::string &context, const std::string &sourceString) const |
Returns the translation of the specified string to the current locale in the specified context. More... | |
std::string | TranslatePlural (const std::string &singularSourceString, const std::string &pluralSourceString, int number) const |
Returns the translation of the specified string to the current locale based on the specified number. More... | |
std::string | TranslatePluralWithContext (const std::string &context, const std::string &singularSourceString, const std::string &pluralSourceString, int number) const |
Returns the translation of the specified string to the current locale in the specified context, based on the specified number. More... | |
std::string | TranslateLines (const std::string &sourceString) const |
Translates a text line by line to the current locale. More... | |
UDate | ParseDateTime (const std::string &dateTimeString, const std::string &dateTimeFormat, const icu::Locale &locale) const |
Parses the date in the input string using the specified date format, and returns the parsed date as a UNIX timestamp in milliseconds (not seconds). More... | |
std::string | LocalizeDateTime (const UDate dateTime, const DateTimeType &type, const icu::DateFormat::EStyle &style) const |
Returns the specified date using the specified date format. More... | |
std::string | FormatMillisecondsIntoDateString (const UDate milliseconds, const std::string &formatString, bool useLocalTimezone) const |
Returns the specified date using the specified date format. More... | |
std::string | FormatDecimalNumberIntoString (double number) const |
Returns the specified floating-point number as a string, with the number formatted as a decimal number using the current locale. More... | |
VfsPath | LocalizePath (const VfsPath &sourcePath) const |
Returns the localized version of the specified path if there is one for the current locale. More... | |
Status | ReloadChangedFile (const VfsPath &path) |
Loads path into the dictionary if it is a translation file of the current locale. More... | |
Public Member Functions inherited from Singleton< L10n > | |
Singleton () | |
~Singleton () | |
Private Member Functions | |
void | LoadDictionaryForCurrentLocale () |
Loads the translation files for the current locale. More... | |
Private Attributes | |
std::unique_ptr< tinygettext::Dictionary > | m_Dictionary |
Dictionary that contains the translations for the current locale and the matching English strings, including contexts and plural forms. More... | |
icu::Locale | m_CurrentLocale |
Locale that the game is currently using. More... | |
std::vector< icu::Locale > | m_AvailableLocales |
Vector with the locales that the game supports. More... | |
bool | m_CurrentLocaleIsOriginalGameLocale {false} |
Whether the game is using the default game locale (true ), 'en_US', or not (false ). More... | |
bool | m_UseLongStrings {false} |
Whether the game is using the special game locale with the longest strings of each translation (true ) or not (false ). More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from Singleton< L10n > | |
static L10n & | GetSingleton () |
static L10n * | GetSingletonPtr () |
static bool | IsInitialised () |
Singleton for internationalization and localization.
enum L10n::DateTimeType |
Types of dates.
Enumerator | |
---|---|
DateTime | Both date and time. |
Date | Only date. |
Time | Only time. |
L10n::L10n | ( | ) |
L10n::~L10n | ( | ) |
std::string L10n::FormatDecimalNumberIntoString | ( | double | number | ) | const |
Returns the specified floating-point number as a string, with the number formatted as a decimal number using the current locale.
number | Number to format. |
std::string L10n::FormatMillisecondsIntoDateString | ( | const UDate | milliseconds, |
const std::string & | formatString, | ||
bool | useLocalTimezone | ||
) | const |
Returns the specified date using the specified date format.
milliseconds | Date specified as a UNIX timestamp in milliseconds (not seconds). |
formatString | Date format string defined using ICU date formatting symbols. Usually, you internationalize the format string and get it translated before you pass it to FormatMillisecondsIntoDateString(). |
useLocalTimezone | Boolean useful for durations |
std::vector< std::string > L10n::GetAllLocales | ( | ) | const |
Returns a vector of locale codes supported by ICU.
A locale code is a string such as "de" or "pt_BR".
const icu::Locale & L10n::GetCurrentLocale | ( | ) | const |
Returns the current locale.
std::string L10n::GetCurrentLocaleString | ( | ) | const |
Returns the code of the current locale.
A locale code is a string such as "de" or "pt_BR".
std::vector< std::wstring > L10n::GetDictionariesForLocale | ( | const std::string & | locale | ) | const |
Returns an array of paths to files in the virtual filesystem that provide translations for the specified locale code.
locale | Locale code. |
locale
. std::string L10n::GetDictionaryLocale | ( | const std::string & | configLocaleString | ) | const |
Returns the code of the recommended locale for the current user that the game supports.
"That the game supports" means both that a translation file is available for that locale and that the locale code is either supported by ICU or the special "long" locale code.
The mechanism to select a recommended locale follows this logic:
configLocale
.configLocaleString | Locale to check for support first. Pass an empty string to check the system locale directly. |
void L10n::GetDictionaryLocale | ( | const std::string & | configLocaleString, |
icu::Locale & | outLocale | ||
) | const |
Saves an instance of the recommended locale for the current user that the game supports in the specified variable.
"That the game supports" means both that a translation file is available for that locale and that the locale code is either supported by ICU or the special "long" locale code.
The mechanism to select a recommended locale follows this logic:
configLocale
.configLocaleString | Locale to check for support first. Pass an empty string to check the system locale directly. |
outLocale | The recommended locale. |
std::wstring L10n::GetFallbackToAvailableDictLocale | ( | const icu::Locale & | locale | ) | const |
std::wstring L10n::GetFallbackToAvailableDictLocale | ( | const std::string & | locale | ) | const |
std::string L10n::GetLocaleBaseName | ( | const std::string & | locale | ) | const |
Returns the programmatic code of the entire locale without keywords.
locale | Locale code. |
std::string L10n::GetLocaleCountry | ( | const std::string & | locale | ) | const |
Returns the ISO-3166 country code of the specified locale code.
For example, if you specify the 'en_US' locate, it returns 'US'.
locale | Locale code. |
std::string L10n::GetLocaleLanguage | ( | const std::string & | locale | ) | const |
Returns the ISO-639 language code of the specified locale code.
For example, if you specify the 'en_US' locate, it returns 'en'.
locale | Locale code. |
std::string L10n::GetLocaleScript | ( | const std::string & | locale | ) | const |
Returns the ISO-15924 abbreviation script code of the specified locale code.
locale | Locale code. |
std::vector< std::string > L10n::GetSupportedLocaleBaseNames | ( | ) | const |
Returns an array of supported locale codes sorted alphabetically.
A locale code is a string such as "de" or "pt_BR".
If yours is a development copy (the 'config/dev.cfg' file is found in the virtual filesystem), the output array may include the special "long" locale code.
std::vector< std::wstring > L10n::GetSupportedLocaleDisplayNames | ( | ) | const |
Returns an array of supported locale names sorted alphabetically by locale code.
A locale code is a string such as "de" or "pt_BR".
If yours is a development copy (the 'config/dev.cfg' file is found in the virtual filesystem), the output array may include the special "Long Strings" locale name.
|
private |
Loads the translation files for the current locale.
This method loads every file in the 'l10n' folder of the game virtual filesystem that is prefixed with the code of the current locale followed by a dot.
For example, if the code of the current locale code is 'de', LoadDictionaryForCurrentLocale() loads the 'l10n/de.engine.po' and 'l10n/de.public.po' translation files.
std::string L10n::LocalizeDateTime | ( | const UDate | dateTime, |
const DateTimeType & | type, | ||
const icu::DateFormat::EStyle & | style | ||
) | const |
Returns the specified date using the specified date format.
dateTime | Date specified as a UNIX timestamp in milliseconds (not seconds). |
type | Whether the formatted date must show both the date and the time, only the date or only the time. |
style | ICU style for the formatted date. |
Returns the localized version of the specified path if there is one for the current locale.
If there is no localized version of the specified path, it returns the specified path.
For example, if the code of the current locale is 'de_DE', LocalizePath() splits the input path into folder path and file name, and checks whether the '<folder>/l10n/de/<file>' file exists. If it does, it returns that path. Otherwise, it returns the input path, verbatim.
This function is used for file localization (for example, image localization).
sourcePath | Path to localize. |
sourcePath
otherwise.UDate L10n::ParseDateTime | ( | const std::string & | dateTimeString, |
const std::string & | dateTimeFormat, | ||
const icu::Locale & | locale | ||
) | const |
Parses the date in the input string using the specified date format, and returns the parsed date as a UNIX timestamp in milliseconds (not seconds).
dateTimeString | String containing the date to parse. |
dateTimeFormat | Date format string to parse the input date, defined using ICU date formatting symbols. |
locale | Locale to use when parsing the input date. |
void L10n::ReevaluateCurrentLocaleAndReload | ( | ) |
Determines the best, supported locale for the current user, makes it the current game locale and reloads the translations dictionary with translations for that locale.
To determine the best locale, ReevaluateCurrentLocaleAndReload():
Loads path
into the dictionary if it is a translation file of the current locale.
bool L10n::SaveLocale | ( | const icu::Locale & | locale | ) | const |
bool L10n::SaveLocale | ( | const std::string & | localeCode | ) | const |
Saves the specified locale in the game configuration file.
The next time that the game starts, the game uses the locale in the configuration file if there are translation files available for it.
SaveLocale() checks the validity of the specified locale with ValidateLocale(). If the specified locale is not valid, SaveLocale() returns false
and does not save the locale to the configuration file.
localeCode | Locale to save to the configuration file. |
true
) or not (false
). std::string L10n::Translate | ( | const std::string & | sourceString | ) | const |
Returns the translation of the specified string to the current locale.
sourceString | String to translate to the current locale. |
sourceString
to the current locale, or sourceString
if there is no translation available. std::string L10n::TranslateLines | ( | const std::string & | sourceString | ) | const |
Translates a text line by line to the current locale.
TranslateLines() is helpful when you need to translate a plain text file after you load it.
sourceString | Text to translate to the current locale. |
sourceString
to the current locale. Some of the lines in the returned text may be in English because there was not translation available for them. std::string L10n::TranslatePlural | ( | const std::string & | singularSourceString, |
const std::string & | pluralSourceString, | ||
int | number | ||
) | const |
Returns the translation of the specified string to the current locale based on the specified number.
singularSourceString | String to translate to the current locale, in English' singular form. |
pluralSourceString | String to translate to the current locale, in English' plural form. |
number | Number that determines the required form of the translation (or the English string if no translation is available). |
number
, or either singularSourceString
(if number
is 1) or pluralSourceString
(if number
is not 1) if there is no translation available. std::string L10n::TranslatePluralWithContext | ( | const std::string & | context, |
const std::string & | singularSourceString, | ||
const std::string & | pluralSourceString, | ||
int | number | ||
) | const |
Returns the translation of the specified string to the current locale in the specified context, based on the specified number.
context | Context where the string is used. See http://www.gnu.org/software/gettext/manual/html_node/Contexts.html |
singularSourceString | String to translate to the current locale, in English' singular form. |
pluralSourceString | String to translate to the current locale, in English' plural form. |
number | Number that determines the required form of the translation (or the English string if no translation is available). * |
context
and for the specified number
, or either singularSourceString
(if number
is 1) or pluralSourceString
(if number
is not 1) if there is no translation available. std::string L10n::TranslateWithContext | ( | const std::string & | context, |
const std::string & | sourceString | ||
) | const |
Returns the translation of the specified string to the current locale in the specified context.
context | Context where the string is used. See http://www.gnu.org/software/gettext/manual/html_node/Contexts.html |
sourceString | String to translate to the current locale. |
sourceString
to the current locale in the specified context
, or sourceString
if there is no translation available. bool L10n::UseLongStrings | ( | ) | const |
Returns true
if the current locale is the special "Long Strings" locale.
It returns false
otherwise.
true
) or not (false
). bool L10n::ValidateLocale | ( | const icu::Locale & | locale | ) | const |
Returns true
if the locale is supported by both ICU and the game.
It returns false
otherwise.
It returns true
if both of these conditions are true:
locale | Locale to check. |
locale
is supported by both ICU and the game (true
) or not (false
). bool L10n::ValidateLocale | ( | const std::string & | localeCode | ) | const |
|
private |
Vector with the locales that the game supports.
The list of available locales is calculated when the game starts. Call LoadListOfAvailableLocales() to refresh the list.
|
private |
Locale that the game is currently using.
To get the current locale, use its getter: GetCurrentLocale(). You can also use GetCurrentLocaleString() to get the locale code of the current locale.
To change the value of this variable:
|
private |
Whether the game is using the default game locale (true
), 'en_US', or not (false
).
This variable is used in the L10n implementation for performance reasons. Many localization steps can be skipped when this variable is true
.
|
private |
Dictionary that contains the translations for the current locale and the matching English strings, including contexts and plural forms.
|
private |
Whether the game is using the special game locale with the longest strings of each translation (true
) or not (false
).