• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_
6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_
7 #pragma once
8 
9 #include <map>
10 #include <string>
11 #include <vector>
12 
13 #include "base/string16.h"
14 #include "chrome/browser/chromeos/cros/input_method_library.h"
15 
16 namespace chromeos {
17 namespace input_method {
18 
19 // The list of language that do not have associated input methods in IBus.
20 // For these languages, we associate input methods here.
21 const struct ExtraLanguage {
22   const char* language_code;
23   const char* input_method_id;
24 } kExtraLanguages[] = {
25   { "en-AU", "xkb:us::eng" },  // For Austrailia, use US keyboard layout.
26   { "id", "xkb:us::eng" }, // For Indonesian, use US keyboard layout.
27   // The code "fil" comes from app/l10_util.cc.
28   { "fil", "xkb:us::eng" },  // For Filipino, use US keyboard layout.
29   // The code "es-419" comes from app/l10_util.cc.
30   // For Spanish in Latin America, use Latin American keyboard layout.
31   { "es-419", "xkb:latam::spa" },
32 };
33 // TODO(yusukes): Move |kExtraLanguages| to input_method_util.cc.
34 
35 // Converts a string sent from IBus IME engines, which is written in English,
36 // into Chrome's string ID, then pulls internationalized resource string from
37 // the resource bundle and returns it. These functions are not thread-safe.
38 // Non-UI threads are not allowed to call them.
39 std::wstring GetString(
40     const std::string& english_string, const std::string& input_method_id);
41 std::string GetStringUTF8(
42     const std::string& english_string, const std::string& input_method_id);
43 string16 GetStringUTF16(
44     const std::string& english_string, const std::string& input_method_id);
45 
46 // This method is ONLY for unit testing. Returns true if the given string is
47 // supported (i.e. the string is associated with a resource ID).
48 bool StringIsSupported(const std::string& english_string,
49                        const std::string& input_method_id);
50 
51 // Normalizes the language code and returns the normalized version.  The
52 // function normalizes the given language code to be compatible with the
53 // one used in Chrome's application locales. Otherwise, returns the
54 // given language code as-is.
55 //
56 // Examples:
57 //
58 // - "zh_CN" => "zh-CN" (Use - instead of _)
59 // - "jpn"   => "ja"    (Use two-letter code)
60 // - "t"     => "t"     (Return as-is if unknown)
61 std::string NormalizeLanguageCode(const std::string& language_code);
62 
63 // Returns true if the given input method id is for a keyboard layout.
64 bool IsKeyboardLayout(const std::string& input_method_id);
65 
66 // Gets the language code from the given input method descriptor.  This
67 // encapsulates differences between the language codes used in
68 // InputMethodDescriptor and Chrome's application locale codes.
69 std::string GetLanguageCodeFromDescriptor(
70     const InputMethodDescriptor& descriptor);
71 
72 // Gets the keyboard layout name from the given input method ID.
73 // If the ID is invalid, an empty string will be returned.
74 // This function only supports xkb layouts.
75 //
76 // Examples:
77 //
78 // "xkb:us::eng"       => "us"
79 // "xkb:us:dvorak:eng" => "us(dvorak)"
80 // "xkb:gb::eng"       => "gb"
81 // "pinyin"            => "us" (because Pinyin uses US keyboard layout)
82 std::string GetKeyboardLayoutName(const std::string& input_method_id);
83 
84 // Gets the ID for the keyboard overlay from the given input method ID.
85 // If the ID is invalid, an empty string will be returned.
86 //
87 // Examples:
88 //
89 // "us"                => "en_US"
90 // "us(dvorak)"        => "en_US_dvorak"
91 // "gb"                => "en_GB"
92 std::string GetKeyboardOverlayId(const std::string& input_method_name);
93 
94 // Converts an input method ID to a language code of the IME. Returns "Eng"
95 // when |input_method_id| is unknown.
96 // Example: "hangul" => "ko"
97 std::string GetLanguageCodeFromInputMethodId(
98     const std::string& input_method_id);
99 
100 // Converts an input method ID to a display name of the IME. Returns
101 // an empty strng when |input_method_id| is unknown.
102 // Examples: "pinyin" => "Pinyin"
103 //           "m17n:ar:kbd" => "kbd (m17n)"
104 std::string GetInputMethodDisplayNameFromId(const std::string& input_method_id);
105 
106 // Converts an input method ID to an input method descriptor. Returns NULL
107 // when |input_method_id| is unknown.
108 // Example: "pinyin" => { id: "pinyin", display_name: "Pinyin",
109 //                        keyboard_layout: "us", language_code: "zh" }
110 const chromeos::InputMethodDescriptor* GetInputMethodDescriptorFromId(
111     const std::string& input_method_id);
112 
113 // Converts a language code to a language display name, using the
114 // current application locale. MaybeRewriteLanguageName() is called
115 // internally.
116 // Examples: "fi"    => "Finnish"
117 //           "en-US" => "English (United States)"
118 string16 GetLanguageDisplayNameFromCode(const std::string& language_code);
119 
120 // Converts a language code to a language native display name.
121 // MaybeRewriteLanguageName() is called internally.
122 // Examples: "fi"    => "suomi" (rather than Finnish)
123 //           "en-US" => "English (United States)"
124 string16 GetLanguageNativeDisplayNameFromCode(const std::string& language_code);
125 
126 // Sorts the given language codes by their corresponding language names,
127 // using the unicode string comparator. Uses unstable sorting.
128 void SortLanguageCodesByNames(std::vector<std::string>* language_codes);
129 
130 // Used for EnableInputMethods() etc.
131 enum InputMethodType {
132   kKeyboardLayoutsOnly,
133   kAllInputMethods,
134 };
135 
136 // Gets input method IDs that belong to |language_code|.
137 // If |type| is |kKeyboardLayoutsOnly|, the function does not return input
138 // methods that are not for keybord layout switching. Returns true on success.
139 // Note that the function might return false or |language_code| is unknown.
140 //
141 // The retured input method IDs are sorted by populalirty per
142 // chromeos/platform/assets/input_methods/whitelist.txt in production.
143 // For testing with the stub libcros, the list in
144 // GetInputMethodDescriptorsForTesting() in input_method_library.cc will
145 // be used.
146 bool GetInputMethodIdsFromLanguageCode(
147     const std::string& language_code,
148     InputMethodType type,
149     std::vector<std::string>* out_input_method_ids);
150 
151 // Gets the input method IDs suitable for the first user login, based on
152 // the given language code (UI language), and the descriptor of the
153 // current input method.
154 void GetFirstLoginInputMethodIds(
155     const std::string& language_code,
156     const InputMethodDescriptor& current_input_method,
157     std::vector<std::string>* out_input_method_ids);
158 
159 // Gets the language codes associated with the given input method IDs.
160 // The returned language codes won't have duplicates.
161 void GetLanguageCodesFromInputMethodIds(
162     const std::vector<std::string>& input_method_ids,
163     std::vector<std::string>* out_language_codes);
164 
165 // Enables input methods (e.g. Chinese, Japanese) and keyboard layouts (e.g.
166 // US qwerty, US dvorak, French azerty) that are necessary for the language code
167 // and then switches to |initial_input_method_id| if the string is not empty.
168 // For example, if |language_code| is "en-US", US qwerty and US dvorak layouts
169 // would be enabled. Likewise, for Germany locale, US qwerty layout and several
170 // keyboard layouts for Germany would be enabled.
171 // If |type| is kAllInputMethods, all keyboard layouts and all input methods
172 // are enabled. If it's kKeyboardLayoutsOnly, only keyboard layouts are enabled.
173 // For example, for Japanese, xkb:jp::jpn is enabled when kKeyboardLayoutsOnly,
174 // and xkb:jp::jpn, mozc, mozc-jp, mozc-dv are enabled when kAllInputMethods.
175 //
176 // Note that this function does not save the input methods in the user's
177 // preferences, as this function is designed for the login screen and the
178 // screen locker, where we shouldn't change the user's preferences.
179 void EnableInputMethods(const std::string& language_code, InputMethodType type,
180                         const std::string& initial_input_method_id);
181 
182 // Returns the input method ID of the hardware keyboard.
183 std::string GetHardwareInputMethodId();
184 
185 // Returns the fallback input method descriptor (the very basic US
186 // keyboard). This function is mostly used for testing, but may be used
187 // as the fallback, when there is no other choice.
188 InputMethodDescriptor GetFallbackInputMethodDescriptor();
189 
190 // This function should be called when Chrome's application locale is
191 // changed, so that the internal maps of this library is reloaded.
192 void OnLocaleChanged();
193 
194 bool GetInputMethodIdsFromLanguageCodeInternal(
195     const std::multimap<std::string, std::string>& language_code_to_ids,
196     const std::string& normalized_language_code,
197     InputMethodType type,
198     std::vector<std::string>* out_input_method_ids);
199 
200 void ReloadInternalMaps();
201 
202 }  // namespace input_method
203 }  // namespace chromeos
204 
205 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_UTIL_H_
206