Merge pull request #26148 from ksooo/fix-secondstotimestring-warning
[xbmc.git] / xbmc / utils / LangCodeExpander.h
blob80f02d7649c3086485e6cde3396a7f7908e3518c
1 /*
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.
7 */
9 #pragma once
11 #include <map>
12 #include <string>
13 #include <vector>
15 class TiXmlElement;
17 class CLangCodeExpander
19 public:
20 CLangCodeExpander();
21 ~CLangCodeExpander();
23 enum LANGFORMATS
25 ISO_639_1,
26 ISO_639_2,
27 ENGLISH_NAME
30 enum class LANG_LIST
32 // Standard ISO
33 DEFAULT,
34 // Standard ISO + Language addons
35 INCLUDE_ADDONS,
36 // Standard ISO + User defined
37 // (User defined can override language name of existing codes)
38 INCLUDE_USERDEFINED,
39 // Standard ISO + Language addons + User defined
40 // (User defined can override language name of existing codes)
41 INCLUDE_ADDONS_USERDEFINED,
44 void LoadUserCodes(const TiXmlElement* pRootElement);
45 void Clear();
47 bool Lookup(const std::string& code, std::string& desc);
48 bool Lookup(const int code, std::string& desc);
50 /** \brief Determines if two english language names represent the same language.
51 * \param[in] lang1 The first language string to compare given as english language name.
52 * \param[in] lang2 The second language string to compare given as english language name.
53 * \return true if the two language strings represent the same language, false otherwise.
54 * For example "Abkhaz" and "Abkhazian" represent the same language.
56 bool CompareFullLanguageNames(const std::string& lang1, const std::string& lang2);
58 /** \brief Determines if two languages given as ISO 639-1, ISO 639-2/T, or ISO 639-2/B codes represent the same language.
59 * \param[in] code1 The first language to compare given as ISO 639-1, ISO 639-2/T, or ISO 639-2/B code.
60 * \param[in] code2 The second language to compare given as ISO 639-1, ISO 639-2/T, or ISO 639-2/B code.
61 * \return true if the two language codes represent the same language, false otherwise.
62 * For example "ger", "deu" and "de" represent the same language.
64 bool CompareISO639Codes(const std::string& code1, const std::string& code2);
66 /** \brief Converts a language given as 2-Char (ISO 639-1),
67 * 3-Char (ISO 639-2/T or ISO 639-2/B),
68 * or full english name string to a 2-Char (ISO 639-1) code.
69 * \param[out] code The 2-Char language code of the given language lang.
70 * \param[in] lang The language that should be converted.
71 * \return true if the conversion succeeded, false otherwise.
73 bool ConvertToISO6391(const std::string& lang, std::string& code);
75 /** \brief Converts a language given as 2-Char (ISO 639-1),
76 * 3-Char (ISO 639-2/T or ISO 639-2/B),
77 * or full english name string to a 3-Char ISO 639-2/B code.
78 * \param[in] lang The language that should be converted.
79 * \return The 3-Char ISO 639-2/B code of lang if that code exists, lang otherwise.
81 std::string ConvertToISO6392B(const std::string& lang);
83 /** \brief Converts a language given as 2-Char (ISO 639-1) to a 3-Char (ISO 639-2/T) code.
84 * \param[in] strISO6391 The language that should be converted.
85 * \param[out] strISO6392B The 3-Char (ISO 639-2/B) language code of the given language strISO6391.
86 * \param[in] checkWin32Locales Whether to also check WIN32 specific language codes.
87 * \return true if the conversion succeeded, false otherwise.
89 static bool ConvertISO6391ToISO6392B(const std::string& strISO6391, std::string& strISO6392B, bool checkWin32Locales = false);
91 /** \brief Converts a language given as 2-Char (ISO 639-1),
92 * 3-Char (ISO 639-2/T or ISO 639-2/B),
93 * or full english name string to a 3-Char ISO 639-2/T code.
94 * \param[in] strCharCode The language that should be converted.
95 * \param[out] strISO6392B The 3-Char (ISO 639-2/B) language code of the given language strISO6391.
96 * \param[in] checkWin32Locales Whether to also check WIN32 specific language codes.
97 * \return true if the conversion succeeded, false otherwise.
99 bool ConvertToISO6392B(const std::string& strCharCode, std::string& strISO6392B, bool checkWin32Locales = false);
101 /** \brief Converts a language given as 2-Char (ISO 639-1),
102 * 3-Char (ISO 639-2/T or ISO 639-2/B),
103 * or full english name string to a 3-Char ISO 639-2/T code.
104 * \param[in] strCharCode The language that should be converted.
105 * \param[out] strISO6392T The 3-Char (ISO 639-2/T) language code of the given language strISO6391.
106 * \param[in] checkWin32Locales Whether to also check WIN32 specific language codes.
107 * \return true if the conversion succeeded, false otherwise.
109 bool ConvertToISO6392T(const std::string& strCharCode, std::string& strISO6392T, bool checkWin32Locales = false);
111 /** \brief Converts a language given as 2-Char (ISO 639-1),
112 * 3-Char (ISO 639-2/T or ISO 639-2/B),
113 * or full english name string to a 3-Char ISO 639-2/T code.
114 * \param[in] lang The language that should be converted.
115 * \return The 3-Char ISO 639-2/T code of lang if that code exists, lang otherwise.
117 std::string ConvertToISO6392T(const std::string& lang);
120 * \brief Find a language code with subtag (e.g. zh-tw, zh-Hans) in to a string.
121 * This function find a limited set of IETF BCP47 specs, so:
122 * language tag + region subtag, or, language tag + script subtag.
123 * The language code can be found if wrapped by curly brackets e.g. {pt-br}.
124 * \param str The string where find the language code.
125 * \return The language code found in the string, otherwise empty string
127 static std::string FindLanguageCodeWithSubtag(const std::string& str);
129 #ifdef TARGET_WINDOWS
130 static bool ConvertISO31661Alpha2ToISO31661Alpha3(const std::string& strISO31661Alpha2, std::string& strISO31661Alpha3);
131 static bool ConvertWindowsLanguageCodeToISO6392B(const std::string& strWindowsLanguageCode, std::string& strISO6392B);
132 #endif
135 * \brief Get the list of language names.
136 * \param format [OPT] The format type.
137 * \param list [OPT] The type of language list to retrieve.
138 * \return The languages
140 std::vector<std::string> GetLanguageNames(LANGFORMATS format = ISO_639_1,
141 LANG_LIST list = LANG_LIST::DEFAULT);
143 protected:
145 * \brief Converts a language code given as a long, see #MAKECODE(a, b, c, d)
146 * to its string representation.
147 * \param[in] code The language code given as a long, see #MAKECODE(a, b, c, d).
148 * \return The string representation of the given language code code.
150 static std::string CodeToString(long code);
152 static bool LookupInISO639Tables(const std::string& code, std::string& desc);
155 * \brief Looks up the language description for given language code
156 * in to the installed language addons.
157 * \param[in] code The language code for which description is looked for.
158 * \param[out] desc The english language name.
159 * \return true if the language description was found, false otherwise.
161 static bool LookupInLangAddons(const std::string& code, std::string& desc);
163 bool LookupInUserMap(const std::string& code, std::string& desc);
165 /** \brief Looks up the ISO 639-1, ISO 639-2/T, or ISO 639-2/B, whichever it finds first,
166 * code of the given english language name.
167 * \param[in] desc The english language name for which a code is looked for.
168 * \param[out] code The ISO 639-1, ISO 639-2/T, or ISO 639-2/B code of the given language desc.
169 * \return true if the a code was found, false otherwise.
171 bool ReverseLookup(const std::string& desc, std::string& code);
174 /** \brief Looks up the user defined code of the given code or language name.
175 * \param[in] desc The language code or name that should be converted.
176 * \param[out] userCode The user defined language code of the given language desc.
177 * \return true if desc was found, false otherwise.
179 bool LookupUserCode(const std::string& desc, std::string &userCode);
181 typedef std::map<std::string, std::string> STRINGLOOKUPTABLE;
182 STRINGLOOKUPTABLE m_mapUser;
185 extern CLangCodeExpander g_LangCodeExpander;