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