OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/common/message_bundle.h" | 5 #include "extensions/common/message_bundle.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 69 |
70 bool MessageBundle::Init(const CatalogVector& locale_catalogs, | 70 bool MessageBundle::Init(const CatalogVector& locale_catalogs, |
71 std::string* error) { | 71 std::string* error) { |
72 dictionary_.clear(); | 72 dictionary_.clear(); |
73 | 73 |
74 for (CatalogVector::const_reverse_iterator it = locale_catalogs.rbegin(); | 74 for (CatalogVector::const_reverse_iterator it = locale_catalogs.rbegin(); |
75 it != locale_catalogs.rend(); ++it) { | 75 it != locale_catalogs.rend(); ++it) { |
76 base::DictionaryValue* catalog = (*it).get(); | 76 base::DictionaryValue* catalog = (*it).get(); |
77 for (base::DictionaryValue::Iterator message_it(*catalog); | 77 for (base::DictionaryValue::Iterator message_it(*catalog); |
78 !message_it.IsAtEnd(); message_it.Advance()) { | 78 !message_it.IsAtEnd(); message_it.Advance()) { |
79 std::string key(StringToLowerASCII(message_it.key())); | 79 std::string key(base::StringToLowerASCII(message_it.key())); |
80 if (!IsValidName(message_it.key())) | 80 if (!IsValidName(message_it.key())) |
81 return BadKeyMessage(key, error); | 81 return BadKeyMessage(key, error); |
82 std::string value; | 82 std::string value; |
83 if (!GetMessageValue(message_it.key(), message_it.value(), &value, error)) | 83 if (!GetMessageValue(message_it.key(), message_it.value(), &value, error)) |
84 return false; | 84 return false; |
85 // Keys are not case-sensitive. | 85 // Keys are not case-sensitive. |
86 dictionary_[key] = value; | 86 dictionary_[key] = value; |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 content_key.c_str(), | 184 content_key.c_str(), |
185 name_key.c_str()); | 185 name_key.c_str()); |
186 return false; | 186 return false; |
187 } | 187 } |
188 std::string content; | 188 std::string content; |
189 if (!placeholder->GetString(kContentKey, &content)) { | 189 if (!placeholder->GetString(kContentKey, &content)) { |
190 *error = base::StringPrintf("Invalid \"%s\" element for key %s.", | 190 *error = base::StringPrintf("Invalid \"%s\" element for key %s.", |
191 kContentKey, name_key.c_str()); | 191 kContentKey, name_key.c_str()); |
192 return false; | 192 return false; |
193 } | 193 } |
194 (*placeholders)[StringToLowerASCII(content_key)] = content; | 194 (*placeholders)[base::StringToLowerASCII(content_key)] = content; |
195 } | 195 } |
196 | 196 |
197 return true; | 197 return true; |
198 } | 198 } |
199 | 199 |
200 bool MessageBundle::ReplacePlaceholders(const SubstitutionMap& placeholders, | 200 bool MessageBundle::ReplacePlaceholders(const SubstitutionMap& placeholders, |
201 std::string* message, | 201 std::string* message, |
202 std::string* error) const { | 202 std::string* error) const { |
203 return ReplaceVariables(placeholders, | 203 return ReplaceVariables(placeholders, |
204 kPlaceholderBegin, | 204 kPlaceholderBegin, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 message->find(var_end_delimiter, beg_index); | 243 message->find(var_end_delimiter, beg_index); |
244 if (end_index == message->npos) | 244 if (end_index == message->npos) |
245 return true; | 245 return true; |
246 | 246 |
247 // Looking for 1 in substring of ...$1$.... | 247 // Looking for 1 in substring of ...$1$.... |
248 const std::string& var_name = | 248 const std::string& var_name = |
249 message->substr(beg_index, end_index - beg_index); | 249 message->substr(beg_index, end_index - beg_index); |
250 if (!IsValidName(var_name)) | 250 if (!IsValidName(var_name)) |
251 continue; | 251 continue; |
252 SubstitutionMap::const_iterator it = | 252 SubstitutionMap::const_iterator it = |
253 variables.find(StringToLowerASCII(var_name)); | 253 variables.find(base::StringToLowerASCII(var_name)); |
254 if (it == variables.end()) { | 254 if (it == variables.end()) { |
255 *error = base::StringPrintf("Variable %s%s%s used but not defined.", | 255 *error = base::StringPrintf("Variable %s%s%s used but not defined.", |
256 var_begin_delimiter.c_str(), | 256 var_begin_delimiter.c_str(), |
257 var_name.c_str(), | 257 var_name.c_str(), |
258 var_end_delimiter.c_str()); | 258 var_end_delimiter.c_str()); |
259 return false; | 259 return false; |
260 } | 260 } |
261 | 261 |
262 // Replace variable with its value. | 262 // Replace variable with its value. |
263 std::string value = it->second; | 263 std::string value = it->second; |
(...skipping 27 matching lines...) Expand all Loading... |
291 // Dictionary interface. | 291 // Dictionary interface. |
292 | 292 |
293 std::string MessageBundle::GetL10nMessage(const std::string& name) const { | 293 std::string MessageBundle::GetL10nMessage(const std::string& name) const { |
294 return GetL10nMessage(name, dictionary_); | 294 return GetL10nMessage(name, dictionary_); |
295 } | 295 } |
296 | 296 |
297 // static | 297 // static |
298 std::string MessageBundle::GetL10nMessage(const std::string& name, | 298 std::string MessageBundle::GetL10nMessage(const std::string& name, |
299 const SubstitutionMap& dictionary) { | 299 const SubstitutionMap& dictionary) { |
300 SubstitutionMap::const_iterator it = | 300 SubstitutionMap::const_iterator it = |
301 dictionary.find(StringToLowerASCII(name)); | 301 dictionary.find(base::StringToLowerASCII(name)); |
302 if (it != dictionary.end()) { | 302 if (it != dictionary.end()) { |
303 return it->second; | 303 return it->second; |
304 } | 304 } |
305 | 305 |
306 return std::string(); | 306 return std::string(); |
307 } | 307 } |
308 | 308 |
309 /////////////////////////////////////////////////////////////////////////////// | 309 /////////////////////////////////////////////////////////////////////////////// |
310 // | 310 // |
311 // Renderer helper functions. | 311 // Renderer helper functions. |
(...skipping 27 matching lines...) Expand all Loading... |
339 return &(it->second); | 339 return &(it->second); |
340 | 340 |
341 return NULL; | 341 return NULL; |
342 } | 342 } |
343 | 343 |
344 void EraseL10nMessagesMap(const std::string& extension_id) { | 344 void EraseL10nMessagesMap(const std::string& extension_id) { |
345 g_extension_to_messages_map.Get().messages_map.erase(extension_id); | 345 g_extension_to_messages_map.Get().messages_map.erase(extension_id); |
346 } | 346 } |
347 | 347 |
348 } // namespace extensions | 348 } // namespace extensions |
OLD | NEW |