| 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/common/extensions/extension_message_bundle.h" | 5 #include "chrome/common/extensions/extension_message_bundle.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 if (!message_bundle->Init(locale_catalogs, error)) | 43 if (!message_bundle->Init(locale_catalogs, error)) |
| 44 return NULL; | 44 return NULL; |
| 45 | 45 |
| 46 return message_bundle.release(); | 46 return message_bundle.release(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 bool ExtensionMessageBundle::Init(const CatalogVector& locale_catalogs, | 49 bool ExtensionMessageBundle::Init(const CatalogVector& locale_catalogs, |
| 50 std::string* error) { | 50 std::string* error) { |
| 51 dictionary_.clear(); | 51 dictionary_.clear(); |
| 52 | 52 |
| 53 CatalogVector::const_reverse_iterator it = locale_catalogs.rbegin(); | 53 for (CatalogVector::const_reverse_iterator it = locale_catalogs.rbegin(); |
| 54 for (; it != locale_catalogs.rend(); ++it) { | 54 it != locale_catalogs.rend(); ++it) { |
| 55 DictionaryValue* catalog = (*it).get(); | 55 DictionaryValue* catalog = (*it).get(); |
| 56 DictionaryValue::key_iterator key_it = catalog->begin_keys(); | 56 for (DictionaryValue::key_iterator key_it = catalog->begin_keys(); |
| 57 for (; key_it != catalog->end_keys(); ++key_it) { | 57 key_it != catalog->end_keys(); ++key_it) { |
| 58 std::string key(StringToLowerASCII(WideToUTF8(*key_it))); | 58 std::string key(StringToLowerASCII(WideToUTF8(*key_it))); |
| 59 if (!IsValidName(*key_it)) | 59 if (!IsValidName(*key_it)) |
| 60 return BadKeyMessage(key, error); | 60 return BadKeyMessage(key, error); |
| 61 std::string value; | 61 std::string value; |
| 62 if (!GetMessageValue(*key_it, *catalog, &value, error)) | 62 if (!GetMessageValue(*key_it, *catalog, &value, error)) |
| 63 return false; | 63 return false; |
| 64 // Keys are not case-sensitive. | 64 // Keys are not case-sensitive. |
| 65 dictionary_[key] = value; | 65 dictionary_[key] = value; |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 return true; | 69 return true; |
| 70 } | 70 } |
| 71 | 71 |
| 72 bool ExtensionMessageBundle::GetMessageValue(const std::wstring& wkey, | 72 bool ExtensionMessageBundle::GetMessageValue(const std::wstring& wkey, |
| 73 const DictionaryValue& catalog, | 73 const DictionaryValue& catalog, |
| 74 std::string* value, | 74 std::string* value, |
| 75 std::string* error) const { | 75 std::string* error) const { |
| 76 std::string key(WideToUTF8(wkey)); | 76 std::string key(WideToUTF8(wkey)); |
| 77 // Get the top level tree for given key (name part). | 77 // Get the top level tree for given key (name part). |
| 78 DictionaryValue* name_tree; | 78 DictionaryValue* name_tree; |
| 79 if (!catalog.GetDictionary(wkey, &name_tree)) { | 79 if (!catalog.GetDictionaryWithoutPathExpansion(wkey, &name_tree)) { |
| 80 *error = StringPrintf("Not a valid tree for key %s.", key.c_str()); | 80 *error = StringPrintf("Not a valid tree for key %s.", key.c_str()); |
| 81 return false; | 81 return false; |
| 82 } | 82 } |
| 83 // Extract message from it. | 83 // Extract message from it. |
| 84 if (!name_tree->GetString(kMessageKey, value)) { | 84 if (!name_tree->GetString(kMessageKey, value)) { |
| 85 *error = StringPrintf("There is no \"%s\" element for key %s.", | 85 *error = StringPrintf("There is no \"%s\" element for key %s.", |
| 86 WideToUTF8(kMessageKey).c_str(), | 86 WideToUTF8(kMessageKey).c_str(), |
| 87 key.c_str()); | 87 key.c_str()); |
| 88 return false; | 88 return false; |
| 89 } | 89 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 110 | 110 |
| 111 DictionaryValue* placeholders_tree; | 111 DictionaryValue* placeholders_tree; |
| 112 if (!name_tree.GetDictionary(kPlaceholdersKey, &placeholders_tree)) { | 112 if (!name_tree.GetDictionary(kPlaceholdersKey, &placeholders_tree)) { |
| 113 *error = StringPrintf("Not a valid \"%s\" element for key %s.", | 113 *error = StringPrintf("Not a valid \"%s\" element for key %s.", |
| 114 WideToUTF8(kPlaceholdersKey).c_str(), | 114 WideToUTF8(kPlaceholdersKey).c_str(), |
| 115 name_key.c_str()); | 115 name_key.c_str()); |
| 116 return false; | 116 return false; |
| 117 } | 117 } |
| 118 | 118 |
| 119 for (DictionaryValue::key_iterator key_it = placeholders_tree->begin_keys(); | 119 for (DictionaryValue::key_iterator key_it = placeholders_tree->begin_keys(); |
| 120 key_it != placeholders_tree->end_keys(); | 120 key_it != placeholders_tree->end_keys(); ++key_it) { |
| 121 ++key_it) { | |
| 122 DictionaryValue* placeholder; | 121 DictionaryValue* placeholder; |
| 123 std::string content_key = WideToUTF8(*key_it); | 122 std::string content_key = WideToUTF8(*key_it); |
| 124 if (!IsValidName(*key_it)) | 123 if (!IsValidName(*key_it)) |
| 125 return BadKeyMessage(content_key, error); | 124 return BadKeyMessage(content_key, error); |
| 126 if (!placeholders_tree->GetDictionary(*key_it, &placeholder)) { | 125 if (!placeholders_tree->GetDictionaryWithoutPathExpansion(*key_it, |
| 126 &placeholder)) { |
| 127 *error = StringPrintf("Invalid placeholder %s for key %s", | 127 *error = StringPrintf("Invalid placeholder %s for key %s", |
| 128 content_key.c_str(), | 128 content_key.c_str(), |
| 129 name_key.c_str()); | 129 name_key.c_str()); |
| 130 return false; | 130 return false; |
| 131 } | 131 } |
| 132 std::string content; | 132 std::string content; |
| 133 if (!placeholder->GetString(kContentKey, &content)) { | 133 if (!placeholder->GetString(kContentKey, &content)) { |
| 134 *error = StringPrintf("Invalid \"%s\" element for key %s.", | 134 *error = StringPrintf("Invalid \"%s\" element for key %s.", |
| 135 WideToUTF8(kContentKey).c_str(), | 135 WideToUTF8(kContentKey).c_str(), |
| 136 name_key.c_str()); | 136 name_key.c_str()); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 std::string ExtensionMessageBundle::GetL10nMessage( | 237 std::string ExtensionMessageBundle::GetL10nMessage( |
| 238 const std::string& name, const SubstitutionMap& dictionary) { | 238 const std::string& name, const SubstitutionMap& dictionary) { |
| 239 SubstitutionMap::const_iterator it = | 239 SubstitutionMap::const_iterator it = |
| 240 dictionary.find(StringToLowerASCII(name)); | 240 dictionary.find(StringToLowerASCII(name)); |
| 241 if (it != dictionary.end()) { | 241 if (it != dictionary.end()) { |
| 242 return it->second; | 242 return it->second; |
| 243 } | 243 } |
| 244 | 244 |
| 245 return ""; | 245 return ""; |
| 246 } | 246 } |
| OLD | NEW |