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/compiler_specific.h" 18 #include "base/containers/span.h" 19 20 namespace base { 21 namespace win { 22 namespace i18n { 23 24 // Selects a language from a set of available translations based on the user's 25 // preferred language list. An optional preferred language may be provided to 26 // override selection should a corresponding translation be available. 27 class BASE_EXPORT LanguageSelector { 28 public: 29 using LangToOffset = std::pair<std::wstring_view, size_t>; 30 31 // Constructor to be used for users of this class that will provide the actual 32 // language offsets that will be used. 33 // |preferred_language| is an optional language used to as higher priority 34 // language when determining the matched language. This languages will 35 // take precedence over the system defined languages. 36 // |languages_to_offset_begin| and |languages_to_offset_end| point to a sorted 37 // array of language identifiers (and their offsets) for which translations 38 // are available. 39 LanguageSelector(std::wstring_view preferred_language, 40 span<const LangToOffset> languages_to_offset); 41 42 // Constructor for testing purposes. 43 // |candidates| is a list of all candidate languages that can be used to 44 // determine which language to use. 45 // |languages_to_offset_begin| and |languages_to_offset_end| point to a sorted 46 // array of language identifiers (and their offsets) for which translations 47 // are available. 48 LanguageSelector(const std::vector<std::wstring>& candidates, 49 span<const LangToOffset> languages_to_offset); 50 51 LanguageSelector(const LanguageSelector&) = delete; 52 LanguageSelector& operator=(const LanguageSelector&) = delete; 53 54 ~LanguageSelector(); 55 56 // The offset of the matched language (i.e., IDS_L10N_OFFSET_*). offset()57 size_t offset() const { return selected_offset_; } 58 59 // The full name of the candidate language for which a match was found. matched_candidate()60 const std::wstring& matched_candidate() const LIFETIME_BOUND { 61 return matched_candidate_; 62 } 63 64 // The name of the selected translation. selected_translation()65 const std::wstring& selected_translation() const LIFETIME_BOUND { 66 return selected_language_; 67 } 68 69 private: 70 std::wstring matched_candidate_; 71 std::wstring selected_language_; 72 size_t selected_offset_; 73 }; 74 75 } // namespace i18n 76 } // namespace win 77 } // namespace base 78 79 #endif // BASE_WIN_EMBEDDED_I18N_LANGUAGE_SELECTOR_H_ 80