kodi
Locale.h
1 /*
2  * Copyright (C) 2015-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 <set>
12 #include <string>
13 #include <unordered_map>
14 
18 class CLocale
19 {
20 public:
21  CLocale();
22  explicit CLocale(const std::string& language);
23  CLocale(const std::string& language, const std::string& territory);
24  CLocale(const std::string& language, const std::string& territory, const std::string& codeset);
25  CLocale(const std::string& language, const std::string& territory, const std::string& codeset, const std::string& modifier);
26  ~CLocale();
27 
31  static const CLocale Empty;
32 
38  static CLocale FromString(const std::string& locale);
39 
40  bool operator==(const CLocale& other) const;
41  inline bool operator!=(const CLocale& other) const { return !(*this == other); }
42 
48  bool IsValid() const { return m_valid; }
49 
53  const std::string& GetLanguageCode() const { return m_language; }
57  const std::string& GetTerritoryCode() const { return m_territory; }
61  const std::string& GetCodeset() const { return m_codeset; }
65  const std::string& GetModifier() const { return m_modifier; }
66 
75  std::string ToString() const;
84  std::string ToStringLC() const;
93  std::string ToShortString() const;
102  std::string ToShortStringLC() const;
103 
111  bool Equals(const std::string& locale) const;
112 
123  bool Matches(const std::string& locale) const;
124 
132  std::string FindBestMatch(const std::set<std::string>& locales) const;
133 
145  std::string FindBestMatch(const std::unordered_map<std::string, std::string>& locales) const;
146 
147 private:
148  static bool CheckValidity(const std::string& language, const std::string& territory, const std::string& codeset, const std::string& modifier);
149  static bool ParseLocale(const std::string &locale, std::string &language, std::string &territory, std::string &codeset, std::string &modifier);
150 
151  void Initialize();
152 
153  int GetMatchRank(const std::string& locale) const;
154 
155  bool m_valid = false;
156  std::string m_language;
157  std::string m_territory;
158  std::string m_codeset;
159  std::string m_modifier;
160 };
161 
std::string FindBestMatch(const std::set< std::string > &locales) const
Tries to find the locale in the given list that matches this locale best.
Definition: Locale.cpp:156
const std::string & GetModifier() const
Returns the modifier of the locale.
Definition: Locale.h:65
static const CLocale Empty
Empty (and invalid) CLocale instance.
Definition: Locale.h:31
static CLocale FromString(const std::string &locale)
Parses the given string representation and turns it into a locale.
Definition: Locale.cpp:60
const std::string & GetLanguageCode() const
Returns the (lower-case) ISO 639-1 language code of the locale.
Definition: Locale.h:53
const std::string & GetCodeset() const
Returns the codeset of the locale.
Definition: Locale.h:61
std::string ToString() const
Returns the full string representation of the locale.
Definition: Locale.cpp:77
bool Equals(const std::string &locale) const
Checks if the given string representation of a locale exactly matches the locale. ...
Definition: Locale.cpp:128
std::string ToStringLC() const
Returns the full string representation of the locale in lowercase.
Definition: Locale.cpp:93
const std::string & GetTerritoryCode() const
Returns the (upper-case) ISO 3166-1 Alpha-2 territory code of the locale.
Definition: Locale.h:57
bool Matches(const std::string &locale) const
Checks if the given string representation of a locale partly matches the locale.
Definition: Locale.cpp:135
std::string ToShortStringLC() const
Returns the short string representation of the locale in lowercase.
Definition: Locale.cpp:117
bool IsValid() const
Whether the locale is valid or not.
Definition: Locale.h:48
Class representing a full locale of the form [language[_territory][.codeset][@modifier]].
Definition: Locale.h:18
std::string ToShortString() const
Returns the short string representation of the locale.
Definition: Locale.cpp:104