| Index: chrome/browser/ui/webui/chromeos/login/l10n_util.cc
|
| diff --git a/chrome/browser/ui/webui/chromeos/login/l10n_util.cc b/chrome/browser/ui/webui/chromeos/login/l10n_util.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..57235fb6d34d9b8dbc9e904e745fca240b396242
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/webui/chromeos/login/l10n_util.cc
|
| @@ -0,0 +1,170 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/ui/webui/chromeos/login/l10n_util.h"
|
| +
|
| +#include <set>
|
| +#include <utility>
|
| +
|
| +#include "base/basictypes.h"
|
| +#include "base/logging.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "base/values.h"
|
| +#include "chrome/browser/chromeos/input_method/input_method_util.h"
|
| +#include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler.h"
|
| +#include "chromeos/ime/component_extension_ime_manager.h"
|
| +#include "chromeos/ime/input_method_descriptor.h"
|
| +#include "chromeos/ime/input_method_manager.h"
|
| +#include "grit/generated_resources.h"
|
| +#include "ui/base/l10n/l10n_util.h"
|
| +
|
| +namespace chromeos {
|
| +
|
| +namespace {
|
| +
|
| +base::DictionaryValue* CreateInputMethodsEntry(
|
| + const input_method::InputMethodDescriptor& method,
|
| + const std::string selected) {
|
| + input_method::InputMethodUtil* util =
|
| + input_method::InputMethodManager::Get()->GetInputMethodUtil();
|
| + const std::string& ime_id = method.id();
|
| + scoped_ptr<base::DictionaryValue> input_method(new base::DictionaryValue);
|
| + input_method->SetString("value", ime_id);
|
| + input_method->SetString("title", util->GetInputMethodLongName(method));
|
| + input_method->SetBoolean("selected", ime_id == selected);
|
| + return input_method.release();
|
| +}
|
| +
|
| +// Returns true if element was inserted.
|
| +bool InsertString(const std::string& str, std::set<std::string>& to) {
|
| + const std::pair<std::set<std::string>::iterator, bool> result =
|
| + to.insert(str);
|
| + return result.second;
|
| +}
|
| +
|
| +void AddOptgroupOtherLayouts(base::ListValue* input_methods_list) {
|
| + scoped_ptr<base::DictionaryValue> optgroup(new base::DictionaryValue);
|
| + optgroup->SetString(
|
| + "optionGroupName",
|
| + l10n_util::GetStringUTF16(IDS_OOBE_OTHER_KEYBOARD_LAYOUTS));
|
| + input_methods_list->Append(optgroup.release());
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +scoped_ptr<base::ListValue> GetUILanguageList(
|
| + const std::vector<std::string>* most_relevant_language_codes,
|
| + const std::string& selected) {
|
| + ComponentExtensionIMEManager* manager =
|
| + input_method::InputMethodManager::Get()->
|
| + GetComponentExtensionIMEManager();
|
| + input_method::InputMethodDescriptors descriptors;
|
| + if (manager->IsInitialized())
|
| + descriptors = manager->GetXkbIMEAsInputMethodDescriptor();
|
| + scoped_ptr<base::ListValue> languages_list(
|
| + options::CrosLanguageOptionsHandler::GetUILanguageList(
|
| + descriptors,
|
| + most_relevant_language_codes));
|
| + for (size_t i = 0; i < languages_list->GetSize(); ++i) {
|
| + base::DictionaryValue* language_info = NULL;
|
| + if (!languages_list->GetDictionary(i, &language_info))
|
| + NOTREACHED();
|
| +
|
| + std::string value;
|
| + language_info->GetString("code", &value);
|
| + std::string display_name;
|
| + language_info->GetString("displayName", &display_name);
|
| + std::string native_name;
|
| + language_info->GetString("nativeDisplayName", &native_name);
|
| +
|
| + // If it's an option group divider, add field name.
|
| + if (value == options::kMostRelevantLanguagesDivider) {
|
| + language_info->SetString(
|
| + "optionGroupName",
|
| + l10n_util::GetStringUTF16(IDS_OOBE_OTHER_LANGUAGES));
|
| + }
|
| + if (display_name != native_name) {
|
| + display_name = base::StringPrintf("%s - %s",
|
| + display_name.c_str(),
|
| + native_name.c_str());
|
| + }
|
| +
|
| + language_info->SetString("value", value);
|
| + language_info->SetString("title", display_name);
|
| + if (value == selected)
|
| + language_info->SetBoolean("selected", true);
|
| + }
|
| + return languages_list.Pass();
|
| +}
|
| +
|
| +scoped_ptr<base::ListValue> GetLoginKeyboardLayouts(
|
| + const std::string& locale,
|
| + const std::string& selected) {
|
| + scoped_ptr<base::ListValue> input_methods_list(new base::ListValue);
|
| + input_method::InputMethodManager* manager =
|
| + input_method::InputMethodManager::Get();
|
| + input_method::InputMethodUtil* util = manager->GetInputMethodUtil();
|
| + if (!manager->GetComponentExtensionIMEManager()->IsInitialized()) {
|
| + input_method::InputMethodDescriptor fallback =
|
| + util->GetFallbackInputMethodDescriptor();
|
| + input_methods_list->Append(
|
| + CreateInputMethodsEntry(fallback, fallback.id()));
|
| + return input_methods_list.Pass();
|
| + }
|
| +
|
| + const std::vector<std::string>& hardware_login_input_methods =
|
| + util->GetHardwareLoginInputMethodIds();
|
| + manager->EnableLoginLayouts(locale, hardware_login_input_methods);
|
| +
|
| + scoped_ptr<input_method::InputMethodDescriptors> input_methods(
|
| + manager->GetActiveInputMethods());
|
| + std::set<std::string> input_methods_added;
|
| +
|
| + for (std::vector<std::string>::const_iterator i =
|
| + hardware_login_input_methods.begin();
|
| + i != hardware_login_input_methods.end();
|
| + ++i) {
|
| + const input_method::InputMethodDescriptor* ime =
|
| + util->GetInputMethodDescriptorFromId(*i);
|
| + // Do not crash in case of misconfiguration.
|
| + if (ime) {
|
| + input_methods_added.insert(*i);
|
| + input_methods_list->Append(CreateInputMethodsEntry(*ime, selected));
|
| + } else {
|
| + NOTREACHED();
|
| + }
|
| + }
|
| +
|
| + bool optgroup_added = false;
|
| + for (size_t i = 0; i < input_methods->size(); ++i) {
|
| + // Makes sure the id is in legacy xkb id format.
|
| + const std::string& ime_id = (*input_methods)[i].id();
|
| + if (!InsertString(ime_id, input_methods_added))
|
| + continue;
|
| + if (!optgroup_added) {
|
| + optgroup_added = true;
|
| + AddOptgroupOtherLayouts(input_methods_list.get());
|
| + }
|
| + input_methods_list->Append(CreateInputMethodsEntry((*input_methods)[i],
|
| + selected));
|
| + }
|
| +
|
| + // "xkb:us::eng" should always be in the list of available layouts.
|
| + const std::string us_keyboard_id =
|
| + util->GetFallbackInputMethodDescriptor().id();
|
| + if (input_methods_added.find(us_keyboard_id) == input_methods_added.end()) {
|
| + const input_method::InputMethodDescriptor* us_eng_descriptor =
|
| + util->GetInputMethodDescriptorFromId(us_keyboard_id);
|
| + DCHECK(us_eng_descriptor);
|
| + if (!optgroup_added) {
|
| + optgroup_added = true;
|
| + AddOptgroupOtherLayouts(input_methods_list.get());
|
| + }
|
| + input_methods_list->Append(CreateInputMethodsEntry(*us_eng_descriptor,
|
| + selected));
|
| + }
|
| + return input_methods_list.Pass();
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|