Chromium Code Reviews| 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/language_combobox_model.h" | 5 #include "chrome/browser/language_combobox_model.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/i18n/rtl.h" | 8 #include "base/i18n/rtl.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 const char* locale_code = locale_codes[i].c_str(); | 43 const char* locale_code = locale_codes[i].c_str(); |
| 44 | 44 |
| 45 // TODO(jungshik): Even though these strings are used for the UI, | 45 // TODO(jungshik): Even though these strings are used for the UI, |
| 46 // the old code does not add an RTL mark for RTL locales. Make sure | 46 // the old code does not add an RTL mark for RTL locales. Make sure |
| 47 // that it's ok without that. | 47 // that it's ok without that. |
| 48 string16 name_in_current_ui = | 48 string16 name_in_current_ui = |
| 49 l10n_util::GetDisplayNameForLocale(locale_code, app_locale, false); | 49 l10n_util::GetDisplayNameForLocale(locale_code, app_locale, false); |
| 50 string16 name_native = | 50 string16 name_native = |
| 51 l10n_util::GetDisplayNameForLocale(locale_code, locale_code, false); | 51 l10n_util::GetDisplayNameForLocale(locale_code, locale_code, false); |
| 52 | 52 |
| 53 locale_names_.push_back(UTF16ToWideHack(name_in_current_ui)); | 53 locale_names_.push_back(name_in_current_ui); |
| 54 native_names_[UTF16ToWideHack(name_in_current_ui)] = LocaleData( | 54 native_names_[name_in_current_ui] = |
| 55 UTF16ToWideHack(name_native), locale_codes[i]); | 55 LocaleData(name_native, locale_codes[i]); |
|
Evan Martin
2010/12/24 01:00:42
one line
| |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Sort using locale specific sorter. | 58 // Sort using locale specific sorter. |
| 59 l10n_util::SortStrings(g_browser_process->GetApplicationLocale(), | 59 l10n_util::SortStrings16(g_browser_process->GetApplicationLocale(), |
| 60 &locale_names_); | 60 &locale_names_); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void LanguageList::CopySpecifiedLanguagesUp(const std::string& locale_codes) { | 63 void LanguageList::CopySpecifiedLanguagesUp(const std::string& locale_codes) { |
| 64 DCHECK(!locale_names_.empty()); | 64 DCHECK(!locale_names_.empty()); |
| 65 std::vector<std::string> locale_codes_vector; | 65 std::vector<std::string> locale_codes_vector; |
| 66 base::SplitString(locale_codes, ',', &locale_codes_vector); | 66 base::SplitString(locale_codes, ',', &locale_codes_vector); |
| 67 for (size_t i = 0; i != locale_codes_vector.size(); i++) { | 67 for (size_t i = 0; i != locale_codes_vector.size(); i++) { |
| 68 const int locale_index = GetIndexFromLocale(locale_codes_vector[i]); | 68 const int locale_index = GetIndexFromLocale(locale_codes_vector[i]); |
| 69 CHECK_NE(locale_index, -1); | 69 CHECK_NE(locale_index, -1); |
| 70 locale_names_.insert(locale_names_.begin(), locale_names_[locale_index]); | 70 locale_names_.insert(locale_names_.begin(), locale_names_[locale_index]); |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Overridden from ComboboxModel: | 74 // Overridden from ComboboxModel: |
| 75 int LanguageList::get_languages_count() const { | 75 int LanguageList::get_languages_count() const { |
| 76 return static_cast<int>(locale_names_.size()); | 76 return static_cast<int>(locale_names_.size()); |
| 77 } | 77 } |
| 78 | 78 |
| 79 std::wstring LanguageList::GetLanguageNameAt(int index) const { | 79 string16 LanguageList::GetLanguageNameAt(int index) const { |
| 80 DCHECK(static_cast<int>(locale_names_.size()) > index); | 80 DCHECK(static_cast<int>(locale_names_.size()) > index); |
| 81 LocaleDataMap::const_iterator it = | 81 LocaleDataMap::const_iterator it = |
| 82 native_names_.find(locale_names_[index]); | 82 native_names_.find(locale_names_[index]); |
| 83 DCHECK(it != native_names_.end()); | 83 DCHECK(it != native_names_.end()); |
| 84 | 84 |
| 85 // If the name is the same in the native language and local language, | 85 // If the name is the same in the native language and local language, |
| 86 // don't show it twice. | 86 // don't show it twice. |
| 87 if (it->second.native_name == locale_names_[index]) | 87 if (it->second.native_name == locale_names_[index]) |
| 88 return it->second.native_name; | 88 return it->second.native_name; |
| 89 | 89 |
| 90 // We must add directionality formatting to both the native name and the | 90 // We must add directionality formatting to both the native name and the |
| 91 // locale name in order to avoid text rendering problems such as misplaced | 91 // locale name in order to avoid text rendering problems such as misplaced |
| 92 // parentheses or languages appearing in the wrong order. | 92 // parentheses or languages appearing in the wrong order. |
| 93 std::wstring locale_name = locale_names_[index]; | 93 string16 locale_name = locale_names_[index]; |
| 94 base::i18n::AdjustStringForLocaleDirection(&locale_name); | 94 base::i18n::AdjustStringForLocaleDirection(&locale_name); |
| 95 | 95 |
| 96 std::wstring native_name = it->second.native_name; | 96 string16 native_name = it->second.native_name; |
| 97 base::i18n::AdjustStringForLocaleDirection(&native_name); | 97 base::i18n::AdjustStringForLocaleDirection(&native_name); |
| 98 | 98 |
| 99 // We used to have a localizable template here, but none of translators | 99 // We used to have a localizable template here, but none of translators |
| 100 // changed the format. We also want to switch the order of locale_name | 100 // changed the format. We also want to switch the order of locale_name |
| 101 // and native_name without going back to translators. | 101 // and native_name without going back to translators. |
| 102 std::wstring formatted_item; | 102 std::string formatted_item; |
| 103 base::SStringPrintf(&formatted_item, L"%ls - %ls", locale_name.c_str(), | 103 base::SStringPrintf(&formatted_item, "%s - %s", |
| 104 native_name.c_str()); | 104 UTF16ToUTF8(locale_name).c_str(), |
| 105 UTF16ToUTF8(native_name).c_str()); | |
| 105 if (base::i18n::IsRTL()) | 106 if (base::i18n::IsRTL()) |
| 106 // Somehow combo box (even with LAYOUTRTL flag) doesn't get this | 107 // Somehow combo box (even with LAYOUTRTL flag) doesn't get this |
| 107 // right so we add RTL BDO (U+202E) to set the direction | 108 // right so we add RTL BDO (U+202E) to set the direction |
| 108 // explicitly. | 109 // explicitly. |
| 109 formatted_item.insert(0, L"\x202E"); | 110 formatted_item.insert(0, "\xE2\x80\xAE"); // U+202E = UTF-8 0xE280AE |
| 110 return formatted_item; | 111 return UTF8ToUTF16(formatted_item); |
| 111 } | 112 } |
| 112 | 113 |
| 113 // Return the locale for the given index. E.g., may return pt-BR. | 114 // Return the locale for the given index. E.g., may return pt-BR. |
| 114 std::string LanguageList::GetLocaleFromIndex(int index) const { | 115 std::string LanguageList::GetLocaleFromIndex(int index) const { |
| 115 DCHECK(static_cast<int>(locale_names_.size()) > index); | 116 DCHECK(static_cast<int>(locale_names_.size()) > index); |
| 116 LocaleDataMap::const_iterator it = | 117 LocaleDataMap::const_iterator it = |
| 117 native_names_.find(locale_names_[index]); | 118 native_names_.find(locale_names_[index]); |
| 118 DCHECK(it != native_names_.end()); | 119 DCHECK(it != native_names_.end()); |
| 119 | 120 |
| 120 return it->second.locale_code; | 121 return it->second.locale_code; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 145 profile_(profile) { | 146 profile_(profile) { |
| 146 } | 147 } |
| 147 | 148 |
| 148 LanguageComboboxModel::~LanguageComboboxModel() {} | 149 LanguageComboboxModel::~LanguageComboboxModel() {} |
| 149 | 150 |
| 150 int LanguageComboboxModel::GetItemCount() { | 151 int LanguageComboboxModel::GetItemCount() { |
| 151 return get_languages_count(); | 152 return get_languages_count(); |
| 152 } | 153 } |
| 153 | 154 |
| 154 string16 LanguageComboboxModel::GetItemAt(int index) { | 155 string16 LanguageComboboxModel::GetItemAt(int index) { |
| 155 return WideToUTF16Hack(GetLanguageNameAt(index)); | 156 return GetLanguageNameAt(index); |
| 156 } | 157 } |
| 157 | 158 |
| 158 // Returns the index of the language currently specified in the user's | 159 // Returns the index of the language currently specified in the user's |
| 159 // preference file. Note that it's possible for language A to be picked | 160 // preference file. Note that it's possible for language A to be picked |
| 160 // while chrome is currently in language B if the user specified language B | 161 // while chrome is currently in language B if the user specified language B |
| 161 // via --lang. Since --lang is not a persistent setting, it seems that it | 162 // via --lang. Since --lang is not a persistent setting, it seems that it |
| 162 // shouldn't be reflected in this combo box. We return -1 if the value in | 163 // shouldn't be reflected in this combo box. We return -1 if the value in |
| 163 // the pref doesn't map to a know language (possible if the user edited the | 164 // the pref doesn't map to a know language (possible if the user edited the |
| 164 // prefs file manually). | 165 // prefs file manually). |
| 165 int LanguageComboboxModel::GetSelectedLanguageIndex(const std::string& prefs) { | 166 int LanguageComboboxModel::GetSelectedLanguageIndex(const std::string& prefs) { |
| 166 PrefService* local_state; | 167 PrefService* local_state; |
| 167 if (!profile_) | 168 if (!profile_) |
| 168 local_state = g_browser_process->local_state(); | 169 local_state = g_browser_process->local_state(); |
| 169 else | 170 else |
| 170 local_state = profile_->GetPrefs(); | 171 local_state = profile_->GetPrefs(); |
| 171 | 172 |
| 172 DCHECK(local_state); | 173 DCHECK(local_state); |
| 173 const std::string& current_locale = local_state->GetString(prefs.c_str()); | 174 const std::string& current_locale = local_state->GetString(prefs.c_str()); |
| 174 | 175 |
| 175 return GetIndexFromLocale(current_locale); | 176 return GetIndexFromLocale(current_locale); |
| 176 } | 177 } |
| OLD | NEW |