From 9986ee16343028edb452fe235ac6a8fca5ee12f5 Mon Sep 17 00:00:00 2001 From: "thomasvl@chromium.org" Date: Thu, 19 Nov 2009 16:25:04 +0000 Subject: Mac language/locale cleanup - Effectively revert revision 28193 (http://codereview.chromium.org/258037), this makes Mac match the other platform for what at it's core is used for the chrome concept of locale. - For the ApplicationLanguage, the browser will end up with what Cocoa picks (same as before) - All other process types will honor the language they got on the command line when starting up. - When asked the apps language, have the same side effect as Windows and Linux has of pushing the language through to ICU also (so dates format right, etc.) - During browser startup, if someone passed a language, bail because Mac can't support that. TEST=The tips on the NTP and the dates on the history page are in the same language at the UI. BUG=26856 BUG=22727 Review URL: http://codereview.chromium.org/399086 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32515 0039d316-1c4b-4281-b951-d872f2087c98 --- app/l10n_util.cc | 39 +++++++++++++++++++++++++++++++------- app/l10n_util_mac.h | 4 ++++ app/l10n_util_mac.mm | 53 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 84 insertions(+), 12 deletions(-) (limited to 'app') diff --git a/app/l10n_util.cc b/app/l10n_util.cc index d35f68d..f44ce37 100644 --- a/app/l10n_util.cc +++ b/app/l10n_util.cc @@ -25,6 +25,10 @@ #include #endif +#if defined(OS_MACOSX) +#include "app/l10n_util_mac.h" +#endif + // TODO(playmobil): remove this undef once SkPostConfig.h is fixed. // skia/include/corecg/SkPostConfig.h #defines strcasecmp() so we can't use // base::strcasecmp() without #undefing it here. @@ -422,19 +426,16 @@ namespace l10n_util { // Represents the locale-specific text direction. static TextDirection g_text_direction = UNKNOWN_DIRECTION; -// On the Mac, we don't want to test preferences or ICU for the language, -// we want to use whatever Cocoa is using when it loaded the main nib file. -// It handles all the mapping and fallbacks for us, we just need to ask. -// See l10n_util_mac for that implementation. -#if !defined(OS_MACOSX) std::string GetApplicationLocale(const std::wstring& pref_locale) { +#if !defined(OS_MACOSX) + FilePath locale_path; PathService::Get(app::DIR_LOCALES, &locale_path); std::string resolved_locale; std::vector candidates; const std::string system_locale = GetSystemLocale(); - // We only use --lang and the app pref on Windows. On Linux/Mac, we only + // We only use --lang and the app pref on Windows. On Linux, we only // look at the LC_*/LANG environment variables. We do, however, pass --lang // to renderer and plugin processes so they know what language the parent // process decided to use. @@ -482,8 +483,32 @@ std::string GetApplicationLocale(const std::wstring& pref_locale) { NOTREACHED(); return std::string(); -} + +#else // !defined(OS_MACOSX) + + // Use any override (Cocoa for the browser), otherwise use the command line + // argument. + std::string app_locale = l10n_util::GetLocaleOverride(); + if (app_locale.empty()) { + const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); + app_locale = parsed_command_line.GetSwitchValueASCII(switches::kLang); + } + + // The above should handle all of the cases Chrome normally hits, but for some + // unit tests, we need something to fall back too. + if (app_locale.empty()) + app_locale = "en-US"; + + // Windows/Linux call CheckAndResolveLocale which calls IsLocaleAvailable + // which calls SetICUDefaultLocale to let ICU use the same locale. Mac + // doesn't use a locale directory tree of resources (it uses Mac style + // resources), so mirror that ICU behavior by calling SetICUDefaultLocale + // directly. + UBool icu_set = SetICUDefaultLocale(app_locale); + DCHECK(icu_set); + return app_locale; #endif // !defined(OS_MACOSX) +} string16 GetDisplayNameForLocale(const std::string& locale_code, const std::string& display_locale, diff --git a/app/l10n_util_mac.h b/app/l10n_util_mac.h index 87ccb05..995d191 100644 --- a/app/l10n_util_mac.h +++ b/app/l10n_util_mac.h @@ -63,6 +63,10 @@ NSString* GetNSStringFWithFixup(int message_id, const string16& c, const string16& d); +// Support the override of the locale with the value from Cocoa. +void OverrideLocaleWithCocoaLocale(); +const std::string& GetLocaleOverride(); + } // namespace l10n_util #endif // APP_L10N_UTIL_MAC_H_ diff --git a/app/l10n_util_mac.mm b/app/l10n_util_mac.mm index fe09826..9861f14 100644 --- a/app/l10n_util_mac.mm +++ b/app/l10n_util_mac.mm @@ -5,14 +5,57 @@ #import #include "app/l10n_util_mac.h" #include "base/sys_string_conversions.h" +#include "base/lazy_instance.h" + +namespace { + +class OverrideLocaleHolder { + public: + OverrideLocaleHolder() {} + const std::string& value() const { return value_; } + void set_value(const std::string override_value) { value_ = override_value; } + private: + DISALLOW_COPY_AND_ASSIGN(OverrideLocaleHolder); + std::string value_; +}; + +base::LazyInstance + override_locale_holder(base::LINKER_INITIALIZED); + +} // namespace namespace l10n_util { -std::string GetApplicationLocale(const std::wstring& pref_locale) { - // NOTE: The Win/Linux version of this calls out to CheckAndResolveLocale - // to do some remapping. Since Mac is using real locales that Cocoa has - // to be able to load, that shouldn't be needed. - return [[[NSLocale currentLocale] localeIdentifier] UTF8String]; +const std::string& GetLocaleOverride() { + return override_locale_holder.Get().value(); +} + +void OverrideLocaleWithCocoaLocale() { + // NSBundle really should only be called on the main thread. + DCHECK([NSThread isMainThread]); + + // Chrome really only has one concept of locale, but Mac OS X has locale and + // language that can be set independently. After talking with Chrome UX folks + // (Cole), the best path from an experience point of view is to map the Mac OS + // X language into the Chrome locale. This way strings like "Yesterday" and + // "Today" are in the same language as raw dates like "March 20, 1999" (Chrome + // strings resources vs ICU generated strings). This also makes the Mac acts + // like other Chrome platforms. + NSArray* languageList = [[NSBundle mainBundle] preferredLocalizations]; + NSString* firstLocale = [languageList objectAtIndex:0]; + // Mac OS X uses "_" instead of "-", so swap to get a real locale value. + std::string locale_value = + [[firstLocale stringByReplacingOccurrencesOfString:@"_" + withString:@"-"] UTF8String]; + + // On disk the "en-US" resources are just "en" (http://crbug.com/25578), so + // the reverse mapping is done here to continue to feed Chrome the same values + // in all cases on all platforms. (l10n_util maps en to en-US if it gets + // passed this on the command line) + if (locale_value == "en") + locale_value = "en-US"; + + override_locale_holder.Get().set_value(locale_value); } // Remove the Windows-style accelerator marker and change "..." into an -- cgit v1.1