diff options
author | torne@chromium.org <torne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-03 01:45:32 +0000 |
---|---|---|
committer | torne@chromium.org <torne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-03 01:45:32 +0000 |
commit | 756b6f4395615e32f6cf5ceb53d394cf393056e9 (patch) | |
tree | 45a978cd574310a6b76d1d3fc6836e4d4aef2f90 /ui | |
parent | 111839ad9e4d10a13b02d200b9f97e8be2deec93 (diff) | |
download | chromium_src-756b6f4395615e32f6cf5ceb53d394cf393056e9.zip chromium_src-756b6f4395615e32f6cf5ceb53d394cf393056e9.tar.gz chromium_src-756b6f4395615e32f6cf5ceb53d394cf393056e9.tar.bz2 |
Reuse ResourceBundle to detect locale availability.
IsLocaleAvailable reimplements the logic for finding the resource file
for a given locale and testing if it exists. Expose this in
ResourceBundle instead which contains the actual resource file loading
code and thus is the canonical source for how to create this path.
TEST=ResourceBundle.LocaleDataPakExists
Review URL: http://codereview.chromium.org/7831036
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99495 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/base/l10n/l10n_util.cc | 24 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.cc | 5 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.h | 3 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_unittest.cc | 6 |
4 files changed, 21 insertions, 17 deletions
diff --git a/ui/base/l10n/l10n_util.cc b/ui/base/l10n/l10n_util.cc index ee4bb7c..26faf95 100644 --- a/ui/base/l10n/l10n_util.cc +++ b/ui/base/l10n/l10n_util.cc @@ -36,9 +36,6 @@ namespace { -static const FilePath::CharType kLocaleFileExtension[] = - FILE_PATH_LITERAL(".pak"); - static const char* const kAcceptLanguageList[] = { "af", // Afrikaans "am", // Amharic @@ -228,8 +225,7 @@ bool IsLocalePartiallyPopulated(const std::string& locale_name) { } #if !defined(OS_MACOSX) -bool IsLocaleAvailable(const std::string& locale, - const FilePath& locale_path) { +bool IsLocaleAvailable(const std::string& locale) { // If locale has any illegal characters in it, we don't want to try to // load it because it may be pointing outside the locale data file directory. if (!file_util::IsFilenameLegal(ASCIIToUTF16(locale))) @@ -243,16 +239,12 @@ bool IsLocaleAvailable(const std::string& locale, if (!l10n_util::IsLocaleSupportedByOS(locale)) return false; - FilePath test_path = locale_path; - test_path = - test_path.AppendASCII(locale).ReplaceExtension(kLocaleFileExtension); - return file_util::PathExists(test_path); + return ResourceBundle::LocaleDataPakExists(locale); } bool CheckAndResolveLocale(const std::string& locale, - const FilePath& locale_path, std::string* resolved_locale) { - if (IsLocaleAvailable(locale, locale_path)) { + if (IsLocaleAvailable(locale)) { *resolved_locale = locale; return true; } @@ -287,7 +279,7 @@ bool CheckAndResolveLocale(const std::string& locale, tmp_locale.append("-CN"); } } - if (IsLocaleAvailable(tmp_locale, locale_path)) { + if (IsLocaleAvailable(tmp_locale)) { resolved_locale->swap(tmp_locale); return true; } @@ -308,7 +300,7 @@ bool CheckAndResolveLocale(const std::string& locale, for (size_t i = 0; i < ARRAYSIZE_UNSAFE(alias_map); ++i) { if (LowerCaseEqualsASCII(locale, alias_map[i].source)) { std::string tmp_locale(alias_map[i].dest); - if (IsLocaleAvailable(tmp_locale, locale_path)) { + if (IsLocaleAvailable(tmp_locale)) { resolved_locale->swap(tmp_locale); return true; } @@ -367,8 +359,6 @@ std::string GetApplicationLocale(const std::string& pref_locale) { #else - FilePath locale_path; - PathService::Get(ui::DIR_LOCALES, &locale_path); std::string resolved_locale; std::vector<std::string> candidates; @@ -421,7 +411,7 @@ std::string GetApplicationLocale(const std::string& pref_locale) { std::vector<std::string>::const_iterator i = candidates.begin(); for (; i != candidates.end(); ++i) { - if (CheckAndResolveLocale(*i, locale_path, &resolved_locale)) { + if (CheckAndResolveLocale(*i, &resolved_locale)) { base::i18n::SetICUDefaultLocale(resolved_locale); return resolved_locale; } @@ -429,7 +419,7 @@ std::string GetApplicationLocale(const std::string& pref_locale) { // Fallback on en-US. const std::string fallback_locale("en-US"); - if (IsLocaleAvailable(fallback_locale, locale_path)) { + if (IsLocaleAvailable(fallback_locale)) { base::i18n::SetICUDefaultLocale(fallback_locale); return fallback_locale; } diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc index 6a68f20..04cf392 100644 --- a/ui/base/resource/resource_bundle.cc +++ b/ui/base/resource/resource_bundle.cc @@ -119,6 +119,11 @@ ResourceBundle& ResourceBundle::GetSharedInstance() { return *g_shared_instance_; } +/* static */ +bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { + return !GetLocaleFilePath(locale).empty(); +} + #if !defined(OS_MACOSX) /* static */ FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h index 7076a6e..45edb38 100644 --- a/ui/base/resource/resource_bundle.h +++ b/ui/base/resource/resource_bundle.h @@ -107,6 +107,9 @@ class UI_EXPORT ResourceBundle { // Return the global resource loader instance. static ResourceBundle& GetSharedInstance(); + // Check if the .pak for the given locale exists. + static bool LocaleDataPakExists(const std::string& locale); + // Gets the bitmap with the specified resource_id from the current module // data. Returns a pointer to a shared instance of the SkBitmap. This shared // bitmap is owned by the resource bundle and should not be freed. diff --git a/ui/base/resource/resource_bundle_unittest.cc b/ui/base/resource/resource_bundle_unittest.cc index 9e4f12c..f778588 100644 --- a/ui/base/resource/resource_bundle_unittest.cc +++ b/ui/base/resource/resource_bundle_unittest.cc @@ -35,4 +35,10 @@ TEST(ResourceBundle, LoadDataResourceBytes) { EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); } +TEST(ResourceBundle, LocaleDataPakExists) { + // Check that ResourceBundle::LocaleDataPakExists returns the correct results. + EXPECT_TRUE(ResourceBundle::LocaleDataPakExists("en-US")); + EXPECT_FALSE(ResourceBundle::LocaleDataPakExists("not_a_real_locale")); +} + } // namespace ui |