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(base::StringToLowerASCII(message_it.key())); | 79 std::string key(base::ToLowerASCII(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)[base::StringToLowerASCII(content_key)] = content; | 194 (*placeholders)[base::ToLowerASCII(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 28 matching lines...) Expand all Loading... |
233 while (true) { | 233 while (true) { |
234 beg_index = message->find(var_begin_delimiter, beg_index); | 234 beg_index = message->find(var_begin_delimiter, beg_index); |
235 if (beg_index == message->npos) | 235 if (beg_index == message->npos) |
236 return true; | 236 return true; |
237 | 237 |
238 // Advance it immediately to the begining of possible variable name. | 238 // Advance it immediately to the begining of possible variable name. |
239 beg_index += var_begin_delimiter_size; | 239 beg_index += var_begin_delimiter_size; |
240 if (beg_index >= message->size()) | 240 if (beg_index >= message->size()) |
241 return true; | 241 return true; |
242 std::string::size_type end_index = | 242 std::string::size_type end_index = |
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 auto it = variables.find(base::ToLowerASCII(var_name)); |
253 variables.find(base::StringToLowerASCII(var_name)); | |
254 if (it == variables.end()) { | 253 if (it == variables.end()) { |
255 *error = base::StringPrintf("Variable %s%s%s used but not defined.", | 254 *error = base::StringPrintf("Variable %s%s%s used but not defined.", |
256 var_begin_delimiter.c_str(), | 255 var_begin_delimiter.c_str(), |
257 var_name.c_str(), | 256 var_name.c_str(), |
258 var_end_delimiter.c_str()); | 257 var_end_delimiter.c_str()); |
259 return false; | 258 return false; |
260 } | 259 } |
261 | 260 |
262 // Replace variable with its value. | 261 // Replace variable with its value. |
263 std::string value = it->second; | 262 std::string value = it->second; |
(...skipping 27 matching lines...) Expand all Loading... |
291 | 290 |
292 // Dictionary interface. | 291 // Dictionary interface. |
293 | 292 |
294 std::string MessageBundle::GetL10nMessage(const std::string& name) const { | 293 std::string MessageBundle::GetL10nMessage(const std::string& name) const { |
295 return GetL10nMessage(name, dictionary_); | 294 return GetL10nMessage(name, dictionary_); |
296 } | 295 } |
297 | 296 |
298 // static | 297 // static |
299 std::string MessageBundle::GetL10nMessage(const std::string& name, | 298 std::string MessageBundle::GetL10nMessage(const std::string& name, |
300 const SubstitutionMap& dictionary) { | 299 const SubstitutionMap& dictionary) { |
301 SubstitutionMap::const_iterator it = | 300 auto it = dictionary.find(base::ToLowerASCII(name)); |
302 dictionary.find(base::StringToLowerASCII(name)); | |
303 if (it != dictionary.end()) { | 301 if (it != dictionary.end()) { |
304 return it->second; | 302 return it->second; |
305 } | 303 } |
306 | 304 |
307 return std::string(); | 305 return std::string(); |
308 } | 306 } |
309 | 307 |
310 /////////////////////////////////////////////////////////////////////////////// | 308 /////////////////////////////////////////////////////////////////////////////// |
311 // | 309 // |
312 // Renderer helper functions. | 310 // Renderer helper functions. |
(...skipping 27 matching lines...) Expand all Loading... |
340 return &(it->second); | 338 return &(it->second); |
341 | 339 |
342 return NULL; | 340 return NULL; |
343 } | 341 } |
344 | 342 |
345 void EraseL10nMessagesMap(const std::string& extension_id) { | 343 void EraseL10nMessagesMap(const std::string& extension_id) { |
346 g_extension_to_messages_map.Get().messages_map.erase(extension_id); | 344 g_extension_to_messages_map.Get().messages_map.erase(extension_id); |
347 } | 345 } |
348 | 346 |
349 } // namespace extensions | 347 } // namespace extensions |
OLD | NEW |