| 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> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 l10n_util::GetStringF( | 49 l10n_util::GetStringF( |
| 50 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE, | 50 IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE, |
| 51 l10n_util::GetString(IDS_PRODUCT_OS_NAME))); | 51 l10n_util::GetString(IDS_PRODUCT_OS_NAME))); |
| 52 localized_strings->SetString(L"display_in_this_language", | 52 localized_strings->SetString(L"display_in_this_language", |
| 53 l10n_util::GetStringF( | 53 l10n_util::GetStringF( |
| 54 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE, | 54 IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE, |
| 55 l10n_util::GetString(IDS_PRODUCT_OS_NAME))); | 55 l10n_util::GetString(IDS_PRODUCT_OS_NAME))); |
| 56 localized_strings->SetString(L"restart_required", | 56 localized_strings->SetString(L"restart_required", |
| 57 l10n_util::GetString(IDS_OPTIONS_RESTART_REQUIRED)); | 57 l10n_util::GetString(IDS_OPTIONS_RESTART_REQUIRED)); |
| 58 | 58 |
| 59 // GetSupportedInputMethods() never return NULL. |
| 60 scoped_ptr<InputMethodDescriptors> descriptors( |
| 61 CrosLibrary::Get()->GetInputMethodLibrary()->GetSupportedInputMethods()); |
| 62 |
| 59 // The followigns are resources, rather than local strings. | 63 // The followigns are resources, rather than local strings. |
| 60 localized_strings->SetString(L"currentUiLanguageCode", | 64 localized_strings->SetString(L"currentUiLanguageCode", |
| 61 UTF8ToWide(g_browser_process->GetApplicationLocale())); | 65 UTF8ToWide(g_browser_process->GetApplicationLocale())); |
| 62 localized_strings->Set(L"inputMethodList", GetInputMethodList()); | 66 localized_strings->Set(L"inputMethodList", GetInputMethodList(*descriptors)); |
| 63 localized_strings->Set(L"languageList", GetLanguageList()); | 67 localized_strings->Set(L"languageList", GetLanguageList(*descriptors)); |
| 64 } | 68 } |
| 65 | 69 |
| 66 void LanguageOptionsHandler::RegisterMessages() { | 70 void LanguageOptionsHandler::RegisterMessages() { |
| 67 DCHECK(dom_ui_); | 71 DCHECK(dom_ui_); |
| 68 dom_ui_->RegisterMessageCallback("uiLanguageChange", | 72 dom_ui_->RegisterMessageCallback("uiLanguageChange", |
| 69 NewCallback(this, | 73 NewCallback(this, |
| 70 &LanguageOptionsHandler::UiLanguageChangeCallback)); | 74 &LanguageOptionsHandler::UiLanguageChangeCallback)); |
| 71 } | 75 } |
| 72 | 76 |
| 73 ListValue* LanguageOptionsHandler::GetInputMethodList() { | 77 ListValue* LanguageOptionsHandler::GetInputMethodList( |
| 78 const InputMethodDescriptors& descriptors) { |
| 74 ListValue* input_method_list = new ListValue(); | 79 ListValue* input_method_list = new ListValue(); |
| 75 | 80 |
| 76 // GetSupportedLanguages() never return NULL. | 81 for (size_t i = 0; i < descriptors.size(); ++i) { |
| 77 scoped_ptr<InputMethodDescriptors> descriptors( | 82 const InputMethodDescriptor& descriptor = descriptors[i]; |
| 78 CrosLibrary::Get()->GetInputMethodLibrary()->GetSupportedInputMethods()); | |
| 79 for (size_t i = 0; i < descriptors->size(); ++i) { | |
| 80 const InputMethodDescriptor& descriptor = descriptors->at(i); | |
| 81 const std::string language_code = | 83 const std::string language_code = |
| 82 input_method::GetLanguageCodeFromDescriptor(descriptor); | 84 input_method::GetLanguageCodeFromDescriptor(descriptor); |
| 83 const std::string display_name = | 85 const std::string display_name = |
| 84 input_method::GetInputMethodDisplayNameFromId(descriptor.id); | 86 input_method::GetInputMethodDisplayNameFromId(descriptor.id); |
| 85 | 87 |
| 86 DictionaryValue* dictionary = new DictionaryValue(); | 88 DictionaryValue* dictionary = new DictionaryValue(); |
| 87 dictionary->SetString(L"id", UTF8ToWide(descriptor.id)); | 89 dictionary->SetString(L"id", UTF8ToWide(descriptor.id)); |
| 88 dictionary->SetString(L"displayName", UTF8ToWide(display_name)); | 90 dictionary->SetString(L"displayName", UTF8ToWide(display_name)); |
| 89 dictionary->SetString(L"languageCode", UTF8ToWide(language_code)); | 91 dictionary->SetString(L"languageCode", UTF8ToWide(language_code)); |
| 90 input_method_list->Append(dictionary); | 92 input_method_list->Append(dictionary); |
| 91 } | 93 } |
| 92 | 94 |
| 93 return input_method_list; | 95 return input_method_list; |
| 94 } | 96 } |
| 95 | 97 |
| 96 ListValue* LanguageOptionsHandler::GetLanguageList() { | 98 ListValue* LanguageOptionsHandler::GetLanguageList( |
| 99 const InputMethodDescriptors& descriptors) { |
| 100 std::vector<std::string> language_codes; |
| 101 // Collect the language codes from the supported input methods. |
| 102 for (size_t i = 0; i < descriptors.size(); ++i) { |
| 103 const InputMethodDescriptor& descriptor = descriptors[i]; |
| 104 const std::string language_code = |
| 105 input_method::GetLanguageCodeFromDescriptor(descriptor); |
| 106 language_codes.push_back(language_code); |
| 107 } |
| 108 // Collect the language codes from kExtraLanguages. |
| 109 for (size_t i = 0; i < arraysize(input_method::kExtraLanguages); ++i) { |
| 110 const char* language_code = |
| 111 input_method::kExtraLanguages[i].language_code; |
| 112 language_codes.push_back(language_code); |
| 113 } |
| 114 |
| 97 // Map of display name -> {language code, native_display_name}. | 115 // Map of display name -> {language code, native_display_name}. |
| 98 // In theory, we should be able to create a map that is sorted by | 116 // 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 | 117 // display names using ICU comparator, but doing it is hard, thus we'll |
| 100 // use an auxiliary vector to achieve the same result. | 118 // use an auxiliary vector to achieve the same result. |
| 101 typedef std::pair<std::string, std::wstring> LanguagePair; | 119 typedef std::pair<std::string, std::wstring> LanguagePair; |
| 102 typedef std::map<std::wstring, LanguagePair> LanguageMap; | 120 typedef std::map<std::wstring, LanguagePair> LanguageMap; |
| 103 LanguageMap language_map; | 121 LanguageMap language_map; |
| 104 // The auxiliary vector mentioned above. | 122 // The auxiliary vector mentioned above. |
| 105 std::vector<std::wstring> display_names; | 123 std::vector<std::wstring> display_names; |
| 106 | 124 |
| 107 // Build the list of display names, and build the language map. | 125 // Build the list of display names, and build the language map. |
| 108 const std::vector<std::string>& locales = l10n_util::GetAvailableLocales(); | 126 for (size_t i = 0; i < language_codes.size(); ++i) { |
| 109 for (size_t i = 0; i < locales.size(); ++i) { | |
| 110 const std::wstring display_name = | 127 const std::wstring display_name = |
| 111 input_method::GetLanguageDisplayNameFromCode(locales[i]); | 128 input_method::GetLanguageDisplayNameFromCode(language_codes[i]); |
| 112 const std::wstring native_display_name = | 129 const std::wstring native_display_name = |
| 113 input_method::GetLanguageNativeDisplayNameFromCode( | 130 input_method::GetLanguageNativeDisplayNameFromCode( |
| 114 locales[i]); | 131 language_codes[i]); |
| 115 display_names.push_back(display_name); | 132 display_names.push_back(display_name); |
| 116 language_map[display_name] = | 133 language_map[display_name] = |
| 117 std::make_pair(locales[i], native_display_name); | 134 std::make_pair(language_codes[i], native_display_name); |
| 118 } | 135 } |
| 119 DCHECK_EQ(display_names.size(), language_map.size()); | 136 DCHECK_EQ(display_names.size(), language_map.size()); |
| 120 | 137 |
| 121 // Sort display names using locale specific sorter. | 138 // Sort display names using locale specific sorter. |
| 122 l10n_util::SortStrings(g_browser_process->GetApplicationLocale(), | 139 l10n_util::SortStrings(g_browser_process->GetApplicationLocale(), |
| 123 &display_names); | 140 &display_names); |
| 124 | 141 |
| 125 // Build the language list from the language map. | 142 // Build the language list from the language map. |
| 126 ListValue* language_list = new ListValue(); | 143 ListValue* language_list = new ListValue(); |
| 127 for (size_t i = 0; i < display_names.size(); ++i) { | 144 for (size_t i = 0; i < display_names.size(); ++i) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 152 return; | 169 return; |
| 153 } | 170 } |
| 154 PrefService* prefs = g_browser_process->local_state(); | 171 PrefService* prefs = g_browser_process->local_state(); |
| 155 prefs->SetString(prefs::kApplicationLocale, language_code); | 172 prefs->SetString(prefs::kApplicationLocale, language_code); |
| 156 prefs->SavePersistentPrefs(); | 173 prefs->SavePersistentPrefs(); |
| 157 dom_ui_->CallJavascriptFunction( | 174 dom_ui_->CallJavascriptFunction( |
| 158 L"options.LanguageOptions.uiLanguageSaved"); | 175 L"options.LanguageOptions.uiLanguageSaved"); |
| 159 } | 176 } |
| 160 | 177 |
| 161 } // namespace chromeos | 178 } // namespace chromeos |
| OLD | NEW |