OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/dom_ui/language_options_handler.h" | 5 #include "chrome/browser/chromeos/dom_ui/language_options_handler.h" |
6 | 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 |
7 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
8 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
9 #include "base/values.h" | 13 #include "base/values.h" |
10 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/chromeos/cros/cros_library.h" | 15 #include "chrome/browser/chromeos/cros/cros_library.h" |
12 #include "chrome/browser/chromeos/cros/input_method_library.h" | 16 #include "chrome/browser/chromeos/cros/input_method_library.h" |
13 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 17 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
14 #include "chrome/browser/pref_service.h" | 18 #include "chrome/browser/pref_service.h" |
15 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
16 #include "grit/chromium_strings.h" | 20 #include "grit/chromium_strings.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 dictionary->SetString(L"id", UTF8ToWide(descriptor.id)); | 87 dictionary->SetString(L"id", UTF8ToWide(descriptor.id)); |
84 dictionary->SetString(L"displayName", UTF8ToWide(display_name)); | 88 dictionary->SetString(L"displayName", UTF8ToWide(display_name)); |
85 dictionary->SetString(L"languageCode", UTF8ToWide(language_code)); | 89 dictionary->SetString(L"languageCode", UTF8ToWide(language_code)); |
86 input_method_list->Append(dictionary); | 90 input_method_list->Append(dictionary); |
87 } | 91 } |
88 | 92 |
89 return input_method_list; | 93 return input_method_list; |
90 } | 94 } |
91 | 95 |
92 ListValue* LanguageOptionsHandler::GetLanguageList() { | 96 ListValue* LanguageOptionsHandler::GetLanguageList() { |
93 ListValue* language_list = new ListValue(); | 97 // Map of display name -> {language code, native_display_name}. |
| 98 // In theory, we should be able to create a map that is sorted by |
| 99 // display names using ICU comparator, but doing it is hard, thus we'll |
| 100 // use an auxiliary vector to achieve the same result. |
| 101 typedef std::pair<std::string, std::wstring> LanguagePair; |
| 102 typedef std::map<std::wstring, LanguagePair> LanguageMap; |
| 103 LanguageMap language_map; |
| 104 // The auxiliary list mentioned above. |
| 105 std::vector<std::wstring> display_names; |
94 | 106 |
| 107 // Build the list of display names, and build the language map. |
95 const std::vector<std::string>& locales = l10n_util::GetAvailableLocales(); | 108 const std::vector<std::string>& locales = l10n_util::GetAvailableLocales(); |
96 for (size_t i = 0; i < locales.size(); ++i) { | 109 for (size_t i = 0; i < locales.size(); ++i) { |
| 110 const std::wstring display_name = |
| 111 chromeos::input_method::GetLanguageDisplayNameFromCode(locales[i]); |
| 112 const std::wstring native_display_name = |
| 113 chromeos::input_method::GetLanguageNativeDisplayNameFromCode( |
| 114 locales[i]); |
| 115 display_names.push_back(display_name); |
| 116 language_map[display_name] = |
| 117 std::make_pair(locales[i], native_display_name); |
| 118 } |
| 119 DCHECK_EQ(display_names.size(), language_map.size()); |
| 120 |
| 121 // Sort display names using locale specific sorter. |
| 122 l10n_util::SortStrings(g_browser_process->GetApplicationLocale(), |
| 123 &display_names); |
| 124 |
| 125 // Build the language list from the language map. |
| 126 ListValue* language_list = new ListValue(); |
| 127 for (size_t i = 0; i < display_names.size(); ++i) { |
| 128 const LanguagePair& pair = language_map[display_names[i]]; |
97 DictionaryValue* dictionary = new DictionaryValue(); | 129 DictionaryValue* dictionary = new DictionaryValue(); |
98 dictionary->SetString(L"code", UTF8ToWide(locales[i])); | 130 dictionary->SetString(L"code", pair.first); |
99 dictionary->SetString(L"displayName", | 131 dictionary->SetString(L"displayName", display_names[i]); |
100 chromeos::input_method::GetLanguageDisplayNameFromCode(locales[i])); | 132 dictionary->SetString(L"nativeDisplayName", pair.second); |
101 dictionary->SetString(L"nativeDisplayName", | |
102 chromeos::input_method::GetLanguageNativeDisplayNameFromCode( | |
103 locales[i])); | |
104 language_list->Append(dictionary); | 133 language_list->Append(dictionary); |
105 } | 134 } |
106 | 135 |
107 return language_list; | 136 return language_list; |
108 } | 137 } |
109 | 138 |
110 void LanguageOptionsHandler::UiLanguageChangeCallback( | 139 void LanguageOptionsHandler::UiLanguageChangeCallback( |
111 const Value* value) { | 140 const Value* value) { |
112 if (!value || !value->IsType(Value::TYPE_LIST)) { | 141 if (!value || !value->IsType(Value::TYPE_LIST)) { |
113 NOTREACHED(); | 142 NOTREACHED(); |
114 LOG(INFO) << "NOTREACHED"; | 143 LOG(INFO) << "NOTREACHED"; |
115 return; | 144 return; |
116 } | 145 } |
117 const ListValue* list_value = static_cast<const ListValue*>(value); | 146 const ListValue* list_value = static_cast<const ListValue*>(value); |
118 std::string language_code; | 147 std::string language_code; |
119 if (list_value->GetSize() != 1 || | 148 if (list_value->GetSize() != 1 || |
120 !list_value->GetString(0, &language_code)) { | 149 !list_value->GetString(0, &language_code)) { |
121 NOTREACHED(); | 150 NOTREACHED(); |
122 LOG(INFO) << "NOTREACHED"; | 151 LOG(INFO) << "NOTREACHED"; |
123 return; | 152 return; |
124 } | 153 } |
125 PrefService* prefs = g_browser_process->local_state(); | 154 PrefService* prefs = g_browser_process->local_state(); |
126 prefs->SetString(prefs::kApplicationLocale, language_code); | 155 prefs->SetString(prefs::kApplicationLocale, language_code); |
127 prefs->SavePersistentPrefs(); | 156 prefs->SavePersistentPrefs(); |
128 dom_ui_->CallJavascriptFunction( | 157 dom_ui_->CallJavascriptFunction( |
129 L"options.LanguageOptions.uiLanguageSaved"); | 158 L"options.LanguageOptions.uiLanguageSaved"); |
130 } | 159 } |
OLD | NEW |