diff options
author | cira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-22 20:56:18 +0000 |
---|---|---|
committer | cira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-22 20:56:18 +0000 |
commit | af958dd5f74817150f58d762a1b99f05e5d355a6 (patch) | |
tree | 2d7b59b8a06d446e2caa9cfadc8ff69f1d78faa6 /chrome/browser/extensions | |
parent | 14e66f1c33488f4683337b5424170d2a27c98c07 (diff) | |
download | chromium_src-af958dd5f74817150f58d762a1b99f05e5d355a6.zip chromium_src-af958dd5f74817150f58d762a1b99f05e5d355a6.tar.gz chromium_src-af958dd5f74817150f58d762a1b99f05e5d355a6.tar.bz2 |
Loading local resources uses improved fallback algorithm.
Before:
_locales/current_locale/rel_path, then extension root/rel_path
Now:
_locales/{current_locale, parents}/rel_path, then extension root/rel_path
This change syncs local resource loading with catalog loading algo.
BUG=12131
Review URL: http://codereview.chromium.org/316013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29815 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
-rw-r--r-- | chrome/browser/extensions/extension_l10n_util.cc | 32 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_l10n_util.h | 17 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_l10n_util_unittest.cc | 42 |
3 files changed, 61 insertions, 30 deletions
diff --git a/chrome/browser/extensions/extension_l10n_util.cc b/chrome/browser/extensions/extension_l10n_util.cc index 4c8b116..1ac93ff 100644 --- a/chrome/browser/extensions/extension_l10n_util.cc +++ b/chrome/browser/extensions/extension_l10n_util.cc @@ -62,19 +62,15 @@ bool AddLocale(const std::set<std::string>& chrome_locales, return true; } -// Converts all - into _, to be consistent with ICU and file system names. -static std::string NormalizeLocale(const std::string& locale) { +std::string NormalizeLocale(const std::string& locale) { std::string normalized_locale(locale); std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); return normalized_locale; } -// Produce a vector of parent locales for given locale. -// It includes the current locale in the result. -// sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. -static void GetParentLocales(const std::string& current_locale, - std::vector<std::string>* parent_locales) { +void GetParentLocales(const std::string& current_locale, + std::vector<std::string>* parent_locales) { std::string locale(NormalizeLocale(current_locale)); const int kNameCapacity = 256; @@ -187,16 +183,22 @@ ExtensionMessageBundle* LoadMessageCatalogs( return ExtensionMessageBundle::Create(catalogs, error); } -FilePath GetL10nRelativePath(const FilePath& relative_resource_path) { - // Get locale relative path. - static std::string current_locale = l10n_util::GetApplicationLocale(L""); - std::replace(current_locale.begin(), current_locale.end(), '-', '_'); +void GetL10nRelativePaths(const FilePath& relative_resource_path, + std::vector<FilePath>* l10n_paths) { + DCHECK(NULL != l10n_paths); + + std::vector<std::string> locales; + static const std::string current_locale = + l10n_util::GetApplicationLocale(L""); + GetParentLocales(current_locale, &locales); FilePath locale_relative_path; - return locale_relative_path - .AppendASCII(Extension::kLocaleFolder) - .AppendASCII(current_locale) - .Append(relative_resource_path); + for (size_t i = 0; i < locales.size(); ++i) { + l10n_paths->push_back(locale_relative_path + .AppendASCII(Extension::kLocaleFolder) + .AppendASCII(locales[i]) + .Append(relative_resource_path)); + } } } // namespace extension_l10n_util diff --git a/chrome/browser/extensions/extension_l10n_util.h b/chrome/browser/extensions/extension_l10n_util.h index 57c2033..07be73e 100644 --- a/chrome/browser/extensions/extension_l10n_util.h +++ b/chrome/browser/extensions/extension_l10n_util.h @@ -9,6 +9,7 @@ #include <set> #include <string> +#include <vector> class DictionaryValue; class Extension; @@ -33,6 +34,15 @@ bool AddLocale(const std::set<std::string>& chrome_locales, std::set<std::string>* valid_locales, std::string* error); +// Converts all - into _, to be consistent with ICU and file system names. +std::string NormalizeLocale(const std::string& locale); + +// Produce a vector of parent locales for given locale. +// It includes the current locale in the result. +// sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. +void GetParentLocales(const std::string& current_locale, + std::vector<std::string>* parent_locales); + // Adds valid locales to the extension. // 1. Do nothing if _locales directory is missing (not an error). // 2. Get list of Chrome locales. @@ -56,8 +66,11 @@ ExtensionMessageBundle* LoadMessageCatalogs( const std::set<std::string>& valid_locales, std::string* error); -// Returns relative l10n path to the resource. -FilePath GetL10nRelativePath(const FilePath& relative_resource_path); +// Returns relative l10n paths to the resource. +// Returned vector starts with more specific locale path, and ends with the +// least specific one. +void GetL10nRelativePaths(const FilePath& relative_resource_path, + std::vector<FilePath>* l10n_paths); } // namespace extension_l10n_util diff --git a/chrome/browser/extensions/extension_l10n_util_unittest.cc b/chrome/browser/extensions/extension_l10n_util_unittest.cc index acf946c..7e7ce27 100644 --- a/chrome/browser/extensions/extension_l10n_util_unittest.cc +++ b/chrome/browser/extensions/extension_l10n_util_unittest.cc @@ -183,6 +183,17 @@ TEST(ExtensionL10nUtil, LoadMessageCatalogsDuplicateKeys) { EXPECT_TRUE(error.empty()); } +TEST(ExtensionL10nUtil, GetParentLocales) { + std::vector<std::string> locales; + const std::string top_locale("sr_Cyrl_RS"); + extension_l10n_util::GetParentLocales(top_locale, &locales); + + ASSERT_EQ(3U, locales.size()); + EXPECT_EQ("sr_Cyrl_RS", locales[0]); + EXPECT_EQ("sr_Cyrl", locales[1]); + EXPECT_EQ("sr", locales[2]); +} + bool PathsAreEqual(const FilePath& path1, const FilePath& path2) { FilePath::StringType path1_str = path1.value(); std::replace(path1_str.begin(), path1_str.end(), '\\', '/'); @@ -198,19 +209,24 @@ bool PathsAreEqual(const FilePath& path1, const FilePath& path2) { } TEST(ExtensionL10nUtil, GetL10nRelativePath) { - static std::string current_locale = l10n_util::GetApplicationLocale(L""); - std::replace(current_locale.begin(), current_locale.end(), '-', '_'); - - FilePath locale_path; - locale_path = locale_path - .AppendASCII(Extension::kLocaleFolder) - .AppendASCII(current_locale) - .AppendASCII("foo") - .AppendASCII("bar.js"); - - FilePath result = extension_l10n_util::GetL10nRelativePath( - FilePath(FILE_PATH_LITERAL("foo/bar.js"))); - EXPECT_TRUE(PathsAreEqual(locale_path, result)); + static std::string current_locale = + extension_l10n_util::NormalizeLocale(l10n_util::GetApplicationLocale(L"")); + + std::vector<FilePath> l10n_paths; + extension_l10n_util::GetL10nRelativePaths( + FilePath(FILE_PATH_LITERAL("foo/bar.js")), &l10n_paths); + ASSERT_FALSE(l10n_paths.empty()); + + std::vector<std::string> locales; + extension_l10n_util::GetParentLocales(current_locale, &locales); + ASSERT_EQ(locales.size(), l10n_paths.size()); + + for (size_t i = 0; i < locales.size(); ++i) { + FilePath tmp; + tmp = tmp.AppendASCII(Extension::kLocaleFolder) + .AppendASCII(locales[i]).AppendASCII("foo/bar.js"); + EXPECT_TRUE(PathsAreEqual(tmp, l10n_paths[i])); + } } } // namespace |