huangcm
2025-02-28 b45e871a67cd1272e3da9ba5bd383f832b0f1824
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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifndef LIBTEXTCLASSIFIER_UTILS_I18N_LOCALE_H_
#define LIBTEXTCLASSIFIER_UTILS_I18N_LOCALE_H_
 
#include <string>
#include <vector>
 
#include "utils/base/integral_types.h"
#include "utils/base/logging.h"
#include "utils/strings/stringpiece.h"
 
namespace libtextclassifier3 {
 
class Locale {
 public:
  // Constructs the object from a valid BCP47 tag. If the tag is invalid,
  // an object is created that gives false when IsInvalid() is called.
  static Locale FromBCP47(const std::string& locale_tag);
 
  // Creates a prototypical invalid locale object.
  static Locale Invalid() {
    Locale locale(/*language=*/"", /*script=*/"", /*region=*/"");
    locale.is_valid_ = false;
    return locale;
  }
 
  std::string Language() const { return language_; }
 
  std::string Script() const { return script_; }
 
  std::string Region() const { return region_; }
 
  bool IsValid() const { return is_valid_; }
  bool IsUnknown() const;
 
  // Returns whether any of the given locales is supported by any of the
  // supported locales. Returns default value if the given 'locales' list, or
  // 'supported_locales' list is empty or an unknown locale is found.
  // Locale::FromBCP47("*") means any locale.
  static bool IsAnyLocaleSupported(const std::vector<Locale>& locales,
                                   const std::vector<Locale>& supported_locales,
                                   bool default_value);
 
 private:
  Locale(const std::string& language, const std::string& script,
         const std::string& region)
      : language_(language),
        script_(script),
        region_(region),
        is_valid_(true) {}
 
  static bool IsLocaleSupported(const Locale& locale,
                                const std::vector<Locale>& supported_locales,
                                bool default_value);
 
  std::string language_;
  std::string script_;
  std::string region_;
  bool is_valid_;
};
 
// Pretty-printing function for Locale.
logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
                                         const Locale& locale);
 
// Parses a comma-separated list of BCP47 tags.
bool ParseLocales(StringPiece locales_list, std::vector<Locale>* locales);
 
}  // namespace libtextclassifier3
 
#endif  // LIBTEXTCLASSIFIER_UTILS_I18N_LOCALE_H_