OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_l10n_util.h" | 5 #include "chrome/browser/extensions/extension_l10n_util.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/extensions/extension_message_handler.h" | |
15 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
16 #include "chrome/common/extensions/extension_constants.h" | 17 #include "chrome/common/extensions/extension_constants.h" |
18 #include "chrome/common/json_value_serializer.h" | |
17 | 19 |
18 namespace keys = extension_manifest_keys; | 20 namespace keys = extension_manifest_keys; |
19 | 21 |
20 namespace extension_l10n_util { | 22 namespace extension_l10n_util { |
21 | 23 |
22 bool ValidateDefaultLocale(const Extension* extension) { | 24 bool ValidateDefaultLocale(const Extension* extension) { |
23 std::string default_locale = extension->default_locale(); | 25 std::string default_locale = extension->default_locale(); |
24 | 26 |
25 if (extension->supported_locales().find(default_locale) != | 27 if (extension->supported_locales().find(default_locale) != |
26 extension->supported_locales().end()) { | 28 extension->supported_locales().end()) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 } | 82 } |
81 | 83 |
82 if (extension->supported_locales().empty()) { | 84 if (extension->supported_locales().empty()) { |
83 *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed; | 85 *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed; |
84 return false; | 86 return false; |
85 } | 87 } |
86 | 88 |
87 return true; | 89 return true; |
88 } | 90 } |
89 | 91 |
92 // Loads contents of the messages file for given locale. If file is not found, | |
93 // or there was parsing error we return NULL and set |error|. | |
94 // Caller owns the returned object. | |
95 static DictionaryValue* LoadMessageFile(const FilePath& locale_path, | |
96 const std::string& locale, | |
97 std::string* error) { | |
98 std::string extension_locale = locale; | |
99 // Normalize hyphens to underscores because that's what our locale files use. | |
100 std::replace(extension_locale.begin(), extension_locale.end(), '-', '_'); | |
101 FilePath file = locale_path.AppendASCII(extension_locale) | |
102 .AppendASCII(Extension::kMessagesFilename); | |
Aaron Boodman
2009/08/31 21:42:55
BTW: The message filenames should have the ".json"
| |
103 JSONFileValueSerializer messages_serializer(file); | |
104 Value *dictionary = messages_serializer.Deserialize(error); | |
105 if (!dictionary && error->empty()) { | |
106 // JSONFileValueSerializer just returns NULL if file cannot be found. It | |
107 // doesn't set the error, so we have to do it. | |
108 *error = StringPrintf("Catalog file is missing for locale %s.", | |
109 extension_locale.c_str()); | |
110 } | |
111 | |
112 return static_cast<DictionaryValue*>(dictionary); | |
113 } | |
114 | |
115 bool LoadMessageCatalogs(const FilePath& locale_path, | |
116 Extension* extension, | |
117 std::string* error) { | |
118 scoped_ptr<DictionaryValue> default_catalog( | |
119 LoadMessageFile(locale_path, extension->default_locale(), error)); | |
120 if (!default_catalog.get()) { | |
121 return false; | |
122 } | |
123 | |
124 scoped_ptr<DictionaryValue> app_catalog( | |
125 LoadMessageFile(locale_path, extension->application_locale(), error)); | |
126 if (!app_catalog.get()) { | |
127 // Only default catalog has to be present. This is not an error. | |
128 app_catalog.reset(new DictionaryValue); | |
129 error->clear(); | |
130 } | |
131 | |
132 ExtensionMessageHandler* message_handler = new ExtensionMessageHandler; | |
133 if (!message_handler->Init(default_catalog.get(), app_catalog.get(), error)) { | |
134 return false; | |
135 } | |
136 extension->set_message_handler(message_handler); | |
137 | |
138 return true; | |
139 } | |
140 | |
90 } // namespace extension_l10n_util | 141 } // namespace extension_l10n_util |
OLD | NEW |