2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "settings/lib/ISettingCallback.h"
12 #include "settings/lib/ISettingsHandler.h"
13 #include "utils/GlobalsHandling.h"
14 #include "utils/Locale.h"
15 #include "utils/Speed.h"
16 #include "utils/Temperature.h"
29 #endif // GetDateFormat
32 #endif // GetTimeFormat
33 #endif // TARGET_WINDOWS
36 struct StringSettingOption
;
40 class CLanguageResource
;
42 typedef std::shared_ptr
<ADDON::CLanguageResource
> LanguageResourcePtr
;
44 enum class MeridiemSymbol
50 class CLangInfo
: public ISettingCallback
, public ISettingsHandler
54 ~CLangInfo() override
;
56 // implementation of ISettingCallback
57 void OnSettingChanged(const std::shared_ptr
<const CSetting
>& setting
) override
;
59 // implementation of ISettingsHandler
60 void OnSettingsLoaded() override
;
63 * \brief Get language codes list of the installed language addons.
64 * \param languages [OUT] The list of languages (language code, name).
66 static void GetAddonsLanguageCodes(std::map
<std::string
, std::string
>& languages
);
69 \brief Returns the language addon for the given locale (or the current one).
71 \param locale (optional) Locale of the language (current if empty)
72 \return Language addon for the given locale or NULL if the locale is invalid.
74 LanguageResourcePtr
GetLanguageAddon(const std::string
& locale
= "") const;
76 std::string
GetGuiCharSet() const;
77 std::string
GetSubtitleCharSet() const;
79 // three char language code (not win32 specific)
80 const std::string
& GetLanguageCode() const { return m_languageCodeGeneral
; }
83 * \brief Convert an english language name to an addon locale,
84 * by searching in the installed language addons.
85 * \param langName [IN] The english language name
86 * \return The locale for the given english name, or empty if not found
88 static std::string
ConvertEnglishNameToAddonLocale(const std::string
& langName
);
91 * \brief Get the english language name from given locale,
92 * by searching in the installed language addons.
93 * \param locale [OPT] Locale of the language (current if empty)
95 std::string
GetEnglishLanguageName(const std::string
& locale
= "") const;
98 \brief Sets and loads the given (or configured) language, its details and strings.
100 \param strLanguage (optional) Language to be loaded.
101 \param reloadServices (optional) Whether to reload services relying on localization.
102 \return True if the language has been successfully loaded, false otherwise.
104 bool SetLanguage(std::string strLanguage
= "", bool reloadServices
= true);
107 * \brief Get the audio language in ISO 639-2 format.
108 * \param allowFallback If set to true, when audio language setting is set to "default", "original" or "mediadefault"
109 * the returned value can fallback to a general language code (e.g. eng), otherwise an empty value is returned.
110 * \return The language code (user-defined also allowed). The value can be empty when allowFallback if set to false.
112 const std::string
& GetAudioLanguage(bool allowFallback
) const;
115 * \brief Set the audio language.
116 * \param language The language can either be a two char language code,
117 * or a three char language code, or a language name in english,
118 * also user-defined languages are allowed.
119 * \param isIso6392 Defines that language is in ISO 639-2 format, otherwise will be considered as ISO 639-1 format.
121 void SetAudioLanguage(const std::string
& language
, bool isIso6392
= false);
124 * \brief Get the subtitle language in ISO 639-2 format.
125 * \param allowFallback If set to true, when audio language setting is set to "default", "original"
126 * the returned value can fallback to a general language code (e.g. eng), otherwise an empty value is returned.
127 * \return The language code (user-defined also allowed). The value can be empty when allowFallback if set to false.
129 const std::string
& GetSubtitleLanguage(bool allowFallback
) const;
132 * \brief Set the subtitle language.
133 * \param language The language can either be a two char language code,
134 * or a three char language code, or a language name in english,
135 * also user-defined languages are allowed.
136 * \param isIso6392 Defines that language is in ISO 639-2 format, otherwise will be considered as ISO 639-1 format.
138 void SetSubtitleLanguage(const std::string
& language
, bool isIso6392
= false);
140 const std::string
GetDVDMenuLanguage() const;
141 const std::string
GetDVDAudioLanguage() const;
142 const std::string
GetDVDSubtitleLanguage() const;
143 const std::string
& GetTimeZone() const;
145 const std::string
& GetRegionLocale() const;
147 const std::locale
& GetOriginalLocale() const;
150 \brief Returns the full locale of the current language.
152 const CLocale
& GetLocale() const;
155 \brief Returns the system's current locale.
157 const std::locale
& GetSystemLocale() const { return m_systemLocale
; }
159 bool ForceUnicodeFont() const { return m_forceUnicodeFont
; }
161 const std::string
& GetDateFormat(bool bLongDate
= false) const;
162 void SetDateFormat(const std::string
& dateFormat
, bool bLongDate
= false);
163 const std::string
& GetShortDateFormat() const;
164 void SetShortDateFormat(const std::string
& shortDateFormat
);
165 const std::string
& GetLongDateFormat() const;
166 void SetLongDateFormat(const std::string
& longDateFormat
);
168 const std::string
& GetTimeFormat() const;
169 void SetTimeFormat(const std::string
& timeFormat
);
170 bool Use24HourClock() const;
171 void Set24HourClock(bool use24HourClock
);
172 void Set24HourClock(const std::string
& str24HourClock
);
173 const std::string
& GetMeridiemSymbol(MeridiemSymbol symbol
) const;
174 static const std::string
& MeridiemSymbolToString(MeridiemSymbol symbol
);
176 CTemperature::Unit
GetTemperatureUnit() const;
177 void SetTemperatureUnit(CTemperature::Unit temperatureUnit
);
178 void SetTemperatureUnit(const std::string
& temperatureUnit
);
179 const std::string
& GetTemperatureUnitString() const;
180 static const std::string
& GetTemperatureUnitString(CTemperature::Unit temperatureUnit
);
181 std::string
GetTemperatureAsString(const CTemperature
& temperature
) const;
183 CSpeed::Unit
GetSpeedUnit() const;
184 void SetSpeedUnit(CSpeed::Unit speedUnit
);
185 void SetSpeedUnit(const std::string
& speedUnit
);
186 const std::string
& GetSpeedUnitString() const;
187 static const std::string
& GetSpeedUnitString(CSpeed::Unit speedUnit
);
188 std::string
GetSpeedAsString(const CSpeed
& speed
) const;
190 void GetRegionNames(std::vector
<std::string
>& array
);
191 void SetCurrentRegion(const std::string
& strName
);
192 const std::string
& GetCurrentRegion() const;
194 std::set
<std::string
> GetSortTokens() const;
196 static std::string
GetLanguagePath() { return "resource://"; }
197 static std::string
GetLanguagePath(const std::string
&language
);
198 static std::string
GetLanguageInfoPath(const std::string
&language
);
199 bool UseLocaleCollation();
201 static void LoadTokens(const TiXmlNode
* pTokens
, std::set
<std::string
>& vecTokens
);
203 static void SettingOptionsLanguageNamesFiller(const std::shared_ptr
<const CSetting
>& setting
,
204 std::vector
<StringSettingOption
>& list
,
205 std::string
& current
,
207 static void SettingOptionsAudioStreamLanguagesFiller(
208 const std::shared_ptr
<const CSetting
>& setting
,
209 std::vector
<StringSettingOption
>& list
,
210 std::string
& current
,
212 static void SettingOptionsSubtitleStreamLanguagesFiller(
213 const std::shared_ptr
<const CSetting
>& setting
,
214 std::vector
<StringSettingOption
>& list
,
215 std::string
& current
,
217 static void SettingOptionsSubtitleDownloadlanguagesFiller(
218 const std::shared_ptr
<const CSetting
>& setting
,
219 std::vector
<StringSettingOption
>& list
,
220 std::string
& current
,
222 static void SettingOptionsISO6391LanguagesFiller(const std::shared_ptr
<const CSetting
>& setting
,
223 std::vector
<StringSettingOption
>& list
,
224 std::string
& current
,
226 static void SettingOptionsRegionsFiller(const std::shared_ptr
<const CSetting
>& setting
,
227 std::vector
<StringSettingOption
>& list
,
228 std::string
& current
,
230 static void SettingOptionsShortDateFormatsFiller(const std::shared_ptr
<const CSetting
>& setting
,
231 std::vector
<StringSettingOption
>& list
,
232 std::string
& current
,
234 static void SettingOptionsLongDateFormatsFiller(const std::shared_ptr
<const CSetting
>& setting
,
235 std::vector
<StringSettingOption
>& list
,
236 std::string
& current
,
238 static void SettingOptionsTimeFormatsFiller(const std::shared_ptr
<const CSetting
>& setting
,
239 std::vector
<StringSettingOption
>& list
,
240 std::string
& current
,
242 static void SettingOptions24HourClockFormatsFiller(const std::shared_ptr
<const CSetting
>& setting
,
243 std::vector
<StringSettingOption
>& list
,
244 std::string
& current
,
246 static void SettingOptionsTemperatureUnitsFiller(const std::shared_ptr
<const CSetting
>& setting
,
247 std::vector
<StringSettingOption
>& list
,
248 std::string
& current
,
250 static void SettingOptionsSpeedUnitsFiller(const std::shared_ptr
<const CSetting
>& setting
,
251 std::vector
<StringSettingOption
>& list
,
252 std::string
& current
,
257 bool Load(const std::string
& strLanguage
);
259 static bool DetermineUse24HourClockFromTimeFormat(const std::string
& timeFormat
);
260 static bool DetermineUseMeridiemFromTimeFormat(const std::string
& timeFormat
);
261 static std::string
PrepareTimeFormat(const std::string
& timeFormat
, bool use24HourClock
);
262 static void AddLanguages(std::vector
<StringSettingOption
> &list
);
269 void SetTemperatureUnit(const std::string
& strUnit
);
270 void SetSpeedUnit(const std::string
& strUnit
);
271 void SetTimeZone(const std::string
& strTimeZone
);
273 class custom_numpunct
: public std::numpunct
<char>
276 custom_numpunct(const char decimal_point
, const char thousands_sep
, const std::string
& grouping
)
277 : cDecimalPoint(decimal_point
), cThousandsSep(thousands_sep
), sGroup(grouping
) {}
279 char do_decimal_point() const override
{ return cDecimalPoint
; }
280 char do_thousands_sep() const override
{ return cThousandsSep
; }
281 std::string
do_grouping() const override
{ return sGroup
; }
283 const char cDecimalPoint
;
284 const char cThousandsSep
;
285 const std::string sGroup
;
288 /*! \brief Set the locale associated with this region global.
290 Set the locale associated with this region global. This affects string
291 sorting & transformations.
293 void SetGlobalLocale();
294 std::string m_strLangLocaleName
;
295 std::string m_strLangLocaleCodeTwoChar
;
296 std::string m_strRegionLocaleName
;
297 std::string m_strName
;
298 std::string m_strDateFormatLong
;
299 std::string m_strDateFormatShort
;
300 std::string m_strTimeFormat
;
301 std::string m_strMeridiemSymbols
[2];
302 std::string m_strTimeZone
;
303 std::string m_strGrouping
;
305 char m_cThousandsSep
;
307 CTemperature::Unit m_tempUnit
;
308 CSpeed::Unit m_speedUnit
;
312 typedef std::map
<std::string
, CRegion
> MAPREGIONS
;
313 typedef std::map
<std::string
, CRegion
>::iterator ITMAPREGIONS
;
314 typedef std::pair
<std::string
, CRegion
> PAIR_REGIONS
;
315 MAPREGIONS m_regions
;
316 CRegion
* m_currentRegion
; // points to the current region
317 CRegion m_defaultRegion
; // default, will be used if no region available via langinfo.xml
318 std::locale m_systemLocale
; // current locale, matching GUI settings
319 std::locale m_originalLocale
; // original locale, without changes of collate
321 LanguageResourcePtr m_languageAddon
;
323 std::string m_strGuiCharSet
;
324 bool m_forceUnicodeFont
;
325 std::string m_strSubtitleCharSet
;
326 std::string m_strDVDMenuLanguage
;
327 std::string m_strDVDAudioLanguage
;
328 std::string m_strDVDSubtitleLanguage
;
329 std::set
<std::string
> m_sortTokens
;
331 std::string m_shortDateFormat
;
332 std::string m_longDateFormat
;
333 std::string m_timeFormat
;
334 bool m_use24HourClock
;
335 CTemperature::Unit m_temperatureUnit
;
336 CSpeed::Unit m_speedUnit
;
338 std::string m_audioLanguage
; // ISO 639-2 three char (not win32 specific)
339 std::string m_subtitleLanguage
; // ISO 639-2 three char (not win32 specific)
340 std::string m_languageCodeGeneral
; // ISO 639-2 three char (not win32-specific)
344 XBMC_GLOBAL_REF(CLangInfo
, g_langInfo
);
345 #define g_langInfo XBMC_GLOBAL_USE(CLangInfo)