Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / input_method_util.cc
blob57d840a7a36cf686f64a98cef8d3ab5b270410d0
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/input_method/input_method_util.h"
7 #include <algorithm>
8 #include <functional>
9 #include <map>
10 #include <utility>
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/string_split.h"
15 #include "base/string_util.h"
16 #include "base/utf_string_conversions.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/common/pref_names.h"
20 #include "grit/generated_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/l10n/l10n_util_collator.h"
23 #include "unicode/uloc.h"
25 namespace {
27 // A mapping from an input method id to a string for the language indicator. The
28 // mapping is necessary since some input methods belong to the same language.
29 // For example, both "xkb:us::eng" and "xkb:us:dvorak:eng" are for US English.
30 const struct {
31 const char* input_method_id;
32 const char* indicator_text;
33 } kMappingFromIdToIndicatorText[] = {
34 // To distinguish from "xkb:us::eng"
35 { "xkb:us:altgr-intl:eng", "EXTD" },
36 { "xkb:us:dvorak:eng", "DV" },
37 { "xkb:us:intl:eng", "INTL" },
38 { "xkb:us:colemak:eng", "CO" },
39 { "english-m", "??" },
40 { "xkb:de:neo:ger", "NEO" },
41 // To distinguish from "xkb:es::spa"
42 { "xkb:es:cat:cat", "CAS" },
43 // To distinguish from "xkb:gb::eng"
44 { "xkb:gb:dvorak:eng", "DV" },
45 // To distinguish from "xkb:jp::jpn"
46 { "mozc", "\xe3\x81\x82" }, // U+3042, Japanese Hiragana letter A in UTF-8.
47 { "mozc-dv", "\xe3\x81\x82" },
48 { "mozc-jp", "\xe3\x81\x82" },
49 // For simplified Chinese input methods
50 { "pinyin", "\xe6\x8b\xbc" }, // U+62FC
51 { "pinyin-dv", "\xe6\x8b\xbc" },
52 // For traditional Chinese input methods
53 { "mozc-chewing", "\xe9\x85\xb7" }, // U+9177
54 { "m17n:zh:cangjie", "\xe5\x80\x89" }, // U+5009
55 { "m17n:zh:quick", "\xe9\x80\x9f" }, // U+901F
56 // For Hangul input method.
57 { "mozc-hangul", "\xed\x95\x9c" }, // U+D55C
60 const size_t kMappingFromIdToIndicatorTextLen =
61 ARRAYSIZE_UNSAFE(kMappingFromIdToIndicatorText);
63 // A mapping from an input method id to a resource id for a
64 // medium length language indicator.
65 // For those languages that want to display a slightly longer text in the
66 // "Your input method has changed to..." bubble than in the status tray.
67 // If an entry is not found in this table the short name is used.
68 const struct {
69 const char* input_method_id;
70 const int resource_id;
71 } kMappingImeIdToMediumLenNameResourceId[] = {
72 { "m17n:zh:cangjie", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
73 { "m17n:zh:quick", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
74 { "mozc", IDS_LANGUAGES_MEDIUM_LEN_NAME_JAPANESE },
75 { "mozc-chewing", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
76 { "mozc-dv", IDS_LANGUAGES_MEDIUM_LEN_NAME_JAPANESE },
77 { "mozc-hangul", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
78 { "mozc-jp", IDS_LANGUAGES_MEDIUM_LEN_NAME_JAPANESE },
79 { "pinyin", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_SIMPLIFIED },
80 { "pinyin-dv", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_SIMPLIFIED },
82 const size_t kMappingImeIdToMediumLenNameResourceIdLen =
83 ARRAYSIZE_UNSAFE(kMappingImeIdToMediumLenNameResourceId);
85 string16 GetLanguageName(const std::string& language_code) {
86 const string16 language_name = l10n_util::GetDisplayNameForLocale(
87 language_code, g_browser_process->GetApplicationLocale(), true);
88 return language_name;
93 namespace chromeos {
95 extern const char* kExtensionImePrefix;
97 namespace input_method {
99 namespace {
101 const struct EnglishToResouceId {
102 const char* english_string_from_ibus;
103 int resource_id;
104 } kEnglishToResourceIdArray[] = {
105 // For ibus-mozc.
106 { "Direct input", IDS_STATUSBAR_IME_JAPANESE_IME_STATUS_DIRECT_INPUT },
107 { "Hiragana", IDS_STATUSBAR_IME_JAPANESE_IME_STATUS_HIRAGANA },
108 { "Katakana", IDS_STATUSBAR_IME_JAPANESE_IME_STATUS_KATAKANA },
109 { "Half width katakana", // small k is not a typo.
110 IDS_STATUSBAR_IME_JAPANESE_IME_STATUS_HALF_WIDTH_KATAKANA },
111 { "Latin", IDS_STATUSBAR_IME_JAPANESE_IME_STATUS_LATIN },
112 { "Wide Latin", IDS_STATUSBAR_IME_JAPANESE_IME_STATUS_WIDE_LATIN },
114 // For ibus-mozc-hangul
115 { "Hanja mode", IDS_STATUSBAR_IME_KOREAN_HANJA_INPUT_MODE },
116 { "Hangul mode", IDS_STATUSBAR_IME_KOREAN_HANGUL_INPUT_MODE },
118 // For ibus-mozc-pinyin.
119 { "Full/Half width",
120 IDS_STATUSBAR_IME_CHINESE_PINYIN_TOGGLE_FULL_HALF },
121 { "Full/Half width punctuation",
122 IDS_STATUSBAR_IME_CHINESE_PINYIN_TOGGLE_FULL_HALF_PUNCTUATION },
123 // TODO(hsumita): Fixes a typo
124 { "Simplfied/Traditional Chinese",
125 IDS_STATUSBAR_IME_CHINESE_PINYIN_TOGGLE_S_T_CHINESE },
126 { "Chinese",
127 IDS_STATUSBAR_IME_CHINESE_PINYIN_TOGGLE_CHINESE_ENGLISH },
129 // For ibus-mozc-chewing.
130 { "English",
131 IDS_STATUSBAR_IME_CHINESE_MOZC_CHEWING_ENGLISH_MODE },
132 { "_Chinese",
133 IDS_STATUSBAR_IME_CHINESE_MOZC_CHEWING_CHINESE_MODE },
134 { "Full-width English",
135 IDS_STATUSBAR_IME_CHINESE_MOZC_CHEWING_FULL_WIDTH_ENGLISH_MODE },
137 // For the "Languages and Input" dialog.
138 { "m17n:ar:kbd", IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
139 { "m17n:hi:itrans", // also uses the "STANDARD_INPUT_METHOD" id.
140 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
141 { "m17n:zh:cangjie",
142 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_CHINESE_CANGJIE_INPUT_METHOD },
143 { "m17n:zh:quick",
144 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_CHINESE_QUICK_INPUT_METHOD },
145 { "m17n:fa:isiri",
146 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_PERSIAN_ISIRI_2901_INPUT_METHOD },
147 { "m17n:th:kesmanee",
148 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_THAI_KESMANEE_INPUT_METHOD },
149 { "m17n:th:tis820",
150 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_THAI_TIS820_INPUT_METHOD },
151 { "m17n:th:pattachote",
152 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_THAI_PATTACHOTE_INPUT_METHOD },
153 { "m17n:vi:tcvn",
154 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_VIETNAMESE_TCVN_INPUT_METHOD },
155 { "m17n:vi:telex",
156 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_VIETNAMESE_TELEX_INPUT_METHOD },
157 { "m17n:vi:viqr",
158 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_VIETNAMESE_VIQR_INPUT_METHOD },
159 { "m17n:vi:vni",
160 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_VIETNAMESE_VNI_INPUT_METHOD },
161 { "m17n:bn:itrans",
162 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
163 { "m17n:gu:itrans",
164 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
165 { "m17n:ml:itrans",
166 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
167 { "m17n:mr:itrans",
168 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
169 { "m17n:ta:phonetic",
170 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_TAMIL_PHONETIC },
171 { "m17n:ta:inscript",
172 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_TAMIL_INSCRIPT },
173 { "m17n:ta:tamil99",
174 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_TAMIL_TAMIL99 },
175 { "m17n:ta:itrans",
176 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_TAMIL_ITRANS },
177 { "m17n:ta:typewriter",
178 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_TAMIL_TYPEWRITER },
179 { "m17n:am:sera",
180 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
181 { "m17n:te:itrans",
182 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
183 { "m17n:kn:itrans",
184 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD },
186 { "mozc-chewing",
187 IDS_OPTIONS_SETTINGS_LANGUAGES_CHEWING_INPUT_METHOD },
188 { "pinyin", IDS_OPTIONS_SETTINGS_LANGUAGES_PINYIN_INPUT_METHOD },
189 { "pinyin-dv",
190 IDS_OPTIONS_SETTINGS_LANGUAGES_PINYIN_DV_INPUT_METHOD },
191 #if defined(GOOGLE_CHROME_BUILD)
192 { "mozc",
193 IDS_OPTIONS_SETTINGS_LANGUAGES_JAPANESE_GOOGLE_US_INPUT_METHOD },
194 { "mozc-dv",
195 IDS_OPTIONS_SETTINGS_LANGUAGES_JAPANESE_GOOGLE_US_DV_INPUT_METHOD },
196 { "mozc-jp",
197 IDS_OPTIONS_SETTINGS_LANGUAGES_JAPANESE_GOOGLE_JP_INPUT_METHOD },
198 #else
199 { "mozc",
200 IDS_OPTIONS_SETTINGS_LANGUAGES_JAPANESE_MOZC_US_INPUT_METHOD },
201 { "mozc-dv",
202 IDS_OPTIONS_SETTINGS_LANGUAGES_JAPANESE_MOZC_US_DV_INPUT_METHOD },
203 { "mozc-jp",
204 IDS_OPTIONS_SETTINGS_LANGUAGES_JAPANESE_MOZC_JP_INPUT_METHOD },
205 #endif // if defined(GOOGLE_CHROME_BUILD)
206 { "zinnia-japanese",
207 IDS_OPTIONS_SETTINGS_LANGUAGES_JAPANESE_HANDWRITING_INPUT_METHOD },
208 { "mozc-hangul", IDS_OPTIONS_SETTINGS_LANGUAGES_KOREAN_INPUT_METHOD },
210 // For ibus-xkb-layouts engine: third_party/ibus-xkb-layouts/files
211 { "xkb:jp::jpn", IDS_STATUSBAR_LAYOUT_JAPAN },
212 { "xkb:si::slv", IDS_STATUSBAR_LAYOUT_SLOVENIA },
213 { "xkb:de::ger", IDS_STATUSBAR_LAYOUT_GERMANY },
214 { "xkb:de:neo:ger", IDS_STATUSBAR_LAYOUT_GERMANY_NEO2 },
215 { "xkb:it::ita", IDS_STATUSBAR_LAYOUT_ITALY },
216 { "xkb:ee::est", IDS_STATUSBAR_LAYOUT_ESTONIA },
217 { "xkb:hu::hun", IDS_STATUSBAR_LAYOUT_HUNGARY },
218 { "xkb:pl::pol", IDS_STATUSBAR_LAYOUT_POLAND },
219 { "xkb:dk::dan", IDS_STATUSBAR_LAYOUT_DENMARK },
220 { "xkb:hr::scr", IDS_STATUSBAR_LAYOUT_CROATIA },
221 { "xkb:br::por", IDS_STATUSBAR_LAYOUT_BRAZIL },
222 { "xkb:rs::srp", IDS_STATUSBAR_LAYOUT_SERBIA },
223 { "xkb:cz::cze", IDS_STATUSBAR_LAYOUT_CZECHIA },
224 { "xkb:us:dvorak:eng", IDS_STATUSBAR_LAYOUT_USA_DVORAK },
225 { "xkb:us:colemak:eng", IDS_STATUSBAR_LAYOUT_USA_COLEMAK },
226 { "xkb:ro::rum", IDS_STATUSBAR_LAYOUT_ROMANIA },
227 { "xkb:us::eng", IDS_STATUSBAR_LAYOUT_USA },
228 { "xkb:us:altgr-intl:eng", IDS_STATUSBAR_LAYOUT_USA_EXTENDED },
229 { "xkb:us:intl:eng", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
230 { "xkb:lt::lit", IDS_STATUSBAR_LAYOUT_LITHUANIA },
231 { "xkb:gb:extd:eng", IDS_STATUSBAR_LAYOUT_UNITED_KINGDOM },
232 { "xkb:gb:dvorak:eng", IDS_STATUSBAR_LAYOUT_UNITED_KINGDOM_DVORAK },
233 { "xkb:sk::slo", IDS_STATUSBAR_LAYOUT_SLOVAKIA },
234 { "xkb:ru::rus", IDS_STATUSBAR_LAYOUT_RUSSIA },
235 { "xkb:ru:phonetic:rus", IDS_STATUSBAR_LAYOUT_RUSSIA_PHONETIC },
236 { "xkb:gr::gre", IDS_STATUSBAR_LAYOUT_GREECE },
237 { "xkb:be::fra", IDS_STATUSBAR_LAYOUT_BELGIUM },
238 { "xkb:be::ger", IDS_STATUSBAR_LAYOUT_BELGIUM },
239 { "xkb:be::nld", IDS_STATUSBAR_LAYOUT_BELGIUM },
240 { "xkb:bg::bul", IDS_STATUSBAR_LAYOUT_BULGARIA },
241 { "xkb:bg:phonetic:bul", IDS_STATUSBAR_LAYOUT_BULGARIA_PHONETIC },
242 { "xkb:ch::ger", IDS_STATUSBAR_LAYOUT_SWITZERLAND },
243 { "xkb:ch:fr:fra", IDS_STATUSBAR_LAYOUT_SWITZERLAND_FRENCH },
244 { "xkb:tr::tur", IDS_STATUSBAR_LAYOUT_TURKEY },
245 { "xkb:pt::por", IDS_STATUSBAR_LAYOUT_PORTUGAL },
246 { "xkb:es::spa", IDS_STATUSBAR_LAYOUT_SPAIN },
247 { "xkb:fi::fin", IDS_STATUSBAR_LAYOUT_FINLAND },
248 { "xkb:ua::ukr", IDS_STATUSBAR_LAYOUT_UKRAINE },
249 { "xkb:es:cat:cat", IDS_STATUSBAR_LAYOUT_SPAIN_CATALAN },
250 { "xkb:fr::fra", IDS_STATUSBAR_LAYOUT_FRANCE },
251 { "xkb:no::nob", IDS_STATUSBAR_LAYOUT_NORWAY },
252 { "xkb:se::swe", IDS_STATUSBAR_LAYOUT_SWEDEN },
253 { "xkb:nl::nld", IDS_STATUSBAR_LAYOUT_NETHERLANDS },
254 { "xkb:latam::spa", IDS_STATUSBAR_LAYOUT_LATIN_AMERICAN },
255 { "xkb:lv:apostrophe:lav", IDS_STATUSBAR_LAYOUT_LATVIA },
256 { "xkb:ca::fra", IDS_STATUSBAR_LAYOUT_CANADA },
257 { "xkb:ca:eng:eng", IDS_STATUSBAR_LAYOUT_CANADA_ENGLISH },
258 { "xkb:il::heb", IDS_STATUSBAR_LAYOUT_ISRAEL },
259 { "xkb:kr:kr104:kor", IDS_STATUSBAR_LAYOUT_KOREA_104 },
261 { "english-m", IDS_STATUSBAR_LAYOUT_USA_MYSTERY },
263 const size_t kEnglishToResourceIdArraySize =
264 arraysize(kEnglishToResourceIdArray);
266 // The comparator is used for sorting language codes by their
267 // corresponding language names, using the ICU collator.
268 struct CompareLanguageCodesByLanguageName
269 : std::binary_function<const std::string&, const std::string&, bool> {
270 explicit CompareLanguageCodesByLanguageName(icu::Collator* collator)
271 : collator_(collator) {
274 // Calling GetLanguageDisplayNameFromCode() in the comparator is not
275 // efficient, but acceptable as the function is cheap, and the language
276 // list is short (about 60 at most).
277 bool operator()(const std::string& s1, const std::string& s2) const {
278 const string16 key1 = InputMethodUtil::GetLanguageDisplayNameFromCode(s1);
279 const string16 key2 = InputMethodUtil::GetLanguageDisplayNameFromCode(s2);
280 return l10n_util::StringComparator<string16>(collator_)(key1, key2);
283 private:
284 icu::Collator* collator_;
287 } // namespace
289 const ExtraLanguage kExtraLanguages[] = {
290 // Language Code Input Method ID
291 { "en-AU", "xkb:us::eng" }, // For Austrailia, use US keyboard layout.
292 { "id", "xkb:us::eng" }, // For Indonesian, use US keyboard layout.
293 // The code "fil" comes from l10_util.cc.
294 { "fil", "xkb:us::eng" }, // For Filipino, use US keyboard layout.
295 // For Netherlands, use US international keyboard layout.
296 { "nl", "xkb:us:intl:eng" },
297 // The code "es-419" comes from l10_util.cc.
298 // For Spanish in Latin America, use Latin American keyboard layout.
299 { "es-419", "xkb:latam::spa" },
300 // For Malay, use US keyboard layout. crosbug.com/p/8288
301 { "ms", "xkb:us::eng" },
303 // TODO(yusukes): Add {"sw", "xkb:us::eng"} once Swahili is removed from the
304 // blacklist in src/ui/base/l10n/l10n_util_posix.cc.
306 const size_t kExtraLanguagesLength = arraysize(kExtraLanguages);
308 InputMethodUtil::InputMethodUtil(
309 InputMethodDescriptors* supported_input_methods)
310 : supported_input_methods_(supported_input_methods) {
311 ReloadInternalMaps();
313 // Initialize a map from English string to Chrome string resource ID as well.
314 for (size_t i = 0; i < kEnglishToResourceIdArraySize; ++i) {
315 const EnglishToResouceId& map_entry = kEnglishToResourceIdArray[i];
316 const bool result = english_to_resource_id_.insert(std::make_pair(
317 map_entry.english_string_from_ibus, map_entry.resource_id)).second;
318 DCHECK(result) << "Duplicated string is found: "
319 << map_entry.english_string_from_ibus;
323 InputMethodUtil::~InputMethodUtil() {
326 bool InputMethodUtil::TranslateStringInternal(
327 const std::string& english_string, string16 *out_string) const {
328 DCHECK(out_string);
329 HashType::const_iterator iter = english_to_resource_id_.find(english_string);
330 if (iter == english_to_resource_id_.end()) {
331 // TODO(yusukes): Write Autotest which checks if all display names and all
332 // property names for supported input methods are listed in the resource
333 // ID array (crosbug.com/4572).
334 LOG(ERROR) << "Resource ID is not found for: " << english_string;
335 return false;
338 *out_string = l10n_util::GetStringUTF16(iter->second);
339 return true;
342 string16 InputMethodUtil::TranslateString(
343 const std::string& english_string) const {
344 string16 localized_string;
345 if (TranslateStringInternal(english_string, &localized_string)) {
346 return localized_string;
348 return UTF8ToUTF16(english_string);
351 bool InputMethodUtil::StringIsSupported(
352 const std::string& english_string) const {
353 string16 localized_string;
354 return TranslateStringInternal(english_string, &localized_string);
357 bool InputMethodUtil::IsValidInputMethodId(
358 const std::string& input_method_id) const {
359 return GetInputMethodDescriptorFromId(input_method_id) != NULL;
362 // static
363 bool InputMethodUtil::IsKeyboardLayout(const std::string& input_method_id) {
364 const bool kCaseInsensitive = false;
365 return StartsWithASCII(input_method_id, "xkb:", kCaseInsensitive);
368 // static
369 bool InputMethodUtil::IsExtensionInputMethod(
370 const std::string& input_method_id) {
371 const bool kCaseInsensitive = false;
372 return StartsWithASCII(input_method_id,
373 kExtensionImePrefix,
374 kCaseInsensitive);
377 std::string InputMethodUtil::GetLanguageCodeFromInputMethodId(
378 const std::string& input_method_id) const {
379 // The code should be compatible with one of codes used for UI languages,
380 // defined in app/l10_util.cc.
381 const char kDefaultLanguageCode[] = "en-US";
382 std::map<std::string, std::string>::const_iterator iter
383 = id_to_language_code_.find(input_method_id);
384 return (iter == id_to_language_code_.end()) ?
385 // Returning |kDefaultLanguageCode| here is not for Chrome OS but for
386 // Ubuntu where the ibus-xkb-layouts engine could be missing.
387 kDefaultLanguageCode : iter->second;
390 std::string InputMethodUtil::GetKeyboardLayoutName(
391 const std::string& input_method_id) const {
392 InputMethodIdToDescriptorMap::const_iterator iter
393 = id_to_descriptor_.find(input_method_id);
394 return (iter == id_to_descriptor_.end()) ?
395 "" : iter->second.keyboard_layout();
398 std::string InputMethodUtil::GetInputMethodDisplayNameFromId(
399 const std::string& input_method_id) const {
400 string16 display_name;
401 if (!IsExtensionInputMethod(input_method_id) &&
402 TranslateStringInternal(input_method_id, &display_name)) {
403 return UTF16ToUTF8(display_name);
405 // Return an empty string if the display name is not found.
406 return "";
409 string16 InputMethodUtil::GetInputMethodShortName(
410 const InputMethodDescriptor& input_method) const {
411 // For the status area, we use two-letter, upper-case language code like
412 // "US" and "JP".
413 string16 text;
415 // Check special cases first.
416 for (size_t i = 0; i < kMappingFromIdToIndicatorTextLen; ++i) {
417 if (kMappingFromIdToIndicatorText[i].input_method_id == input_method.id()) {
418 text = UTF8ToUTF16(kMappingFromIdToIndicatorText[i].indicator_text);
419 break;
423 // Display the keyboard layout name when using a keyboard layout.
424 if (text.empty() &&
425 IsKeyboardLayout(input_method.id())) {
426 const size_t kMaxKeyboardLayoutNameLen = 2;
427 const string16 keyboard_layout =
428 UTF8ToUTF16(GetKeyboardLayoutName(input_method.id()));
429 text = StringToUpperASCII(keyboard_layout).substr(
430 0, kMaxKeyboardLayoutNameLen);
433 // TODO(yusukes): Some languages have two or more input methods. For example,
434 // Thai has 3, Vietnamese has 4. If these input methods could be activated at
435 // the same time, we should do either of the following:
436 // (1) Add mappings to |kMappingFromIdToIndicatorText|
437 // (2) Add suffix (1, 2, ...) to |text| when ambiguous.
439 if (text.empty()) {
440 const size_t kMaxLanguageNameLen = 2;
441 const std::string language_code = input_method.language_code();
442 text = StringToUpperASCII(UTF8ToUTF16(language_code)).substr(
443 0, kMaxLanguageNameLen);
445 DCHECK(!text.empty());
446 return text;
449 string16 InputMethodUtil::GetInputMethodMediumName(
450 const InputMethodDescriptor& input_method) const {
451 // For the "Your input method has changed to..." bubble. In most cases
452 // it uses the same name as the short name, unless found in a table
453 // for medium length names.
454 for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) {
455 if (kMappingImeIdToMediumLenNameResourceId[i].input_method_id ==
456 input_method.id()) {
457 return l10n_util::GetStringUTF16(
458 kMappingImeIdToMediumLenNameResourceId[i].resource_id);
461 return GetInputMethodShortName(input_method);
464 string16 InputMethodUtil::GetInputMethodLongName(
465 const InputMethodDescriptor& input_method) const {
466 if (!input_method.name().empty()) {
467 // If the descriptor has a name, use it.
468 return UTF8ToUTF16(input_method.name());
471 // We don't show language here. Name of keyboard layout or input method
472 // usually imply (or explicitly include) its language.
474 // Special case for German, French and Dutch: these languages have multiple
475 // keyboard layouts and share the same layout of keyboard (Belgian). We need
476 // to show explicitly the language for the layout. For Arabic, Amharic, and
477 // Indic languages: they share "Standard Input Method".
478 const string16 standard_input_method_text = l10n_util::GetStringUTF16(
479 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD);
480 const std::string language_code = input_method.language_code();
482 string16 text = TranslateString(input_method.id());
483 if (text == standard_input_method_text ||
484 language_code == "de" ||
485 language_code == "fr" ||
486 language_code == "nl") {
487 text = GetLanguageName(language_code) + UTF8ToUTF16(" - ") + text;
490 DCHECK(!text.empty());
491 return text;
494 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId(
495 const std::string& input_method_id) const {
496 InputMethodIdToDescriptorMap::const_iterator iter
497 = id_to_descriptor_.find(input_method_id);
498 return (iter == id_to_descriptor_.end()) ? NULL : &(iter->second);
501 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromXkbId(
502 const std::string& xkb_id) const {
503 InputMethodIdToDescriptorMap::const_iterator iter
504 = xkb_id_to_descriptor_.find(xkb_id);
505 return (iter == xkb_id_to_descriptor_.end()) ? NULL : &(iter->second);
508 // static
509 string16 InputMethodUtil::GetLanguageDisplayNameFromCode(
510 const std::string& language_code) {
511 if (!g_browser_process) {
512 return string16();
514 return l10n_util::GetDisplayNameForLocale(
515 language_code, g_browser_process->GetApplicationLocale(), true);
518 // static
519 string16 InputMethodUtil::GetLanguageNativeDisplayNameFromCode(
520 const std::string& language_code) {
521 return l10n_util::GetDisplayNameForLocale(language_code, language_code, true);
524 // static
525 void InputMethodUtil::SortLanguageCodesByNames(
526 std::vector<std::string>* language_codes) {
527 if (!g_browser_process) {
528 return;
530 // We should build collator outside of the comparator. We cannot have
531 // scoped_ptr<> in the comparator for a subtle STL reason.
532 UErrorCode error = U_ZERO_ERROR;
533 icu::Locale locale(g_browser_process->GetApplicationLocale().c_str());
534 scoped_ptr<icu::Collator> collator(
535 icu::Collator::createInstance(locale, error));
536 if (U_FAILURE(error)) {
537 collator.reset();
539 std::sort(language_codes->begin(), language_codes->end(),
540 CompareLanguageCodesByLanguageName(collator.get()));
543 bool InputMethodUtil::GetInputMethodIdsFromLanguageCode(
544 const std::string& normalized_language_code,
545 InputMethodType type,
546 std::vector<std::string>* out_input_method_ids) const {
547 return GetInputMethodIdsFromLanguageCodeInternal(
548 language_code_to_ids_,
549 normalized_language_code, type, out_input_method_ids);
552 bool InputMethodUtil::GetInputMethodIdsFromLanguageCodeInternal(
553 const std::multimap<std::string, std::string>& language_code_to_ids,
554 const std::string& normalized_language_code,
555 InputMethodType type,
556 std::vector<std::string>* out_input_method_ids) const {
557 DCHECK(out_input_method_ids);
558 out_input_method_ids->clear();
560 bool result = false;
561 std::pair<LanguageCodeToIdsMap::const_iterator,
562 LanguageCodeToIdsMap::const_iterator> range =
563 language_code_to_ids.equal_range(normalized_language_code);
564 for (LanguageCodeToIdsMap::const_iterator iter = range.first;
565 iter != range.second; ++iter) {
566 const std::string& input_method_id = iter->second;
567 if ((type == kAllInputMethods) || IsKeyboardLayout(input_method_id)) {
568 out_input_method_ids->push_back(input_method_id);
569 result = true;
572 if ((type == kAllInputMethods) && !result) {
573 DVLOG(1) << "Unknown language code: " << normalized_language_code;
575 return result;
578 void InputMethodUtil::GetFirstLoginInputMethodIds(
579 const std::string& language_code,
580 const InputMethodDescriptor& current_input_method,
581 std::vector<std::string>* out_input_method_ids) const {
582 out_input_method_ids->clear();
584 // First, add the current keyboard layout (one used on the login screen).
585 out_input_method_ids->push_back(current_input_method.id());
587 // Second, find the most popular input method associated with the
588 // current UI language. The input method IDs returned from
589 // GetInputMethodIdsFromLanguageCode() are sorted by popularity, hence
590 // our basic strategy is to pick the first one, but it's a bit more
591 // complicated as shown below.
592 std::string most_popular_id;
593 std::vector<std::string> input_method_ids;
594 // This returns the input methods sorted by popularity.
595 GetInputMethodIdsFromLanguageCode(
596 language_code, kAllInputMethods, &input_method_ids);
597 for (size_t i = 0; i < input_method_ids.size(); ++i) {
598 const std::string& input_method_id = input_method_ids[i];
599 // Pick the first one.
600 if (most_popular_id.empty())
601 most_popular_id = input_method_id;
603 // Check if there is one that matches the current keyboard layout, but
604 // not the current keyboard itself. This is useful if there are
605 // multiple keyboard layout choices for one input method. For
606 // instance, Mozc provides three choices: mozc (US keyboard), mozc-jp
607 // (JP keyboard), mozc-dv (Dvorak).
608 const InputMethodDescriptor* descriptor =
609 GetInputMethodDescriptorFromId(input_method_id);
610 if (descriptor &&
611 descriptor->id() != current_input_method.id() &&
612 descriptor->keyboard_layout() ==
613 current_input_method.keyboard_layout()) {
614 most_popular_id = input_method_id;
615 break;
618 // Add the most popular input method ID, if it's different from the
619 // current input method.
620 if (most_popular_id != current_input_method.id() &&
621 // TODO(yusukes): Remove this hack when we remove the "english-m" IME.
622 most_popular_id != "english-m") {
623 out_input_method_ids->push_back(most_popular_id);
627 void InputMethodUtil::GetLanguageCodesFromInputMethodIds(
628 const std::vector<std::string>& input_method_ids,
629 std::vector<std::string>* out_language_codes) const {
630 out_language_codes->clear();
632 for (size_t i = 0; i < input_method_ids.size(); ++i) {
633 const std::string& input_method_id = input_method_ids[i];
634 const InputMethodDescriptor* input_method =
635 GetInputMethodDescriptorFromId(input_method_id);
636 if (!input_method) {
637 DVLOG(1) << "Unknown input method ID: " << input_method_ids[i];
638 continue;
640 const std::string language_code = input_method->language_code();
641 // Add it if it's not already present.
642 if (std::count(out_language_codes->begin(), out_language_codes->end(),
643 language_code) == 0) {
644 out_language_codes->push_back(language_code);
649 std::string InputMethodUtil::GetHardwareInputMethodId() const {
650 if (!hardware_input_method_id_for_testing_.empty()) {
651 return hardware_input_method_id_for_testing_;
654 if (!(g_browser_process && g_browser_process->local_state())) {
655 // This shouldn't happen but just in case.
656 DVLOG(1) << "Local state is not yet ready";
657 return InputMethodDescriptor::GetFallbackInputMethodDescriptor().id();
660 PrefService* local_state = g_browser_process->local_state();
661 if (!local_state->FindPreference(prefs::kHardwareKeyboardLayout)) {
662 // This could happen in unittests. We register the preference in
663 // BrowserMain::InitializeLocalState and that method is not called during
664 // unittests.
665 DVLOG(1) << prefs::kHardwareKeyboardLayout << " is not registered";
666 return InputMethodDescriptor::GetFallbackInputMethodDescriptor().id();
669 const std::string input_method_id =
670 local_state->GetString(prefs::kHardwareKeyboardLayout);
671 if (input_method_id.empty()) {
672 // This is totally fine if it's empty. The hardware keyboard layout is
673 // not stored if startup_manifest.json (OEM customization data) is not
674 // present (ex. Cr48 doen't have that file).
675 return InputMethodDescriptor::GetFallbackInputMethodDescriptor().id();
677 return input_method_id;
680 void InputMethodUtil::ReloadInternalMaps() {
681 if (supported_input_methods_->size() <= 1) {
682 DVLOG(1) << "GetSupportedInputMethods returned a fallback ID";
683 // TODO(yusukes): Handle this error in nicer way.
686 // Clear the existing maps.
687 language_code_to_ids_.clear();
688 id_to_language_code_.clear();
689 id_to_descriptor_.clear();
690 xkb_id_to_descriptor_.clear();
692 for (size_t i = 0; i < supported_input_methods_->size(); ++i) {
693 const InputMethodDescriptor& input_method =
694 supported_input_methods_->at(i);
695 const std::string language_code = input_method.language_code();
696 language_code_to_ids_.insert(
697 std::make_pair(language_code, input_method.id()));
698 // Remember the pairs.
699 id_to_language_code_.insert(
700 std::make_pair(input_method.id(), language_code));
701 id_to_descriptor_.insert(
702 std::make_pair(input_method.id(), input_method));
703 if (IsKeyboardLayout(input_method.id())) {
704 xkb_id_to_descriptor_.insert(
705 std::make_pair(input_method.keyboard_layout(), input_method));
709 // Go through the languages listed in kExtraLanguages.
710 for (size_t i = 0; i < kExtraLanguagesLength; ++i) {
711 const char* language_code = kExtraLanguages[i].language_code;
712 const char* input_method_id = kExtraLanguages[i].input_method_id;
713 InputMethodIdToDescriptorMap::const_iterator iter =
714 id_to_descriptor_.find(input_method_id);
715 // If the associated input method descriptor is found, add the language
716 // code and the input method.
717 if (iter != id_to_descriptor_.end()) {
718 const InputMethodDescriptor& input_method = iter->second;
719 language_code_to_ids_.insert(
720 std::make_pair(language_code, input_method.id()));
725 void InputMethodUtil::OnLocaleChanged() {
726 ReloadInternalMaps();
729 void InputMethodUtil::SetHardwareInputMethodIdForTesting(
730 const std::string& input_method_id) {
731 hardware_input_method_id_for_testing_ = input_method_id;
734 } // namespace input_method
735 } // namespace chromeos