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