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 typedef enum 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);
106 const std::string
& GetAudioLanguage() const;
107 // language can either be a two char language code as defined in ISO639
108 // or a three char language code
109 // or a language name in english (as used by XBMC)
110 void SetAudioLanguage(const std::string
& language
);
112 // three char language code (not win32 specific)
113 const std::string
& GetSubtitleLanguage() const;
114 // language can either be a two char language code as defined in ISO639
115 // or a three char language code
116 // or a language name in english (as used by XBMC)
117 void SetSubtitleLanguage(const std::string
& language
);
119 const std::string
GetDVDMenuLanguage() const;
120 const std::string
GetDVDAudioLanguage() const;
121 const std::string
GetDVDSubtitleLanguage() const;
122 const std::string
& GetTimeZone() const;
124 const std::string
& GetRegionLocale() const;
126 const std::locale
& GetOriginalLocale() const;
129 \brief Returns the full locale of the current language.
131 const CLocale
& GetLocale() const;
134 \brief Returns the system's current locale.
136 const std::locale
& GetSystemLocale() const { return m_systemLocale
; }
138 bool ForceUnicodeFont() const { return m_forceUnicodeFont
; }
140 const std::string
& GetDateFormat(bool bLongDate
= false) const;
141 void SetDateFormat(const std::string
& dateFormat
, bool bLongDate
= false);
142 const std::string
& GetShortDateFormat() const;
143 void SetShortDateFormat(const std::string
& shortDateFormat
);
144 const std::string
& GetLongDateFormat() const;
145 void SetLongDateFormat(const std::string
& longDateFormat
);
147 const std::string
& GetTimeFormat() const;
148 void SetTimeFormat(const std::string
& timeFormat
);
149 bool Use24HourClock() const;
150 void Set24HourClock(bool use24HourClock
);
151 void Set24HourClock(const std::string
& str24HourClock
);
152 const std::string
& GetMeridiemSymbol(MeridiemSymbol symbol
) const;
153 static const std::string
& MeridiemSymbolToString(MeridiemSymbol symbol
);
155 CTemperature::Unit
GetTemperatureUnit() const;
156 void SetTemperatureUnit(CTemperature::Unit temperatureUnit
);
157 void SetTemperatureUnit(const std::string
& temperatureUnit
);
158 const std::string
& GetTemperatureUnitString() const;
159 static const std::string
& GetTemperatureUnitString(CTemperature::Unit temperatureUnit
);
160 std::string
GetTemperatureAsString(const CTemperature
& temperature
) const;
162 CSpeed::Unit
GetSpeedUnit() const;
163 void SetSpeedUnit(CSpeed::Unit speedUnit
);
164 void SetSpeedUnit(const std::string
& speedUnit
);
165 const std::string
& GetSpeedUnitString() const;
166 static const std::string
& GetSpeedUnitString(CSpeed::Unit speedUnit
);
167 std::string
GetSpeedAsString(const CSpeed
& speed
) const;
169 void GetRegionNames(std::vector
<std::string
>& array
);
170 void SetCurrentRegion(const std::string
& strName
);
171 const std::string
& GetCurrentRegion() const;
173 std::set
<std::string
> GetSortTokens() const;
175 static std::string
GetLanguagePath() { return "resource://"; }
176 static std::string
GetLanguagePath(const std::string
&language
);
177 static std::string
GetLanguageInfoPath(const std::string
&language
);
178 bool UseLocaleCollation();
180 static void LoadTokens(const TiXmlNode
* pTokens
, std::set
<std::string
>& vecTokens
);
182 static void SettingOptionsLanguageNamesFiller(const std::shared_ptr
<const CSetting
>& setting
,
183 std::vector
<StringSettingOption
>& list
,
184 std::string
& current
,
186 static void SettingOptionsAudioStreamLanguagesFiller(
187 const std::shared_ptr
<const CSetting
>& setting
,
188 std::vector
<StringSettingOption
>& list
,
189 std::string
& current
,
191 static void SettingOptionsSubtitleStreamLanguagesFiller(
192 const std::shared_ptr
<const CSetting
>& setting
,
193 std::vector
<StringSettingOption
>& list
,
194 std::string
& current
,
196 static void SettingOptionsSubtitleDownloadlanguagesFiller(
197 const std::shared_ptr
<const CSetting
>& setting
,
198 std::vector
<StringSettingOption
>& list
,
199 std::string
& current
,
201 static void SettingOptionsISO6391LanguagesFiller(const std::shared_ptr
<const CSetting
>& setting
,
202 std::vector
<StringSettingOption
>& list
,
203 std::string
& current
,
205 static void SettingOptionsRegionsFiller(const std::shared_ptr
<const CSetting
>& setting
,
206 std::vector
<StringSettingOption
>& list
,
207 std::string
& current
,
209 static void SettingOptionsShortDateFormatsFiller(const std::shared_ptr
<const CSetting
>& setting
,
210 std::vector
<StringSettingOption
>& list
,
211 std::string
& current
,
213 static void SettingOptionsLongDateFormatsFiller(const std::shared_ptr
<const CSetting
>& setting
,
214 std::vector
<StringSettingOption
>& list
,
215 std::string
& current
,
217 static void SettingOptionsTimeFormatsFiller(const std::shared_ptr
<const CSetting
>& setting
,
218 std::vector
<StringSettingOption
>& list
,
219 std::string
& current
,
221 static void SettingOptions24HourClockFormatsFiller(const std::shared_ptr
<const CSetting
>& setting
,
222 std::vector
<StringSettingOption
>& list
,
223 std::string
& current
,
225 static void SettingOptionsTemperatureUnitsFiller(const std::shared_ptr
<const CSetting
>& setting
,
226 std::vector
<StringSettingOption
>& list
,
227 std::string
& current
,
229 static void SettingOptionsSpeedUnitsFiller(const std::shared_ptr
<const CSetting
>& setting
,
230 std::vector
<StringSettingOption
>& list
,
231 std::string
& current
,
236 bool Load(const std::string
& strLanguage
);
238 static bool DetermineUse24HourClockFromTimeFormat(const std::string
& timeFormat
);
239 static bool DetermineUseMeridiemFromTimeFormat(const std::string
& timeFormat
);
240 static std::string
PrepareTimeFormat(const std::string
& timeFormat
, bool use24HourClock
);
241 static void AddLanguages(std::vector
<StringSettingOption
> &list
);
248 void SetTemperatureUnit(const std::string
& strUnit
);
249 void SetSpeedUnit(const std::string
& strUnit
);
250 void SetTimeZone(const std::string
& strTimeZone
);
252 class custom_numpunct
: public std::numpunct
<char>
255 custom_numpunct(const char decimal_point
, const char thousands_sep
, const std::string
& grouping
)
256 : cDecimalPoint(decimal_point
), cThousandsSep(thousands_sep
), sGroup(grouping
) {}
258 char do_decimal_point() const override
{ return cDecimalPoint
; }
259 char do_thousands_sep() const override
{ return cThousandsSep
; }
260 std::string
do_grouping() const override
{ return sGroup
; }
262 const char cDecimalPoint
;
263 const char cThousandsSep
;
264 const std::string sGroup
;
267 /*! \brief Set the locale associated with this region global.
269 Set the locale associated with this region global. This affects string
270 sorting & transformations.
272 void SetGlobalLocale();
273 std::string m_strLangLocaleName
;
274 std::string m_strLangLocaleCodeTwoChar
;
275 std::string m_strRegionLocaleName
;
276 std::string m_strName
;
277 std::string m_strDateFormatLong
;
278 std::string m_strDateFormatShort
;
279 std::string m_strTimeFormat
;
280 std::string m_strMeridiemSymbols
[2];
281 std::string m_strTimeZone
;
282 std::string m_strGrouping
;
284 char m_cThousandsSep
;
286 CTemperature::Unit m_tempUnit
;
287 CSpeed::Unit m_speedUnit
;
291 typedef std::map
<std::string
, CRegion
> MAPREGIONS
;
292 typedef std::map
<std::string
, CRegion
>::iterator ITMAPREGIONS
;
293 typedef std::pair
<std::string
, CRegion
> PAIR_REGIONS
;
294 MAPREGIONS m_regions
;
295 CRegion
* m_currentRegion
; // points to the current region
296 CRegion m_defaultRegion
; // default, will be used if no region available via langinfo.xml
297 std::locale m_systemLocale
; // current locale, matching GUI settings
298 std::locale m_originalLocale
; // original locale, without changes of collate
300 LanguageResourcePtr m_languageAddon
;
302 std::string m_strGuiCharSet
;
303 bool m_forceUnicodeFont
;
304 std::string m_strSubtitleCharSet
;
305 std::string m_strDVDMenuLanguage
;
306 std::string m_strDVDAudioLanguage
;
307 std::string m_strDVDSubtitleLanguage
;
308 std::set
<std::string
> m_sortTokens
;
310 std::string m_shortDateFormat
;
311 std::string m_longDateFormat
;
312 std::string m_timeFormat
;
313 bool m_use24HourClock
;
314 CTemperature::Unit m_temperatureUnit
;
315 CSpeed::Unit m_speedUnit
;
317 std::string m_audioLanguage
;
318 std::string m_subtitleLanguage
;
319 // this is the general (not win32-specific) three char language code
320 std::string m_languageCodeGeneral
;
324 XBMC_GLOBAL_REF(CLangInfo
, g_langInfo
);
325 #define g_langInfo XBMC_GLOBAL_USE(CLangInfo)