summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorjshin@chromium.org <jshin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-30 00:18:22 +0000
committerjshin@chromium.org <jshin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-30 00:18:22 +0000
commitc16605de2a5f9fc7c16cb6c185e565e503667165 (patch)
tree34045b1a4fc73a7fa8329c49d45bd158b8d78fcd /base
parent405acd1cd89d050b17c965ffa8dba6b8c93966cb (diff)
downloadchromium_src-c16605de2a5f9fc7c16cb6c185e565e503667165.zip
chromium_src-c16605de2a5f9fc7c16cb6c185e565e503667165.tar.gz
chromium_src-c16605de2a5f9fc7c16cb6c185e565e503667165.tar.bz2
Use GLib to parse environment variables to determine the default locale.
Switch to using GLib, instead of having a Chromium specific implementation, for parsing environment variables for language preferences for all POSIX systems that use GTK+. If we have a non-GTK port in the future, we have to resurrect our custom code. This will guarantee that UI language selection in Chromium works just like for all other GTK+ applications, and simplify the code. While doing this, also do some cleaning-up of the language preferences code and add a test case to verify that the prioritization is done correctly. Original CL by Fredrik Roubert at http://codereview.chromium.org/2901004/show BUG=NONE TEST=app_unittest with "--gtest_filter=L10nUt*.*Loc*" passes. Review URL: http://codereview.chromium.org/4217004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64524 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/i18n/rtl.cc49
-rw-r--r--base/i18n/rtl.h14
2 files changed, 53 insertions, 10 deletions
diff --git a/base/i18n/rtl.cc b/base/i18n/rtl.cc
index f5381a2..9ff12d8 100644
--- a/base/i18n/rtl.cc
+++ b/base/i18n/rtl.cc
@@ -18,25 +18,57 @@
#include <gtk/gtk.h>
#endif
+namespace {
+
+// Extract language and country, ignore keywords, concatenate using dash.
+std::string GetLocaleString(const icu::Locale& locale) {
+ const char* language = locale.getLanguage();
+ const char* country = locale.getCountry();
+
+ std::string result =
+ (language != NULL && *language != '\0') ? language : "und";
+
+ if (country != NULL && *country != '\0') {
+ result += '-';
+ result += country;
+ }
+
+ return result;
+}
+
+} // namespace
+
namespace base {
namespace i18n {
// Represents the locale-specific ICU text direction.
static TextDirection g_icu_text_direction = UNKNOWN_DIRECTION;
+#if defined(OS_WIN)
void GetLanguageAndRegionFromOS(std::string* lang, std::string* region) {
// Later we may have to change this to be OS-dependent so that
// it's not affected by ICU's default locale. It's all right
// to do this way because SetICUDefaultLocale is internal
// to this file and we know that it's not yet called when this function
// is called.
- icu::Locale locale = icu::Locale::getDefault();
+ const icu::Locale& locale = icu::Locale::getDefault();
const char* language = locale.getLanguage();
const char* country = locale.getCountry();
DCHECK(language);
*lang = language;
*region = country;
}
+#endif
+
+// 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));
+}
// Convert Chrome locale name to ICU locale name
std::string ICULocaleName(const std::string& locale_string) {
@@ -50,13 +82,14 @@ std::string ICULocaleName(const std::string& locale_string) {
// locale. If it's es-RR other than es-ES, map to es-RR. Otherwise, map
// to es-MX (the most populous in Spanish-speaking Latin America).
if (LowerCaseEqualsASCII(locale_string, "es-419")) {
- std::string lang, region;
- GetLanguageAndRegionFromOS(&lang, &region);
- if (LowerCaseEqualsASCII(lang, "es") &&
- !LowerCaseEqualsASCII(region, "es")) {
- lang.append("-");
- lang.append(region);
- return lang;
+ const icu::Locale& locale = icu::Locale::getDefault();
+ std::string language = locale.getLanguage();
+ const char* country = locale.getCountry();
+ if (LowerCaseEqualsASCII(language, "es") &&
+ !LowerCaseEqualsASCII(country, "es")) {
+ language += '-';
+ language += country;
+ return language;
}
return "es-MX";
}
diff --git a/base/i18n/rtl.h b/base/i18n/rtl.h
index 2fe932c..52b1a2b 100644
--- a/base/i18n/rtl.h
+++ b/base/i18n/rtl.h
@@ -29,8 +29,18 @@ enum TextDirection {
LEFT_TO_RIGHT,
};
-// Get language and region from the OS.
+#if defined(OS_WIN)
+// Get language and region from the OS. Used by Chrome Frame.
void GetLanguageAndRegionFromOS(std::string* lang, std::string* region);
+#endif
+
+// Get the locale that the currently running process has been configured to use.
+// The return value is of the form language[-country] (e.g., en-US) where the
+// language is the 2 or 3 letter code from ISO-639.
+std::string GetConfiguredLocale();
+
+// Canonicalize a string (eg. a POSIX locale string) to a Chrome locale name.
+std::string GetCanonicalLocale(const char* locale);
// Sets the default locale of ICU.
// Once the application locale of Chrome in GetApplicationLocale is determined,
@@ -136,7 +146,7 @@ void WrapPathWithLTRFormatting(const FilePath& path,
// string is wrapped with LRE (Left-To-Right Embedding) and PDF (Pop
// Directional Formatting) marks and returned. In LTR locale, the string itself
// is returned.
-string16 GetDisplayStringInLTRDirectionality(const string16& text)
+string16 GetDisplayStringInLTRDirectionality(const string16& text)
WARN_UNUSED_RESULT;
// Strip the beginning (U+202A..U+202B, U+202D..U+202E) and/or ending (U+202C)