kodi
LangCodeExpander.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <map>
12 #include <string>
13 #include <vector>
14 
15 class TiXmlElement;
16 
18 {
19 public:
22 
23  enum LANGFORMATS
24  {
25  ISO_639_1,
26  ISO_639_2,
27  ENGLISH_NAME
28  };
29 
30  enum class LANG_LIST
31  {
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,
42  };
43 
44  void LoadUserCodes(const TiXmlElement* pRootElement);
45  void Clear();
46 
47  bool Lookup(const std::string& code, std::string& desc);
48  bool Lookup(const int code, std::string& desc);
49 
56  bool CompareFullLanguageNames(const std::string& lang1, const std::string& lang2);
57 
64  bool CompareISO639Codes(const std::string& code1, const std::string& code2);
65 
73  bool ConvertToISO6391(const std::string& lang, std::string& code);
74 
81  std::string ConvertToISO6392B(const std::string& lang);
82 
89  static bool ConvertISO6391ToISO6392B(const std::string& strISO6391, std::string& strISO6392B, bool checkWin32Locales = false);
90 
99  bool ConvertToISO6392B(const std::string& strCharCode, std::string& strISO6392B, bool checkWin32Locales = false);
100 
109  bool ConvertToISO6392T(const std::string& strCharCode, std::string& strISO6392T, bool checkWin32Locales = false);
110 
117  std::string ConvertToISO6392T(const std::string& lang);
118 
119  /*
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
126  */
127  static std::string FindLanguageCodeWithSubtag(const std::string& str);
128 
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
133 
134  /*
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
139  */
140  std::vector<std::string> GetLanguageNames(LANGFORMATS format = ISO_639_1,
141  LANG_LIST list = LANG_LIST::DEFAULT);
142 
143 protected:
144  /*
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.
149  */
150  static std::string CodeToString(long code);
151 
152  static bool LookupInISO639Tables(const std::string& code, std::string& desc);
153 
154  /*
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.
160  */
161  static bool LookupInLangAddons(const std::string& code, std::string& desc);
162 
163  bool LookupInUserMap(const std::string& code, std::string& desc);
164 
171  bool ReverseLookup(const std::string& desc, std::string& code);
172 
173 
179  bool LookupUserCode(const std::string& desc, std::string &userCode);
180 
181  typedef std::map<std::string, std::string> STRINGLOOKUPTABLE;
182  STRINGLOOKUPTABLE m_mapUser;
183 };
184 
185 extern CLangCodeExpander g_LangCodeExpander;
bool ReverseLookup(const std::string &desc, std::string &code)
Looks up the ISO 639-1, ISO 639-2/T, or ISO 639-2/B, whichever it finds first, code of the given engl...
Definition: LangCodeExpander.cpp:358
Definition: LangCodeExpander.h:17
bool CompareISO639Codes(const std::string &code1, const std::string &code2)
Determines if two languages given as ISO 639-1, ISO 639-2/T, or ISO 639-2/B codes represent the same ...
Definition: LangCodeExpander.cpp:546
bool ConvertToISO6391(const std::string &lang, std::string &code)
Converts a language given as 2-Char (ISO 639-1), 3-Char (ISO 639-2/T or ISO 639-2/B), or full english name string to a 2-Char (ISO 639-1) code.
Definition: LangCodeExpander.cpp:294
std::string ConvertToISO6392B(const std::string &lang)
Converts a language given as 2-Char (ISO 639-1), 3-Char (ISO 639-2/T or ISO 639-2/B), or full english name string to a 3-Char ISO 639-2/B code.
Definition: LangCodeExpander.cpp:562
bool CompareFullLanguageNames(const std::string &lang1, const std::string &lang2)
Determines if two english language names represent the same language.
Definition: LangCodeExpander.cpp:488
bool ConvertToISO6392T(const std::string &strCharCode, std::string &strISO6392T, bool checkWin32Locales=false)
Converts a language given as 2-Char (ISO 639-1), 3-Char (ISO 639-2/T or ISO 639-2/B), or full english name string to a 3-Char ISO 639-2/T code.
Definition: LangCodeExpander.cpp:217
Definition: inftrees.h:24
static bool ConvertISO6391ToISO6392B(const std::string &strISO6391, std::string &strISO6392B, bool checkWin32Locales=false)
Converts a language given as 2-Char (ISO 639-1) to a 3-Char (ISO 639-2/T) code.
Definition: LangCodeExpander.cpp:133
bool LookupUserCode(const std::string &desc, std::string &userCode)
Looks up the user defined code of the given code or language name.
Definition: LangCodeExpander.cpp:238