Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: ios/chrome/browser/voice/speech_input_locale_config_impl.mm

Issue 2425713002: [ios] Adds configuration helper files for voice search. (Closed)
Patch Set: Review and file moves. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "ios/chrome/browser/voice/speech_input_locale_config_impl.h"
6
7 #import <Foundation/Foundation.h>
8
9 #include "base/mac/bundle_locations.h"
10 #include "base/mac/foundation_util.h"
11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/sys_string_conversions.h"
14 #import "ios/chrome/browser/voice/speech_input_locale_match_config.h"
15 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h"
16 #include "ios/public/provider/chrome/browser/voice/voice_search_language.h"
17
18 namespace {
19
20 // Returns the language portion of |locale_code|.
21 std::string GetLanguageComponentForLocaleCode(const std::string& locale_code) {
22 std::vector<std::string> tokens = base::SplitString(
23 locale_code, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
24 if (tokens.empty())
25 return std::string();
26 return tokens[0];
27 }
28
29 // Use "en-US" as a default value.
30 const char kEnglishUS[] = "en-US";
31
32 // Converts |locale| to its canonical form and returns it as a std::string.
33 std::string GetCanonicalLocaleForLocale(NSString* locale_code) {
34 std::string locale = base::SysNSStringToUTF8(
35 [NSLocale canonicalLocaleIdentifierFromString:locale_code]);
36 return locale;
37 }
38
39 } // namespace
40
41 namespace voice {
42
43 // static
44 SpeechInputLocaleConfigImpl* SpeechInputLocaleConfigImpl::GetInstance() {
45 return base::Singleton<SpeechInputLocaleConfigImpl>::get();
46 }
47
48 SpeechInputLocaleConfigImpl::SpeechInputLocaleConfigImpl() {
49 InitializeAvailableLocales();
50 InitializeLocaleMatches();
51 InitializeTextToSpeechLangauges();
52 }
53
54 SpeechInputLocaleConfigImpl::~SpeechInputLocaleConfigImpl() {}
55
56 SpeechInputLocale SpeechInputLocaleConfigImpl::GetDefaultLocale() const {
57 return GetMatchingLocale(GetDefaultLocaleCode());
58 }
59
60 const std::vector<SpeechInputLocale>&
61 SpeechInputLocaleConfigImpl::GetAvailableLocales() const {
62 return available_locales_;
63 }
64
65 SpeechInputLocale SpeechInputLocaleConfigImpl::GetLocaleForCode(
66 const std::string& locale_code) const {
67 auto index_iterator = locale_indices_for_codes_.find(locale_code);
68 if (index_iterator == locale_indices_for_codes_.end())
69 return SpeechInputLocale();
70 return available_locales_[index_iterator->second];
71 }
72
73 const std::vector<std::string>&
74 SpeechInputLocaleConfigImpl::GetTextToSpeechLanguages() const {
75 return text_to_speech_languages_;
76 }
77
78 bool SpeechInputLocaleConfigImpl::IsTextToSpeechEnabledForCode(
79 const std::string& locale_code) const {
80 std::string language = GetLanguageComponentForLocaleCode(locale_code);
81 auto found_language = std::find(text_to_speech_languages_.begin(),
82 text_to_speech_languages_.end(), language);
83 return found_language != text_to_speech_languages_.end();
84 }
85
86 SpeechInputLocale SpeechInputLocaleConfigImpl::GetMatchingLocale(
87 const std::string& locale_code) const {
88 // Return exact match if one is found.
89 auto index_iterator = locale_indices_for_codes_.find(locale_code);
90 if (index_iterator != locale_indices_for_codes_.end())
91 return available_locales_[index_iterator->second];
92 // If there is no exact match, search for another locale with the same
93 // language component.
94 std::string language = GetLanguageComponentForLocaleCode(locale_code);
95 std::vector<size_t> locale_indices;
96 for (auto locale_index_for_code : locale_indices_for_codes_) {
97 std::string code = locale_index_for_code.first;
98 if (GetLanguageComponentForLocaleCode(code) == language)
99 locale_indices.push_back(locale_index_for_code.second);
100 }
101 // Use en-US as a default value.
102 auto it = locale_indices_for_codes_.find(kEnglishUS);
103 CHECK(it != locale_indices_for_codes_.end());
104 size_t locale_index = it->second;
105 if (locale_indices.size() == 1) {
106 // If only one match was found, use its associated InputLocale.
107 locale_index = locale_indices[0];
108 } else if (locale_indices.size() > 1) {
109 // If multiple regional differences for the same language are supported
110 // (e.g. en-US, en-GB, en-AU, en-NZ), use the default language mapping.
111 auto default_locale_iterator =
112 default_locale_indices_for_languages_.find(language);
113 if (default_locale_iterator != default_locale_indices_for_languages_.end())
114 return available_locales_[default_locale_iterator->second];
115 }
116 return available_locales_[locale_index];
117 }
118
119 std::string SpeechInputLocaleConfigImpl::GetDefaultLocaleCode() const {
120 // Default locale code is computed based on the UI language as reported by
121 // first object in +preferredLanguages and the device's locale (which
122 // corresponds to the "Region Format" in iOS' Settings UI). By combining
123 // the two signals, there is a better chance of setting the default
124 // Voice Search language to the regionally specific language variant.
125 // For example, users who selected English as the UI language and use
126 // South Africa Region Format (for date, time, currency, etc.) will
127 // default to use en-ZA, English (South Africa), as the Voice Search
128 // language.
129 NSLocale* current_locale = [NSLocale currentLocale];
130 NSLocale* lang_pref_locale = [NSLocale
131 localeWithLocaleIdentifier:[[NSLocale preferredLanguages] firstObject]];
132 // Prioritize the language portion of |language_pref_locale|.
133 NSString* language = [lang_pref_locale objectForKey:NSLocaleLanguageCode];
134 if (!language.length)
135 language = [current_locale objectForKey:NSLocaleLanguageCode];
136 DCHECK(language.length);
137 // Prioritize the country portion of |current_locale|.
138 NSString* country = [current_locale objectForKey:NSLocaleCountryCode];
139 if (!country.length)
140 country = [lang_pref_locale objectForKey:NSLocaleCountryCode];
141 DCHECK(country.length);
142 return GetCanonicalLocaleForLocale(
143 [NSString stringWithFormat:@"%@-%@", language, country]);
144 }
145
146 void SpeechInputLocaleConfigImpl::InitializeAvailableLocales() {
147 NSArray* languages =
148 ios::GetChromeBrowserProvider()->GetAvailableVoiceSearchLanguages();
149 for (VoiceSearchLanguage* language in languages) {
150 // Store the InputLocale in |available_locales_|.
151 std::string locale_code = GetCanonicalLocaleForLocale(language.identifier);
152 DCHECK(locale_code.length());
153 voice::SpeechInputLocale locale;
154 locale.code = locale_code;
155 locale.display_name = base::SysNSStringToUTF16(language.displayName);
156 available_locales_.push_back(locale);
157 // Store the index of the InputLocale.
158 size_t locale_index = available_locales_.size() - 1;
159 locale_indices_for_codes_[locale_code] = locale_index;
160 // Store a mapping from |language.localizationPreference| to the locale.
161 std::string localization_preference =
162 GetCanonicalLocaleForLocale(language.localizationPreference);
163 if (localization_preference.length())
164 locale_indices_for_codes_[localization_preference] = locale_index;
165 }
166 }
167
168 void SpeechInputLocaleConfigImpl::InitializeLocaleMatches() {
169 NSArray* matches = [SpeechInputLocaleMatchConfig sharedInstance].matches;
170 for (SpeechInputLocaleMatch* match in matches) {
171 std::string locale = GetCanonicalLocaleForLocale(match.matchedLocaleCode);
172 auto index_iterator = locale_indices_for_codes_.find(locale);
173 if (index_iterator != locale_indices_for_codes_.end()) {
174 size_t index = index_iterator->second;
175 for (NSString* matching_locale in match.matchingLocaleCodes) {
176 // Record the regional variant matches.
177 std::string locale_code = GetCanonicalLocaleForLocale(matching_locale);
178 locale_indices_for_codes_[locale_code] = index;
179 }
180 for (NSString* matching_language in match.matchingLanguages) {
181 // Record the default locale for the matching languages.
182 std::string language = base::SysNSStringToUTF8(matching_language);
183 default_locale_indices_for_languages_[language] = index;
184 }
185 }
186 }
187 }
188
189 void SpeechInputLocaleConfigImpl::InitializeTextToSpeechLangauges() {
190 text_to_speech_languages_ = {"de", "en", "es", "fr", "it", "ja", "ko"};
191 }
192
193 } // namespace voice
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698