Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(778)

Unified Diff: ui/base/l10n/l10n_util.cc

Issue 834183005: Move two locale functions from rtl.h into l10n_util.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/base/l10n/l10n_util.cc
diff --git a/ui/base/l10n/l10n_util.cc b/ui/base/l10n/l10n_util.cc
index 7515f3f6071663a878863e231022303f0f210e35..140ddc42a1043df63f3feaef06e913d3425a64a1 100644
--- a/ui/base/l10n/l10n_util.cc
+++ b/ui/base/l10n/l10n_util.cc
@@ -303,6 +303,30 @@ struct AvailableLocalesTraits
}
};
+// Extract language, country and variant, but ignore keywords. For example,
+// en-US, ca@valencia, ca-ES@valencia.
+std::string GetLocaleString(const icu::Locale& locale) {
+ const char* language = locale.getLanguage();
+ const char* country = locale.getCountry();
+ const char* variant = locale.getVariant();
+
+ std::string result =
+ (language != NULL && *language != '\0') ? language : "und";
+
+ if (country != NULL && *country != '\0') {
+ result += '-';
+ result += country;
+ }
+
+ if (variant != NULL && *variant != '\0') {
+ std::string variant_str(variant);
+ base::StringToLowerASCII(&variant_str);
+ result += '@' + variant_str;
+ }
+
+ return result;
+}
+
base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits>
g_available_locales = LAZY_INSTANCE_INITIALIZER;
@@ -310,8 +334,18 @@ base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits>
namespace l10n_util {
+// Convert the ICU default locale to a string.
+std::string GetConfiguredLocale() {
+ return GetLocaleString(icu::Locale::getDefault());
+}
+
+// Convert the ICU canonicalized locale to a string.
+std::string GetCanonicalLocale(const char* locale) {
+ return GetLocaleString(icu::Locale::createCanonical(locale));
+}
+
std::string GetCanonicalLocale(const std::string& locale) {
- return base::i18n::GetCanonicalLocale(locale.c_str());
+ return GetCanonicalLocale(locale.c_str());
}
std::string GetLanguage(const std::string& locale) {
@@ -444,7 +478,7 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
std::back_inserter(candidates), &GetCanonicalLocale);
} else {
// If no override was set, defer to ICU
- candidates.push_back(base::i18n::GetConfiguredLocale());
+ candidates.push_back(GetConfiguredLocale());
}
#elif defined(OS_ANDROID)
@@ -464,7 +498,7 @@ std::string GetApplicationLocaleInternal(const std::string& pref_locale) {
DCHECK(*languages); // At least one entry, "C", is guaranteed.
for (; *languages != NULL; ++languages) {
- candidates.push_back(base::i18n::GetCanonicalLocale(*languages));
+ candidates.push_back(GetCanonicalLocale(*languages));
}
#else

Powered by Google App Engine
This is Rietveld 408576698