Index: chrome/browser/extensions/extension_l10n_util.cc |
=================================================================== |
--- chrome/browser/extensions/extension_l10n_util.cc (revision 24127) |
+++ chrome/browser/extensions/extension_l10n_util.cc (working copy) |
@@ -12,8 +12,10 @@ |
#include "base/file_util.h" |
#include "base/string_util.h" |
#include "base/values.h" |
+#include "chrome/browser/extensions/extension_message_handler.h" |
#include "chrome/common/extensions/extension.h" |
#include "chrome/common/extensions/extension_constants.h" |
+#include "chrome/common/json_value_serializer.h" |
namespace keys = extension_manifest_keys; |
@@ -87,4 +89,53 @@ |
return true; |
} |
+// Loads contents of the messages file for given locale. If file is not found, |
+// or there was parsing error we return NULL and set |error|. |
+// Caller owns the returned object. |
+static DictionaryValue* LoadMessageFile(const FilePath& locale_path, |
+ const std::string& locale, |
+ std::string* error) { |
+ std::string extension_locale = locale; |
+ // Normalize hyphens to underscores because that's what our locale files use. |
+ std::replace(extension_locale.begin(), extension_locale.end(), '-', '_'); |
+ FilePath file = locale_path.AppendASCII(extension_locale) |
+ .AppendASCII(Extension::kMessagesFilename); |
Aaron Boodman
2009/08/31 21:42:55
BTW: The message filenames should have the ".json"
|
+ JSONFileValueSerializer messages_serializer(file); |
+ Value *dictionary = messages_serializer.Deserialize(error); |
+ if (!dictionary && error->empty()) { |
+ // JSONFileValueSerializer just returns NULL if file cannot be found. It |
+ // doesn't set the error, so we have to do it. |
+ *error = StringPrintf("Catalog file is missing for locale %s.", |
+ extension_locale.c_str()); |
+ } |
+ |
+ return static_cast<DictionaryValue*>(dictionary); |
+} |
+ |
+bool LoadMessageCatalogs(const FilePath& locale_path, |
+ Extension* extension, |
+ std::string* error) { |
+ scoped_ptr<DictionaryValue> default_catalog( |
+ LoadMessageFile(locale_path, extension->default_locale(), error)); |
+ if (!default_catalog.get()) { |
+ return false; |
+ } |
+ |
+ scoped_ptr<DictionaryValue> app_catalog( |
+ LoadMessageFile(locale_path, extension->application_locale(), error)); |
+ if (!app_catalog.get()) { |
+ // Only default catalog has to be present. This is not an error. |
+ app_catalog.reset(new DictionaryValue); |
+ error->clear(); |
+ } |
+ |
+ ExtensionMessageHandler* message_handler = new ExtensionMessageHandler; |
+ if (!message_handler->Init(default_catalog.get(), app_catalog.get(), error)) { |
+ return false; |
+ } |
+ extension->set_message_handler(message_handler); |
+ |
+ return true; |
+} |
+ |
} // namespace extension_l10n_util |