FlightGear next
FGLocale Class Reference

#include <locale.hxx>

Public Member Functions

 FGLocale (SGPropertyNode *root)
 
virtual ~FGLocale ()
 
bool selectLanguage (const std::string &language={})
 Select the locale's primary language according to user-level, system-level language settings and the language argument.
 
std::string getLanguageId () const
 Return the value of _languageId, which uniquely identifies the language for the LanguageInfo class (handling of plural forms...).
 
std::string getPreferredLanguage () const
 Return the preferred language according to user choice and/or settings.
 
void loadAircraftTranslations ()
 
void loadAddonTranslations ()
 
std::string getLocalizedStringWithIndex (const std::string &id, const std::string &context, int index) const
 Obtain a single translation with the given identifier, context and index.
 
std::string getLocalizedString (const std::string &id, const std::string &resource, const std::string &defaultValue={})
 Obtain a single string matching the given id, with fallback.
 
std::vector< std::string > getLocalizedStrings (const std::string &id, const std::string &resource)
 Obtain a list of translations that share the same tag name (id stem).
 
std::size_t getLocalizedStringCount (const std::string &id, const std::string &context) const
 Return the number of strings with a given id in the specified context.
 
std::string getDefaultFont (const char *fallbackFont)
 Obtain default font for current locale.
 
std::string localizedPrintf (const char *id, const char *resource,...)
 Obtain a message string, from a localized resource ID, and use it as a printf format string.
 
std::string vlocalizedPrintf (const char *id, const char *resource, va_list args)
 
void clear ()
 reset all data in the locale.
 
SGPropertyNode_ptr selectLanguageNode (SGPropertyNode *langs) const
 Given a node with children corresponding to different language / locale codes, select one based on the user preferred language.
 

Static Public Member Functions

static void utf8toLatin1 (std::string &s)
 Simple UTF8 to Latin1 encoder.
 

Protected Member Functions

SGPropertyNode * findLocaleNode (const std::string &language)
 Find a property node matching the given language.
 
void loadResourceForDefaultTranslation (const SGPath &xmlFile, const std::string &domain, const std::string &resource)
 Load default strings for the requested resource ("atc", "menu", etc.).
 
void loadDefaultTranslation (const simgear::Dir &defaultTranslationDir, const std::string &domain)
 Load all default translation files from the specified directory.
 
void loadCoreResourcesForDefaultTranslation ()
 Load the core default translation ('atc', 'menu', 'options', 'sys', etc.).
 
void loadResourcesFromAircraftOrAddonDir (const SGPath &basePath, const std::string &domain)
 From an add-on or aircraft directory, load the default translation and, if available, the XLIFF file for the current locale.
 
void loadXLIFFFromAircraftOrAddonDir (const SGPath &basePath, const std::string &domain)
 
string_list getUserLanguages () const
 Obtain user's default language settings.
 
std::string findLanguageId () const
 Return the appropriate value for _languageId according to _currentLocale.
 
void loadXLIFF (const SGPath &basePath, SGPropertyNode *localeNode, const std::string &domain)
 Load an XLIFF 1.2 file.
 

Protected Attributes

SGPropertyNode_ptr _intl
 
SGPropertyNode_ptr _currentLocale
 
std::string _languageId
 This is used to fetch linguistic data such as the number of plural forms for the selected locale.
 
SGPropertyNode_ptr _fallbackLocale
 Proper locale (corresponding to a /sim/intl/locale[n] node, as opposed to the default translation) used when none of the /sim/intl/locale[n]/lang nodes matches the –language value or other user language settings.
 
std::string _currentLocaleString
 Corresponds to user's language settings, possibly overridden by the –language value.
 

Friends

class FGTranslate
 

Detailed Description

Definition at line 124 of file locale.hxx.

Constructor & Destructor Documentation

◆ FGLocale()

FGLocale::FGLocale ( SGPropertyNode * root)

Definition at line 48 of file locale.cxx.

◆ ~FGLocale()

FGLocale::~FGLocale ( )
virtual

Definition at line 54 of file locale.cxx.

Member Function Documentation

◆ clear()

void FGLocale::clear ( )

reset all data in the locale.

This is needed to allow the launcher to use the code, without disturbing the main behaviour. Afteer calling this you can do selectLangauge again without problems.

Definition at line 431 of file locale.cxx.

◆ findLanguageId()

std::string FGLocale::findLanguageId ( ) const
protected

Return the appropriate value for _languageId according to _currentLocale.

When _currentLocale isn't the empty shared pointer, alert if the /sim/intl/locale[n]/language-id node doesn't exist.

Definition at line 279 of file locale.cxx.

◆ findLocaleNode()

SGPropertyNode * FGLocale::findLocaleNode ( const std::string & language)
protected

Find a property node matching the given language.

Definition at line 158 of file locale.cxx.

◆ getDefaultFont()

std::string FGLocale::getDefaultFont ( const char * fallbackFont)

Obtain default font for current locale.

Definition at line 556 of file locale.cxx.

◆ getLanguageId()

std::string FGLocale::getLanguageId ( ) const

Return the value of _languageId, which uniquely identifies the language for the LanguageInfo class (handling of plural forms...).

Returns
"default" for the default translation; else, the string value of the /sim/intl/locale[n]/language-id node where /sim/intl/locale[n] corresponds to the selected locale
See also
getPreferredLanguage()

Definition at line 305 of file locale.cxx.

◆ getLocalizedString()

std::string FGLocale::getLocalizedString ( const std::string & id,
const std::string & resource,
const std::string & defaultValue = {} )

Obtain a single string matching the given id, with fallback.

Selected context refers to "menu", "options", "dialog" etc.

Parameters
defaultValuereturned if the requested translation is missing or empty and the default translation (source text) is empty.

Due to these conditions, this only makes sense when FlightGear uses the default translation and some translatable “strings” have been defined as empty elements (so that their “source text” is empty).

Definition at line 533 of file locale.cxx.

◆ getLocalizedStringCount()

std::size_t FGLocale::getLocalizedStringCount ( const std::string & id,
const std::string & context ) const

Return the number of strings with a given id in the specified context.

Parameters
contexta string such as "menu", "options", "sys", etc.

Definition at line 547 of file locale.cxx.

◆ getLocalizedStrings()

vector< string > FGLocale::getLocalizedStrings ( const std::string & id,
const std::string & resource )

Obtain a list of translations that share the same tag name (id stem).

Parameters
idname of the tag in the default translation XML file
resourcea string such as "menu", "options", "sys", etc.
Returns
A vector of translated strings

Definition at line 541 of file locale.cxx.

◆ getLocalizedStringWithIndex()

std::string FGLocale::getLocalizedStringWithIndex ( const std::string & id,
const std::string & context,
int index ) const

Obtain a single translation with the given identifier, context and index.

Definition at line 525 of file locale.cxx.

◆ getPreferredLanguage()

std::string FGLocale::getPreferredLanguage ( ) const

Return the preferred language according to user choice and/or settings.

Examples: 'fr_CA', 'de_DE'... or the empty string if nothing could be found.

Note that this is not necessarily the same as the last value passed to selectLanguage(), assuming it was non-empty, because the latter may have an encoding specifier, while values returned by getPreferredLanguage() never have that.

See also
getLanguageId()

Definition at line 450 of file locale.cxx.

◆ getUserLanguages()

string_list FGLocale::getUserLanguages ( ) const
protected

Obtain user's default language settings.

Determine locale/language settings on Linux/Unix.

Definition at line 140 of file locale.cxx.

◆ loadAddonTranslations()

void FGLocale::loadAddonTranslations ( )

Definition at line 324 of file locale.cxx.

◆ loadAircraftTranslations()

void FGLocale::loadAircraftTranslations ( )

Definition at line 318 of file locale.cxx.

◆ loadCoreResourcesForDefaultTranslation()

void FGLocale::loadCoreResourcesForDefaultTranslation ( )
protected

Load the core default translation ('atc', 'menu', 'options', 'sys', etc.).

Definition at line 310 of file locale.cxx.

◆ loadDefaultTranslation()

void FGLocale::loadDefaultTranslation ( const simgear::Dir & defaultTranslationDir,
const std::string & domain )
protected

Load all default translation files from the specified directory.

Parameters
defaultTranslationDirbase directory for the XML files
domaintarget domain

The XML files are looked for in two directories: defaultTranslationDir and its auto-extracted subdirectory. Each XML file is loaded as a resource whose name is the SGPath::file_base() of the file so that, e.g., atc.no_translate.xml is loaded as the 'atc' resource.

Definition at line 353 of file locale.cxx.

◆ loadResourceForDefaultTranslation()

void FGLocale::loadResourceForDefaultTranslation ( const SGPath & xmlFile,
const std::string & domain,
const std::string & resource )
protected

Load default strings for the requested resource ("atc", "menu", etc.).

To avoid confusing unrelated things, translatable strings from the simulator core (FGData), from an add-on or from the current aircraft are all stored in different domains. There are three kinds of domains:

  • 'core' for strings coming from FGData;
  • 'addons/⟨addonId⟩' for strings coming from an add-on;
  • 'current-aircraft' for strings coming from the current aircraft.

Definition at line 481 of file locale.cxx.

◆ loadResourcesFromAircraftOrAddonDir()

void FGLocale::loadResourcesFromAircraftOrAddonDir ( const SGPath & basePath,
const std::string & domain )
protected

From an add-on or aircraft directory, load the default translation and, if available, the XLIFF file for the current locale.

Definition at line 339 of file locale.cxx.

◆ loadXLIFF()

void FGLocale::loadXLIFF ( const SGPath & basePath,
SGPropertyNode * localeNode,
const std::string & domain )
protected

Load an XLIFF 1.2 file.

Parameters
basePathbase for the relative path to XLIFF file that is the string value of node /sim/intl/locale[n]/⟨domain⟩/xliff.
localeNodepointer to the /sim/intl/locale[n] node for the current locale
domaina string such as 'core' or 'addons/⟨addonId⟩'

Definition at line 455 of file locale.cxx.

◆ loadXLIFFFromAircraftOrAddonDir()

void FGLocale::loadXLIFFFromAircraftOrAddonDir ( const SGPath & basePath,
const std::string & domain )
protected

Definition at line 381 of file locale.cxx.

◆ localizedPrintf()

std::string FGLocale::localizedPrintf ( const char * id,
const char * resource,
... )

Obtain a message string, from a localized resource ID, and use it as a printf format string.

Definition at line 577 of file locale.cxx.

◆ selectLanguage()

bool FGLocale::selectLanguage ( const std::string & language = {})

Select the locale's primary language according to user-level, system-level language settings and the language argument.

Parameters
languagelocale specification such as fr, fr_FR or fr_FR.UTF-8; it takes precedence over system settings (pass an empty value if you want it to be ignored). The special value 'default' causes FlightGear to use the “default translation” (see above).

Once this function returns, getLanguageId() is safe to call.

Definition at line 197 of file locale.cxx.

◆ selectLanguageNode()

SGPropertyNode_ptr FGLocale::selectLanguageNode ( SGPropertyNode * langs) const

Given a node with children corresponding to different language / locale codes, select one based on the user preferred language.

Definition at line 660 of file locale.cxx.

◆ utf8toLatin1()

void FGLocale::utf8toLatin1 ( std::string & s)
static

Simple UTF8 to Latin1 encoder.

Definition at line 597 of file locale.cxx.

◆ vlocalizedPrintf()

std::string FGLocale::vlocalizedPrintf ( const char * id,
const char * resource,
va_list args )

Definition at line 586 of file locale.cxx.

Friends And Related Symbol Documentation

◆ FGTranslate

friend class FGTranslate
friend

Definition at line 358 of file locale.hxx.

Member Data Documentation

◆ _currentLocale

SGPropertyNode_ptr FGLocale::_currentLocale
protected

Definition at line 302 of file locale.hxx.

◆ _currentLocaleString

std::string FGLocale::_currentLocaleString
protected

Corresponds to user's language settings, possibly overridden by the –language value.

Not sure this is very useful, contrary to _languageId.

Definition at line 324 of file locale.hxx.

◆ _fallbackLocale

SGPropertyNode_ptr FGLocale::_fallbackLocale
protected

Proper locale (corresponding to a /sim/intl/locale[n] node, as opposed to the default translation) used when none of the /sim/intl/locale[n]/lang nodes matches the –language value or other user language settings.

Contrary to the default translation, this is normally proper English with two plural forms.

Definition at line 318 of file locale.hxx.

◆ _intl

SGPropertyNode_ptr FGLocale::_intl
protected

Definition at line 301 of file locale.hxx.

◆ _languageId

std::string FGLocale::_languageId
protected

This is used to fetch linguistic data such as the number of plural forms for the selected locale.

The value is that of the 'language-id' (first and only) child of the _currentLocale node, except for the default locale which is characterized by _currentLocale == nullptr and _languageId == "default".

Definition at line 310 of file locale.hxx.


The documentation for this class was generated from the following files: