1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
* Copyright (C) 2015-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
#include <set>
#include <string>
#include <unordered_map>
/*!
\brief Class representing a full locale of the form `[language[_territory][.codeset][@modifier]]`.
*/
class CLocale
{
public:
CLocale();
explicit CLocale(const std::string& language);
CLocale(const std::string& language, const std::string& territory);
CLocale(const std::string& language, const std::string& territory, const std::string& codeset);
CLocale(const std::string& language, const std::string& territory, const std::string& codeset, const std::string& modifier);
~CLocale();
/*!
\brief Empty (and invalid) CLocale instance.
*/
static const CLocale Empty;
/*!
\brief Parses the given string representation and turns it into a locale.
\param locale String representation of a locale
*/
static CLocale FromString(const std::string& locale);
bool operator==(const CLocale& other) const;
inline bool operator!=(const CLocale& other) const { return !(*this == other); }
/*!
\brief Whether the locale is valid or not.
\details A locale is considered valid if at least the language code is set.
*/
bool IsValid() const { return m_valid; }
/*!
\brief Returns the (lower-case) ISO 639-1 language code of the locale.
*/
const std::string& GetLanguageCode() const { return m_language; }
/*!
\brief Returns the (upper-case) ISO 3166-1 Alpha-2 territory code of the locale.
*/
const std::string& GetTerritoryCode() const { return m_territory; }
/*!
\brief Returns the codeset of the locale.
*/
const std::string& GetCodeset() const { return m_codeset; }
/*!
\brief Returns the modifier of the locale.
*/
const std::string& GetModifier() const { return m_modifier; }
/*!
\brief Returns the full string representation of the locale.
\details The format of the string representation is
`[language[_territory][.codeset][@modifier]]` where the language is
represented as a (lower-case) two character ISO 639-1 code and the territory
is represented as a (upper-case) two character ISO 3166-1 Alpha-2 code.
*/
std::string ToString() const;
/*!
\brief Returns the full string representation of the locale in lowercase.
\details The format of the string representation is
`language[_territory][.codeset][@modifier]]` where the language is
represented as a two character ISO 639-1 code and the territory is
represented as a two character ISO 3166-1 Alpha-2 code.
*/
std::string ToStringLC() const;
/*!
\brief Returns the short string representation of the locale.
\details The format of the short string representation is
`[language[_territory]` where the language is represented as a (lower-case)
two character ISO 639-1 code and the territory is represented as a
(upper-case) two character ISO 3166-1 Alpha-2 code.
*/
std::string ToShortString() const;
/*!
\brief Returns the short string representation of the locale in lowercase.
\details The format of the short string representation is
`[language[_territory]` where the language is represented as a two character
ISO 639-1 code and the territory is represented as a two character
ISO 3166-1 Alpha-2 code.
*/
std::string ToShortStringLC() const;
/*!
\brief Checks if the given string representation of a locale exactly matches
the locale.
\param locale String representation of a locale
\return True if the string representation matches the locale, false otherwise.
*/
bool Equals(const std::string& locale) const;
/*!
\brief Checks if the given string representation of a locale partly matches
the locale.
\details Partial matching means that every available locale part needs to
match the same locale part of the other locale if present.
\param locale String representation of a locale
\return True if the string representation matches the locale, false otherwise.
*/
bool Matches(const std::string& locale) const;
/*!
\brief Tries to find the locale in the given list that matches this locale
best.
\param locales List of string representations of locales
\return Best matching locale from the given list or empty string.
*/
std::string FindBestMatch(const std::set<std::string>& locales) const;
/*!
\brief Tries to find the locale in the given list that matches this locale
best.
\param locales Map list of string representations of locales with first as
locale identifier
\return Best matching locale from the given list or empty string.
\remark Used from \ref CAddonInfo::GetTranslatedText to prevent copy from map
to set.
*/
std::string FindBestMatch(const std::unordered_map<std::string, std::string>& locales) const;
private:
static bool CheckValidity(const std::string& language, const std::string& territory, const std::string& codeset, const std::string& modifier);
static bool ParseLocale(const std::string &locale, std::string &language, std::string &territory, std::string &codeset, std::string &modifier);
void Initialize();
int GetMatchRank(const std::string& locale) const;
bool m_valid = false;
std::string m_language;
std::string m_territory;
std::string m_codeset;
std::string m_modifier;
};
|