1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/webui/options/language_options_handler.h"
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/command_line.h"
15 #include "base/i18n/rtl.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/lifetime/application_lifetime.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/grit/chromium_strings.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "content/public/browser/user_metrics.h"
26 #include "content/public/browser/web_ui.h"
27 #include "ui/base/l10n/l10n_util.h"
29 using base::UserMetricsAction
;
33 LanguageOptionsHandler::LanguageOptionsHandler() {
36 LanguageOptionsHandler::~LanguageOptionsHandler() {
39 void LanguageOptionsHandler::GetLocalizedValues(
40 base::DictionaryValue
* localized_strings
) {
41 LanguageOptionsHandlerCommon::GetLocalizedValues(localized_strings
);
43 RegisterTitle(localized_strings
, "languagePage",
44 IDS_OPTIONS_SETTINGS_LANGUAGES_DIALOG_TITLE
);
45 localized_strings
->SetString("restart_button",
46 l10n_util::GetStringUTF16(
47 IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON
));
48 localized_strings
->Set("languageList", GetLanguageList());
51 void LanguageOptionsHandler::RegisterMessages() {
52 LanguageOptionsHandlerCommon::RegisterMessages();
54 web_ui()->RegisterMessageCallback("uiLanguageRestart",
55 base::Bind(&LanguageOptionsHandler::RestartCallback
,
56 base::Unretained(this)));
59 base::ListValue
* LanguageOptionsHandler::GetLanguageList() {
60 // Collect the language codes from the supported accept-languages.
61 const std::string app_locale
= g_browser_process
->GetApplicationLocale();
62 std::vector
<std::string
> language_codes
;
63 l10n_util::GetAcceptLanguagesForLocale(app_locale
, &language_codes
);
65 // Map of display name -> {language code, native_display_name}.
66 // In theory, we should be able to create a map that is sorted by
67 // display names using ICU comparator, but doing it is hard, thus we'll
68 // use an auxiliary vector to achieve the same result.
69 typedef std::pair
<std::string
, base::string16
> LanguagePair
;
70 typedef std::map
<base::string16
, LanguagePair
> LanguageMap
;
71 LanguageMap language_map
;
72 // The auxiliary vector mentioned above.
73 std::vector
<base::string16
> display_names
;
75 // Build the list of display names, and build the language map.
76 for (size_t i
= 0; i
< language_codes
.size(); ++i
) {
77 base::string16 display_name
=
78 l10n_util::GetDisplayNameForLocale(language_codes
[i
], app_locale
,
80 base::string16 native_display_name
=
81 l10n_util::GetDisplayNameForLocale(language_codes
[i
], language_codes
[i
],
83 display_names
.push_back(display_name
);
84 language_map
[display_name
] =
85 std::make_pair(language_codes
[i
], native_display_name
);
87 DCHECK_EQ(display_names
.size(), language_map
.size());
89 // Sort display names using locale specific sorter.
90 l10n_util::SortStrings16(app_locale
, &display_names
);
92 // Build the language list from the language map.
93 base::ListValue
* language_list
= new base::ListValue();
94 for (size_t i
= 0; i
< display_names
.size(); ++i
) {
95 base::string16
& display_name
= display_names
[i
];
96 base::string16
adjusted_display_name(display_name
);
97 base::i18n::AdjustStringForLocaleDirection(&adjusted_display_name
);
99 const LanguagePair
& pair
= language_map
[display_name
];
100 base::string16
adjusted_native_display_name(pair
.second
);
101 base::i18n::AdjustStringForLocaleDirection(&adjusted_native_display_name
);
103 bool has_rtl_chars
= base::i18n::StringContainsStrongRTLChars(display_name
);
104 std::string directionality
= has_rtl_chars
? "rtl" : "ltr";
106 base::DictionaryValue
* dictionary
= new base::DictionaryValue();
107 dictionary
->SetString("code", pair
.first
);
108 dictionary
->SetString("displayName", adjusted_display_name
);
109 dictionary
->SetString("textDirection", directionality
);
110 dictionary
->SetString("nativeDisplayName", adjusted_native_display_name
);
111 language_list
->Append(dictionary
);
114 return language_list
;
117 base::string16
LanguageOptionsHandler::GetProductName() {
118 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME
);
121 void LanguageOptionsHandler::SetApplicationLocale(
122 const std::string
& language_code
) {
123 PrefService
* pref_service
= g_browser_process
->local_state();
124 pref_service
->SetString(prefs::kApplicationLocale
, language_code
);
127 void LanguageOptionsHandler::RestartCallback(const base::ListValue
* args
) {
128 content::RecordAction(UserMetricsAction("LanguageOptions_Restart"));
129 chrome::AttemptRestart();
132 } // namespace options