xref: /aosp_15_r20/external/cronet/base/win/embedded_i18n/language_selector.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file declares a helper class for selecting a supported language from a
6 // set of candidates.
7 
8 #ifndef BASE_WIN_EMBEDDED_I18N_LANGUAGE_SELECTOR_H_
9 #define BASE_WIN_EMBEDDED_I18N_LANGUAGE_SELECTOR_H_
10 
11 #include <string>
12 #include <string_view>
13 #include <utility>
14 #include <vector>
15 
16 #include "base/base_export.h"
17 #include "base/containers/span.h"
18 
19 namespace base {
20 namespace win {
21 namespace i18n {
22 
23 // Selects a language from a set of available translations based on the user's
24 // preferred language list. An optional preferred language may be provided to
25 // override selection should a corresponding translation be available.
26 class BASE_EXPORT LanguageSelector {
27  public:
28   using LangToOffset = std::pair<std::wstring_view, size_t>;
29 
30   // Constructor to be used for users of this class that will provide the actual
31   // language offsets that will be used.
32   // |preferred_language| is an optional language used to as higher priority
33   // language when determining the matched language. This languages will
34   // take precedence over the system defined languages.
35   // |languages_to_offset_begin| and |languages_to_offset_end| point to a sorted
36   // array of language identifiers (and their offsets) for which translations
37   // are available.
38   LanguageSelector(std::wstring_view preferred_language,
39                    span<const LangToOffset> languages_to_offset);
40 
41   // Constructor for testing purposes.
42   // |candidates| is a list of all candidate languages that can be used to
43   // determine which language to use.
44   // |languages_to_offset_begin| and |languages_to_offset_end| point to a sorted
45   // array of language identifiers (and their offsets) for which translations
46   // are available.
47   LanguageSelector(const std::vector<std::wstring>& candidates,
48                    span<const LangToOffset> languages_to_offset);
49 
50   LanguageSelector(const LanguageSelector&) = delete;
51   LanguageSelector& operator=(const LanguageSelector&) = delete;
52 
53   ~LanguageSelector();
54 
55   // The offset of the matched language (i.e., IDS_L10N_OFFSET_*).
offset()56   size_t offset() const { return selected_offset_; }
57 
58   // The full name of the candidate language for which a match was found.
matched_candidate()59   const std::wstring& matched_candidate() const { return matched_candidate_; }
60 
61   // The name of the selected translation.
selected_translation()62   const std::wstring& selected_translation() const {
63     return selected_language_;
64   }
65 
66  private:
67   std::wstring matched_candidate_;
68   std::wstring selected_language_;
69   size_t selected_offset_;
70 };
71 
72 }  // namespace i18n
73 }  // namespace win
74 }  // namespace base
75 
76 #endif  // BASE_WIN_EMBEDDED_I18N_LANGUAGE_SELECTOR_H_
77