language_selector.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  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. #ifndef BASE_WIN_EMBEDDED_I18N_LANGUAGE_SELECTOR_H_
  8. #define BASE_WIN_EMBEDDED_I18N_LANGUAGE_SELECTOR_H_
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/base_export.h"
  13. #include "base/containers/span.h"
  14. #include "base/macros.h"
  15. #include "base/strings/string_piece.h"
  16. namespace base {
  17. namespace win {
  18. namespace i18n {
  19. // Selects a language from a set of available translations based on the user's
  20. // preferred language list. An optional preferred langauge may be provided to
  21. // override selection should a corresponding translation be available.
  22. class BASE_EXPORT LanguageSelector {
  23. public:
  24. using LangToOffset = std::pair<WStringPiece, int>;
  25. // Constructor to be used for users of this class that will provide the actual
  26. // language offsets that will be used.
  27. // |preferred_language| is an optional language used to as higher priority
  28. // language when determining the matched language. This languages will
  29. // take precedence over the system defined languages.
  30. // |languages_to_offset_begin| and |languages_to_offset_end| point to a sorted
  31. // array of language identifiers (and their offsets) for which translations
  32. // are available.
  33. LanguageSelector(WStringPiece preferred_language,
  34. span<const LangToOffset> languages_to_offset);
  35. // Constructor for testing purposes.
  36. // |candidates| is a list of all candiate languages that can be used to
  37. // determine which langauge to use.
  38. // |languages_to_offset_begin| and |languages_to_offset_end| point to a sorted
  39. // array of language identifiers (and their offsets) for which translations
  40. // are available.
  41. LanguageSelector(const std::vector<std::wstring>& candidates,
  42. span<const LangToOffset> languages_to_offset);
  43. ~LanguageSelector();
  44. // The offset of the matched language (i.e., IDS_L10N_OFFSET_*).
  45. int offset() const { return selected_offset_; }
  46. // The full name of the candidate language for which a match was found.
  47. const std::wstring& matched_candidate() const { return matched_candidate_; }
  48. // The name of the selected translation.
  49. const std::wstring& selected_translation() const {
  50. return selected_language_;
  51. }
  52. private:
  53. std::wstring matched_candidate_;
  54. std::wstring selected_language_;
  55. int selected_offset_;
  56. DISALLOW_COPY_AND_ASSIGN(LanguageSelector);
  57. };
  58. } // namespace i18n
  59. } // namespace win
  60. } // namespace base
  61. #endif // BASE_WIN_EMBEDDED_I18N_LANGUAGE_SELECTOR_H_