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 #ifndef LIBTEXTCLASSIFIER_UTILS_I18N_LOCALE_H_ 18 #define LIBTEXTCLASSIFIER_UTILS_I18N_LOCALE_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include "utils/base/integral_types.h" 24 #include "utils/base/logging.h" 25 #include "utils/i18n/language-tag_generated.h" 26 #include "utils/strings/stringpiece.h" 27 28 namespace libtextclassifier3 { 29 30 class Locale { 31 public: 32 // Constructs the object from a valid BCP47 tag. If the tag is invalid, 33 // an object is created that gives false when IsInvalid() is called. 34 static Locale FromBCP47(const std::string& locale_tag); 35 36 // Constructs the object from a flatbuffer language tag. 37 static Locale FromLanguageTag(const LanguageTag* language_tag); 38 39 // Creates a prototypical invalid locale object. Invalid()40 static Locale Invalid() { 41 Locale locale(/*language=*/"", /*script=*/"", /*region=*/""); 42 locale.is_valid_ = false; 43 return locale; 44 } 45 Language()46 std::string Language() const { return language_; } 47 Script()48 std::string Script() const { return script_; } 49 Region()50 std::string Region() const { return region_; } 51 IsValid()52 bool IsValid() const { return is_valid_; } 53 bool IsUnknown() const; 54 55 // Returns whether any of the given locales is supported by any of the 56 // supported locales. Returns default value if the given 'locales' list, or 57 // 'supported_locales' list is empty or an unknown locale is found. 58 // Locale::FromBCP47("*") means any locale. 59 static bool IsAnyLocaleSupported(const std::vector<Locale>& locales, 60 const std::vector<Locale>& supported_locales, 61 bool default_value); 62 63 bool operator==(const Locale& locale) const; 64 bool operator!=(const Locale& locale) const; 65 bool operator<(const Locale& locale) const; 66 67 private: Locale(const std::string & language,const std::string & script,const std::string & region)68 Locale(const std::string& language, const std::string& script, 69 const std::string& region) 70 : language_(language), 71 script_(script), 72 region_(region), 73 is_valid_(true) {} 74 75 static bool IsLocaleSupported(const Locale& locale, 76 const std::vector<Locale>& supported_locales, 77 bool default_value); 78 79 std::string language_; 80 std::string script_; 81 std::string region_; 82 bool is_valid_; 83 }; 84 85 // Pretty-printing function for Locale. 86 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream, 87 const Locale& locale); 88 89 // Parses a comma-separated list of BCP47 tags. 90 bool ParseLocales(StringPiece locales_list, std::vector<Locale>* locales); 91 92 } // namespace libtextclassifier3 93 94 #endif // LIBTEXTCLASSIFIER_UTILS_I18N_LOCALE_H_ 95