1 /* 2 * Copyright (C) 2017 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_UTIL_I18N_LOCALE_H_ 18 #define LIBTEXTCLASSIFIER_UTIL_I18N_LOCALE_H_ 19 20 #include <string> 21 22 #include "util/base/integral_types.h" 23 24 namespace libtextclassifier2 { 25 26 class Locale { 27 public: 28 // Constructs the object from a valid BCP47 tag. If the tag is invalid, 29 // an object is created that gives false when IsInvalid() is called. 30 static Locale FromBCP47(const std::string& locale_tag); 31 32 // Creates a prototypical invalid locale object. Invalid()33 static Locale Invalid() { 34 Locale locale(/*language=*/"", /*script=*/"", /*region=*/""); 35 locale.is_valid_ = false; 36 return locale; 37 } 38 Language()39 std::string Language() const { return language_; } 40 Script()41 std::string Script() const { return script_; } 42 Region()43 std::string Region() const { return region_; } 44 IsValid()45 bool IsValid() const { return is_valid_; } 46 47 private: Locale(const std::string & language,const std::string & script,const std::string & region)48 Locale(const std::string& language, const std::string& script, 49 const std::string& region) 50 : language_(language), 51 script_(script), 52 region_(region), 53 is_valid_(true) {} 54 55 std::string language_; 56 std::string script_; 57 std::string region_; 58 bool is_valid_; 59 }; 60 61 } // namespace libtextclassifier2 62 63 #endif // LIBTEXTCLASSIFIER_UTIL_I18N_LOCALE_H_ 64