diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/l10n_util.cc | 20 | ||||
-rw-r--r-- | app/l10n_util_win.cc | 41 | ||||
-rw-r--r-- | app/l10n_util_win.h | 11 |
3 files changed, 67 insertions, 5 deletions
diff --git a/app/l10n_util.cc b/app/l10n_util.cc index 375f3b6..d86f082 100644 --- a/app/l10n_util.cc +++ b/app/l10n_util.cc @@ -8,7 +8,9 @@ #include <glib/gutils.h> #endif +#include <algorithm> #include <cstdlib> +#include <iterator> #include "app/app_paths.h" #include "app/l10n_util_collator.h" @@ -30,6 +32,8 @@ #if defined(OS_MACOSX) #include "app/l10n_util_mac.h" +#elif defined(OS_WIN) +#include "app/l10n_util_win.h" #endif namespace { @@ -325,6 +329,10 @@ void AdjustParagraphDirectionality(string16* paragraph) { #endif } +std::string GetCanonicalLocale(const std::string& locale) { + return base::i18n::GetCanonicalLocale(locale.c_str()); +} + } // namespace namespace l10n_util { @@ -369,8 +377,16 @@ std::string GetApplicationLocale(const std::string& pref_locale) { if (!pref_locale.empty()) candidates.push_back(pref_locale); - // Next, try the system locale. - candidates.push_back(base::i18n::GetConfiguredLocale()); + // Next, try the overridden locale. + const std::vector<std::string>& languages = l10n_util::GetLocaleOverrides(); + if (!languages.empty()) { + candidates.reserve(candidates.size() + languages.size()); + std::transform(languages.begin(), languages.end(), + std::back_inserter(candidates), &GetCanonicalLocale); + } else { + // If no override was set, defer to ICU + candidates.push_back(base::i18n::GetConfiguredLocale()); + } #elif defined(OS_CHROMEOS) diff --git a/app/l10n_util_win.cc b/app/l10n_util_win.cc index 2f1f627..eacdb35 100644 --- a/app/l10n_util_win.cc +++ b/app/l10n_util_win.cc @@ -2,14 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "app/l10n_util.h" +#include "app/l10n_util_win.h" -#include <algorithm> #include <windowsx.h> +#include <algorithm> +#include <iterator> -#include "app/l10n_util_win.h" +#include "app/l10n_util.h" #include "base/i18n/rtl.h" +#include "base/lazy_instance.h" #include "base/string_number_conversions.h" +#include "base/win/i18n.h" #include "base/win/windows_version.h" #include "grit/app_locale_settings.h" @@ -52,6 +55,21 @@ bool IsFontPresent(const wchar_t* font_name) { return wcscmp(font_name, actual_font_name) == 0; } +class OverrideLocaleHolder { + public: + OverrideLocaleHolder() {} + const std::vector<std::string>& value() const { return value_; } + void swap_value(std::vector<std::string>* override_value) { + value_.swap(*override_value); + } + private: + std::vector<std::string> value_; + DISALLOW_COPY_AND_ASSIGN(OverrideLocaleHolder); +}; + +base::LazyInstance<OverrideLocaleHolder> + override_locale_holder(base::LINKER_INITIALIZED); + } // namespace namespace l10n_util { @@ -147,4 +165,21 @@ void AdjustUIFontForWindow(HWND hwnd) { } } +void OverrideLocaleWithUILanguageList() { + std::vector<std::wstring> ui_languages; + if (base::win::i18n::GetThreadPreferredUILanguageList(&ui_languages)) { + std::vector<std::string> ascii_languages; + ascii_languages.reserve(ui_languages.size()); + std::transform(ui_languages.begin(), ui_languages.end(), + std::back_inserter(ascii_languages), &WideToASCII); + override_locale_holder.Get().swap_value(&ascii_languages); + } else { + NOTREACHED() << "Failed to determine the UI language for locale override."; + } +} + +const std::vector<std::string>& GetLocaleOverrides() { + return override_locale_holder.Get().value(); +} + } // namespace l10n_util diff --git a/app/l10n_util_win.h b/app/l10n_util_win.h index 99a9df3..7a1dda5 100644 --- a/app/l10n_util_win.h +++ b/app/l10n_util_win.h @@ -7,6 +7,8 @@ #pragma once #include <windows.h> +#include <string> +#include <vector> namespace l10n_util { @@ -47,6 +49,15 @@ void AdjustUIFont(LOGFONT* logfont); // stored in the per-locale resource. void AdjustUIFontForWindow(HWND hwnd); +// Allow processes to override the configured locale with the user's Windows UI +// languages. This function should generally be called once early in +// Application startup. +void OverrideLocaleWithUILanguageList(); + +// Retrieve the locale override, or an empty vector if the locale has not been +// or failed to be overridden. +const std::vector<std::string>& GetLocaleOverrides(); + } // namespace l10n_util #endif // APP_L10N_UTIL_WIN_H_ |