1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "utils/i18n/locale.h"
18
19 #include "utils/strings/split.h"
20
21 namespace libtextclassifier3 {
22
23 namespace {
24 constexpr const char* kAnyMatch = "*";
25
26 // BCP 47 code for "Undetermined Language".
27 constexpr const char* kUnknownLanguageCode = "und";
28
CheckLanguage(StringPiece language)29 bool CheckLanguage(StringPiece language) {
30 if (language.size() == 1 && language.data()[0] == '*') {
31 return true;
32 }
33
34 if (language.size() != 2 && language.size() != 3) {
35 return false;
36 }
37
38 // Needs to be all lowercase.
39 for (int i = 0; i < language.size(); ++i) {
40 if (!std::islower(language[i])) {
41 return false;
42 }
43 }
44
45 return true;
46 }
47
CheckScript(StringPiece script)48 bool CheckScript(StringPiece script) {
49 if (script.size() != 4) {
50 return false;
51 }
52
53 if (!std::isupper(script[0])) {
54 return false;
55 }
56
57 // Needs to be all lowercase.
58 for (int i = 1; i < script.size(); ++i) {
59 if (!std::islower(script[i])) {
60 return false;
61 }
62 }
63
64 return true;
65 }
66
CheckRegion(StringPiece region)67 bool CheckRegion(StringPiece region) {
68 if (region.size() == 2) {
69 return std::isupper(region[0]) && std::isupper(region[1]);
70 } else if (region.size() == 3) {
71 return std::isdigit(region[0]) && std::isdigit(region[1]) &&
72 std::isdigit(region[2]);
73 } else {
74 return false;
75 }
76 }
77
78 } // namespace
79
FromBCP47(const std::string & locale_tag)80 Locale Locale::FromBCP47(const std::string& locale_tag) {
81 std::vector<StringPiece> parts = strings::Split(locale_tag, '-');
82 if (parts.empty()) {
83 return Locale::Invalid();
84 }
85
86 auto parts_it = parts.begin();
87 StringPiece language = *parts_it;
88 if (!CheckLanguage(language)) {
89 return Locale::Invalid();
90 }
91 ++parts_it;
92
93 StringPiece script;
94 if (parts_it != parts.end()) {
95 script = *parts_it;
96 if (!CheckScript(script)) {
97 script = "";
98 } else {
99 ++parts_it;
100 }
101 }
102
103 StringPiece region;
104 if (parts_it != parts.end()) {
105 region = *parts_it;
106 if (!CheckRegion(region)) {
107 region = "";
108 } else {
109 ++parts_it;
110 }
111 }
112
113 // NOTE: We don't parse the rest of the BCP47 tag here even if specified.
114
115 return Locale(language.ToString(), script.ToString(), region.ToString());
116 }
117
IsUnknown() const118 bool Locale::IsUnknown() const {
119 return is_valid_ && language_ == kUnknownLanguageCode;
120 }
121
IsLocaleSupported(const Locale & locale,const std::vector<Locale> & supported_locales,bool default_value)122 bool Locale::IsLocaleSupported(const Locale& locale,
123 const std::vector<Locale>& supported_locales,
124 bool default_value) {
125 if (!locale.IsValid()) {
126 return false;
127 }
128 if (locale.IsUnknown()) {
129 return default_value;
130 }
131 for (const Locale& supported_locale : supported_locales) {
132 if (!supported_locale.IsValid()) {
133 continue;
134 }
135 const bool language_matches =
136 supported_locale.Language().empty() ||
137 supported_locale.Language() == kAnyMatch ||
138 supported_locale.Language() == locale.Language();
139 const bool script_matches = supported_locale.Script().empty() ||
140 supported_locale.Script() == kAnyMatch ||
141 locale.Script().empty() ||
142 supported_locale.Script() == locale.Script();
143 const bool region_matches = supported_locale.Region().empty() ||
144 supported_locale.Region() == kAnyMatch ||
145 locale.Region().empty() ||
146 supported_locale.Region() == locale.Region();
147 if (language_matches && script_matches && region_matches) {
148 return true;
149 }
150 }
151 return false;
152 }
153
IsAnyLocaleSupported(const std::vector<Locale> & locales,const std::vector<Locale> & supported_locales,bool default_value)154 bool Locale::IsAnyLocaleSupported(const std::vector<Locale>& locales,
155 const std::vector<Locale>& supported_locales,
156 bool default_value) {
157 if (locales.empty()) {
158 return default_value;
159 }
160 if (supported_locales.empty()) {
161 return default_value;
162 }
163 for (const Locale& locale : locales) {
164 if (IsLocaleSupported(locale, supported_locales, default_value)) {
165 return true;
166 }
167 }
168 return false;
169 }
170
operator <<(logging::LoggingStringStream & stream,const Locale & locale)171 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
172 const Locale& locale) {
173 return stream << "Locale(language=" << locale.Language()
174 << ", script=" << locale.Script()
175 << ", region=" << locale.Region()
176 << ", is_valid=" << locale.IsValid()
177 << ", is_unknown=" << locale.IsUnknown() << ")";
178 }
179
ParseLocales(StringPiece locales_list,std::vector<Locale> * locales)180 bool ParseLocales(StringPiece locales_list, std::vector<Locale>* locales) {
181 for (const auto& locale_str : strings::Split(locales_list, ',')) {
182 const Locale locale = Locale::FromBCP47(locale_str.ToString());
183 if (!locale.IsValid()) {
184 TC3_LOG(ERROR) << "Invalid locale " << locale_str.ToString();
185 return false;
186 }
187 locales->push_back(locale);
188 }
189 return true;
190 }
191
192 } // namespace libtextclassifier3
193