diff options
author | cira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-21 19:08:23 +0000 |
---|---|---|
committer | cira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-21 19:08:23 +0000 |
commit | 863d52e6470909dce3856ce207d1e5f6229524cc (patch) | |
tree | 5ccac33fa1169e56058a2c32a8af04d8228c6620 /chrome/common/extensions/extension_message_bundle.cc | |
parent | da4d0186a344a4584b909acde48d43fe7ccf40f7 (diff) | |
download | chromium_src-863d52e6470909dce3856ce207d1e5f6229524cc.zip chromium_src-863d52e6470909dce3856ce207d1e5f6229524cc.tar.gz chromium_src-863d52e6470909dce3856ce207d1e5f6229524cc.tar.bz2 |
Implementing better fallback algorithm.
Before:
current_locale->default_locale
Now:
current_locale->chain_of_parent_locales->default_locale
If default_locale is de, and current locale en_US, we follow:
en_US -> en -> de
en is not a Chrome locale (only en_US, en_GB are), but we fake it to allow this kind of fallback.
Developers can implement common locale root with most of the messages (like en) and put locale specifics in en_GB (color->colour) or en_US. You can even symlink en and en_US and save on work.
I am planning on fixing loading local resources too, to use this child->parent fallback.
BUG=12131
Review URL: http://codereview.chromium.org/293037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29684 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions/extension_message_bundle.cc')
-rw-r--r-- | chrome/common/extensions/extension_message_bundle.cc | 54 |
1 files changed, 19 insertions, 35 deletions
diff --git a/chrome/common/extensions/extension_message_bundle.cc b/chrome/common/extensions/extension_message_bundle.cc index 980dd08..9e3a7df 100644 --- a/chrome/common/extensions/extension_message_bundle.cc +++ b/chrome/common/extensions/extension_message_bundle.cc @@ -5,8 +5,10 @@ #include "chrome/common/extensions/extension_message_bundle.h" #include <string> +#include <vector> #include "base/hash_tables.h" +#include "base/linked_ptr.h" #include "base/scoped_ptr.h" #include "base/string_util.h" #include "base/values.h" @@ -34,52 +36,34 @@ static bool BadKeyMessage(const std::string& name, std::string* error) { // static ExtensionMessageBundle* ExtensionMessageBundle::Create( - const DictionaryValue& default_locale_catalog, - const DictionaryValue& current_locale_catalog, + const CatalogVector& locale_catalogs, std::string* error) { scoped_ptr<ExtensionMessageBundle> message_bundle( new ExtensionMessageBundle); - if (!message_bundle->Init(default_locale_catalog, - current_locale_catalog, - error)) + if (!message_bundle->Init(locale_catalogs, error)) return NULL; return message_bundle.release(); } -bool ExtensionMessageBundle::Init(const DictionaryValue& default_locale_catalog, - const DictionaryValue& current_locale_catalog, +bool ExtensionMessageBundle::Init(const CatalogVector& locale_catalogs, std::string* error) { dictionary_.clear(); - // Create a single dictionary out of default and current_locale catalogs. - // If message is missing from current_locale catalog, we take one from default - // catalog. - DictionaryValue::key_iterator key_it = current_locale_catalog.begin_keys(); - for (; key_it != current_locale_catalog.end_keys(); ++key_it) { - std::string key(StringToLowerASCII(WideToUTF8(*key_it))); - if (!IsValidName(*key_it)) - return BadKeyMessage(key, error); - std::string value; - if (!GetMessageValue(*key_it, current_locale_catalog, &value, error)) - return false; - // Keys are not case-sensitive. - dictionary_[key] = value; - } - - key_it = default_locale_catalog.begin_keys(); - for (; key_it != default_locale_catalog.end_keys(); ++key_it) { - std::string key(StringToLowerASCII(WideToUTF8(*key_it))); - if (!IsValidName(*key_it)) - return BadKeyMessage(key, error); - // Add only messages that are not provided by app_catalog. - if (dictionary_.find(key) != dictionary_.end()) - continue; - std::string value; - if (!GetMessageValue(*key_it, default_locale_catalog, &value, error)) - return false; - // Keys are not case-sensitive. - dictionary_[key] = value; + CatalogVector::const_reverse_iterator it = locale_catalogs.rbegin(); + for (; it != locale_catalogs.rend(); ++it) { + DictionaryValue* catalog = (*it).get(); + DictionaryValue::key_iterator key_it = catalog->begin_keys(); + for (; key_it != catalog->end_keys(); ++key_it) { + std::string key(StringToLowerASCII(WideToUTF8(*key_it))); + if (!IsValidName(*key_it)) + return BadKeyMessage(key, error); + std::string value; + if (!GetMessageValue(*key_it, *catalog, &value, error)) + return false; + // Keys are not case-sensitive. + dictionary_[key] = value; + } } return true; |