File: translate_ui_languages_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,454 bytes parent folder | download | duplicates (6)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_LANGUAGES_MANAGER_H_
#define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_LANGUAGES_MANAGER_H_

#include <stddef.h>

#include <string>
#include <utility>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "third_party/icu/source/i18n/unicode/coll.h"

namespace translate {

// Handles index management and querying functions for language lists used in
// the Full Page Translate and Partial Translate UIs.

// Note that the API offers a way to read/set language values through array
// indices. Such indices are only valid as long as the visual representation
// (infobar, bubble...) is in sync with the underlying language list which
// can actually change at run time (see translate_language_list.h).
// It is recommended that languages are only updated by language code to
// avoid bugs like crbug.com/555124
class TranslateUILanguagesManager {
 public:
  static const size_t kNoIndex = static_cast<size_t>(-1);

  TranslateUILanguagesManager(
      const std::vector<std::string>& language_codes,
      const std::string& source_language,
      const std::string& target_language);

  TranslateUILanguagesManager(const TranslateUILanguagesManager&) = delete;
  TranslateUILanguagesManager& operator=(const TranslateUILanguagesManager&) =
      delete;

  ~TranslateUILanguagesManager();

  // Returns the number of languages supported.
  size_t GetNumberOfLanguages() const;

  // Returns the initial source language index.
  size_t GetInitialSourceLanguageIndex() const {
    return initial_source_language_index_;
  }

  // Returns the source language index.
  size_t GetSourceLanguageIndex() const { return source_language_index_; }

  // Returns the source language code.
  std::string GetSourceLanguageCode() const;

  // Returns the target language index.
  size_t GetTargetLanguageIndex() const { return target_language_index_; }

  // Returns the target language code.
  std::string GetTargetLanguageCode() const;

  // Returns the ISO code for the language at |index|.
  std::string GetLanguageCodeAt(size_t index) const;

  // Returns the displayable name for the language at |index|.
  std::u16string GetLanguageNameAt(size_t index) const;

  // Updates the source language index.
  bool UpdateSourceLanguageIndex(size_t language_index);

  // Updates the source language and returns true if |language_code| exists in
  // the supported languages list. Returns false if it is not supported.
  bool UpdateSourceLanguage(const std::string& language_code);

  // If the language has changed, updates the target language index and returns
  // true. Returns false otherwise.
  bool UpdateTargetLanguageIndex(size_t language_index);

  // Updates the target language and returns true if |language_code| exists in
  // the supported languages list. Returns false if it is not supported.
  bool UpdateTargetLanguage(const std::string& language_code);

  static std::u16string GetUnknownLanguageDisplayName();

 private:
  // Returns a Collator object which helps to sort strings in a given locale or
  // null if unable to find the right collator.
  //
  // TODO(hajimehoshi): Write a test for icu::Collator::createInstance.
  std::unique_ptr<icu::Collator> CreateCollator(const std::string& locale);

  // ISO code (en, fr...) -> displayable name in the current locale
  typedef std::pair<std::string, std::u16string> LanguageNamePair;

  // The list of supported languages for translation.
  // The languages are sorted alphabetically based on the displayable name.
  std::vector<LanguageNamePair> languages_;

  // The index of the language selected as the page's source language before a
  // translation.
  size_t source_language_index_;

  // The index of the language that the page is in when it was first reported
  // (source_language_index_ changes if the user selects a new source language,
  // but this does not).  This is necessary to report language detection errors
  // with the right source language even if the user changed the source
  // language.
  size_t initial_source_language_index_;

  // The index of the language selected as the target language for translation.
  size_t target_language_index_;
};

}  // namespace translate

#endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_LANGUAGES_MANAGER_H_