Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/ui/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/ui/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.size()) | |
|
sdefresne
2016/10/17 16:12:39
Can you change this to use tokens.empty()?
if (
rohitrao (ping after 24h)
2016/10/17 17:42:27
Done.
| |
| 25 return tokens[0]; | |
| 26 return std::string(); | |
| 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 SpeechInputLocaleConfig* SpeechInputLocaleConfig::GetInstance() { | |
|
sdefresne
2016/10/17 16:12:40
I really dislike having the GetInstance in the sub
rohitrao (ping after 24h)
2016/10/17 17:42:27
Done.
| |
| 45 return SpeechInputLocaleConfigImpl::GetInstance(); | |
| 46 } | |
| 47 | |
| 48 // static | |
| 49 SpeechInputLocaleConfigImpl* SpeechInputLocaleConfigImpl::GetInstance() { | |
| 50 return base::Singleton<SpeechInputLocaleConfigImpl>::get(); | |
| 51 } | |
| 52 | |
| 53 SpeechInputLocaleConfigImpl::SpeechInputLocaleConfigImpl() { | |
| 54 InitializeAvailableLocales(); | |
| 55 InitializeLocaleMatches(); | |
| 56 InitializeTextToSpeechLangauges(); | |
| 57 } | |
| 58 | |
| 59 SpeechInputLocaleConfigImpl::~SpeechInputLocaleConfigImpl() {} | |
| 60 | |
| 61 SpeechInputLocale SpeechInputLocaleConfigImpl::GetDefaultLocale() { | |
| 62 return GetMatchingLocale(GetDefaultLocaleCode()); | |
| 63 } | |
| 64 | |
| 65 const std::vector<SpeechInputLocale>& | |
| 66 SpeechInputLocaleConfigImpl::GetAvailableLocales() { | |
| 67 return available_locales_; | |
| 68 } | |
| 69 | |
| 70 SpeechInputLocale SpeechInputLocaleConfigImpl::GetLocaleForCode( | |
|
sdefresne
2016/10/17 16:12:40
This method implementation does not match the comm
rohitrao (ping after 24h)
2016/10/17 17:42:27
Will follow up with Kurt on this.
| |
| 71 const std::string& locale_code) { | |
| 72 auto index_iterator = locale_indices_for_codes_.find(locale_code); | |
| 73 if (index_iterator == locale_indices_for_codes_.end()) | |
| 74 return SpeechInputLocale(); | |
| 75 return available_locales_[index_iterator->second]; | |
| 76 } | |
| 77 | |
| 78 const std::vector<std::string>& | |
| 79 SpeechInputLocaleConfigImpl::GetTextToSpeechLanguages() { | |
| 80 return text_to_speech_languages_; | |
| 81 } | |
| 82 | |
| 83 bool SpeechInputLocaleConfigImpl::IsTextToSpeechEnabledForCode( | |
| 84 const std::string& locale_code) { | |
| 85 std::string language = GetLanguageComponentForLocaleCode(locale_code); | |
| 86 auto found_language = std::find(text_to_speech_languages_.begin(), | |
| 87 text_to_speech_languages_.end(), language); | |
| 88 return found_language != text_to_speech_languages_.end(); | |
| 89 } | |
| 90 | |
| 91 SpeechInputLocale SpeechInputLocaleConfigImpl::GetMatchingLocale( | |
| 92 const std::string& locale_code) { | |
| 93 // Return exact match if one is found. | |
| 94 auto index_iterator = locale_indices_for_codes_.find(locale_code); | |
| 95 if (index_iterator != locale_indices_for_codes_.end()) | |
| 96 return available_locales_[index_iterator->second]; | |
| 97 // If there is no exact match, search for another locale with the same | |
| 98 // language component. | |
| 99 std::string language = GetLanguageComponentForLocaleCode(locale_code); | |
| 100 std::vector<size_t> locale_indices; | |
| 101 for (auto locale_index_for_code : locale_indices_for_codes_) { | |
| 102 std::string code = locale_index_for_code.first; | |
| 103 if (GetLanguageComponentForLocaleCode(code) == language) | |
| 104 locale_indices.push_back(locale_index_for_code.second); | |
| 105 } | |
| 106 // Use en-US as a default value. | |
| 107 size_t locale_index = locale_indices_for_codes_[kEnglishUS]; | |
| 108 if (locale_indices.size() == 1) { | |
| 109 // If only one match was found, use its associated InputLocale. | |
| 110 locale_index = locale_indices[0]; | |
| 111 } else if (locale_indices.size() > 1) { | |
| 112 // If multiple regional differences for the same language are supported | |
| 113 // (e.g. en-US, en-GB, en-AU, en-NZ), use the default language mapping. | |
| 114 auto default_locale_iterator = | |
| 115 default_locale_indices_for_languages_.find(language); | |
| 116 if (default_locale_iterator != default_locale_indices_for_languages_.end()) | |
| 117 return available_locales_[default_locale_iterator->second]; | |
| 118 } | |
| 119 return available_locales_[locale_index]; | |
| 120 } | |
| 121 | |
| 122 std::string SpeechInputLocaleConfigImpl::GetDefaultLocaleCode() { | |
| 123 // Default locale code is computed based on the UI language as reported by | |
| 124 // first object in +preferredLanguages and the device's locale (which | |
| 125 // corresponds to the "Region Format" in iOS' Settings UI). By combining | |
| 126 // the two signals, there is a better chance of setting the default | |
| 127 // Voice Search language to the regionally specific language variant. | |
| 128 // For example, users who selected English as the UI language and use | |
| 129 // South Africa Region Format (for date, time, currency, etc.) will | |
| 130 // default to use en-ZA, English (South Africa), as the Voice Search | |
| 131 // language. | |
| 132 NSLocale* current_locale = [NSLocale currentLocale]; | |
| 133 NSLocale* lang_pref_locale = [NSLocale | |
| 134 localeWithLocaleIdentifier:[[NSLocale preferredLanguages] firstObject]]; | |
| 135 // Prioritize the language portion of |language_pref_locale|. | |
| 136 NSString* language = [lang_pref_locale objectForKey:NSLocaleLanguageCode]; | |
| 137 if (!language.length) | |
| 138 language = [current_locale objectForKey:NSLocaleLanguageCode]; | |
| 139 DCHECK(language.length); | |
| 140 // Prioritize the country portion of |current_locale|. | |
| 141 NSString* country = [current_locale objectForKey:NSLocaleCountryCode]; | |
| 142 if (!country.length) | |
| 143 country = [lang_pref_locale objectForKey:NSLocaleCountryCode]; | |
| 144 DCHECK(country.length); | |
| 145 return GetCanonicalLocaleForLocale( | |
| 146 [NSString stringWithFormat:@"%@-%@", language, country]); | |
| 147 } | |
| 148 | |
| 149 void SpeechInputLocaleConfigImpl::InitializeAvailableLocales() { | |
| 150 NSArray* languages = | |
| 151 ios::GetChromeBrowserProvider()->GetAvailableVoiceSearchLanguages(); | |
| 152 for (VoiceSearchLanguage* language in languages) { | |
| 153 // Store the InputLocale in |available_locales_|. | |
| 154 std::string locale_code = GetCanonicalLocaleForLocale(language.identifier); | |
| 155 DCHECK(locale_code.length()); | |
| 156 voice::SpeechInputLocale locale; | |
| 157 locale.code = locale_code; | |
| 158 locale.display_name = base::SysNSStringToUTF16(language.displayName); | |
| 159 available_locales_.push_back(locale); | |
| 160 // Store the index of the InputLocale. | |
| 161 size_t locale_index = available_locales_.size() - 1; | |
| 162 locale_indices_for_codes_[locale_code] = locale_index; | |
| 163 // Store a mapping from |language.localizationPreference| to the locale. | |
| 164 std::string localization_preference = | |
| 165 GetCanonicalLocaleForLocale(language.localizationPreference); | |
| 166 if (localization_preference.length()) | |
| 167 locale_indices_for_codes_[localization_preference] = locale_index; | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void SpeechInputLocaleConfigImpl::InitializeLocaleMatches() { | |
| 172 NSArray* matches = [SpeechInputLocaleMatchConfig sharedInstance].matches; | |
| 173 for (SpeechInputLocaleMatch* match in matches) { | |
| 174 std::string locale = GetCanonicalLocaleForLocale(match.matchedLocaleCode); | |
| 175 auto index_iterator = locale_indices_for_codes_.find(locale); | |
| 176 if (index_iterator != locale_indices_for_codes_.end()) { | |
| 177 size_t index = index_iterator->second; | |
| 178 for (NSString* matching_locale in match.matchingLocaleCodes) { | |
| 179 // Record the regional variant matches. | |
| 180 std::string locale_code = GetCanonicalLocaleForLocale(matching_locale); | |
| 181 locale_indices_for_codes_[locale_code] = index; | |
| 182 } | |
| 183 for (NSString* matching_language in match.matchingLanguages) { | |
| 184 // Record the default locale for the matching languages. | |
| 185 std::string language = base::SysNSStringToUTF8(matching_language); | |
| 186 default_locale_indices_for_languages_[language] = index; | |
| 187 } | |
| 188 } | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 void SpeechInputLocaleConfigImpl::InitializeTextToSpeechLangauges() { | |
| 193 text_to_speech_languages_ = {"de", "en", "es", "fr", "it", "ja", "ko"}; | |
| 194 } | |
| 195 } | |
|
sdefresne
2016/10/17 16:12:39
nit: blank line before closing brace of the namesp
rohitrao (ping after 24h)
2016/10/17 17:42:27
Done.
| |
| OLD | NEW |