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/extension_l10n_util.h" | 5 #include "extensions/common/extension_l10n_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <set> | 10 #include <set> |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 // Loads contents of the messages file for given locale. If file is not found, | 35 // Loads contents of the messages file for given locale. If file is not found, |
36 // or there was parsing error we return NULL and set |error|. | 36 // or there was parsing error we return NULL and set |error|. |
37 // Caller owns the returned object. | 37 // Caller owns the returned object. |
38 base::DictionaryValue* LoadMessageFile(const base::FilePath& locale_path, | 38 base::DictionaryValue* LoadMessageFile(const base::FilePath& locale_path, |
39 const std::string& locale, | 39 const std::string& locale, |
40 std::string* error) { | 40 std::string* error) { |
41 base::FilePath file = | 41 base::FilePath file = |
42 locale_path.AppendASCII(locale).Append(extensions::kMessagesFilename); | 42 locale_path.AppendASCII(locale).Append(extensions::kMessagesFilename); |
43 JSONFileValueDeserializer messages_deserializer(file); | 43 JSONFileValueDeserializer messages_deserializer(file); |
44 scoped_ptr<base::DictionaryValue> dictionary = base::DictionaryValue::From( | 44 std::unique_ptr<base::DictionaryValue> dictionary = |
45 messages_deserializer.Deserialize(NULL, error)); | 45 base::DictionaryValue::From( |
| 46 messages_deserializer.Deserialize(NULL, error)); |
46 if (!dictionary) { | 47 if (!dictionary) { |
47 if (error->empty()) { | 48 if (error->empty()) { |
48 // JSONFileValueSerializer just returns NULL if file cannot be found. It | 49 // JSONFileValueSerializer just returns NULL if file cannot be found. It |
49 // doesn't set the error, so we have to do it. | 50 // doesn't set the error, so we have to do it. |
50 *error = base::StringPrintf("Catalog file is missing for locale %s.", | 51 *error = base::StringPrintf("Catalog file is missing for locale %s.", |
51 locale.c_str()); | 52 locale.c_str()); |
52 } else { | 53 } else { |
53 *error = extensions::ErrorUtils::FormatErrorMessage( | 54 *error = extensions::ErrorUtils::FormatErrorMessage( |
54 errors::kLocalesInvalidLocale, | 55 errors::kLocalesInvalidLocale, |
55 base::UTF16ToUTF8(file.LossyDisplayName()), | 56 base::UTF16ToUTF8(file.LossyDisplayName()), |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 return true; | 259 return true; |
259 } | 260 } |
260 | 261 |
261 bool LocalizeExtension(const base::FilePath& extension_path, | 262 bool LocalizeExtension(const base::FilePath& extension_path, |
262 base::DictionaryValue* manifest, | 263 base::DictionaryValue* manifest, |
263 std::string* error) { | 264 std::string* error) { |
264 DCHECK(manifest); | 265 DCHECK(manifest); |
265 | 266 |
266 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); | 267 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); |
267 | 268 |
268 scoped_ptr<extensions::MessageBundle> message_bundle( | 269 std::unique_ptr<extensions::MessageBundle> message_bundle( |
269 extensions::file_util::LoadMessageBundle( | 270 extensions::file_util::LoadMessageBundle(extension_path, default_locale, |
270 extension_path, default_locale, error)); | 271 error)); |
271 | 272 |
272 if (!message_bundle.get() && !error->empty()) | 273 if (!message_bundle.get() && !error->empty()) |
273 return false; | 274 return false; |
274 | 275 |
275 if (message_bundle.get() && | 276 if (message_bundle.get() && |
276 !LocalizeManifest(*message_bundle, manifest, error)) | 277 !LocalizeManifest(*message_bundle, manifest, error)) |
277 return false; | 278 return false; |
278 | 279 |
279 return true; | 280 return true; |
280 } | 281 } |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder); | 409 base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder); |
409 | 410 |
410 std::set<std::string> valid_locales; | 411 std::set<std::string> valid_locales; |
411 if (!GetValidLocales(locale_path, &valid_locales, error)) | 412 if (!GetValidLocales(locale_path, &valid_locales, error)) |
412 return false; | 413 return false; |
413 | 414 |
414 for (std::set<std::string>::const_iterator locale = valid_locales.begin(); | 415 for (std::set<std::string>::const_iterator locale = valid_locales.begin(); |
415 locale != valid_locales.end(); | 416 locale != valid_locales.end(); |
416 ++locale) { | 417 ++locale) { |
417 std::string locale_error; | 418 std::string locale_error; |
418 scoped_ptr<base::DictionaryValue> catalog( | 419 std::unique_ptr<base::DictionaryValue> catalog( |
419 LoadMessageFile(locale_path, *locale, &locale_error)); | 420 LoadMessageFile(locale_path, *locale, &locale_error)); |
420 | 421 |
421 if (!locale_error.empty()) { | 422 if (!locale_error.empty()) { |
422 if (!error->empty()) | 423 if (!error->empty()) |
423 error->append(" "); | 424 error->append(" "); |
424 error->append(locale_error); | 425 error->append(locale_error); |
425 } | 426 } |
426 } | 427 } |
427 | 428 |
428 return error->empty(); | 429 return error->empty(); |
(...skipping 29 matching lines...) Expand all Loading... |
458 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) | 459 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) |
459 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { | 460 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { |
460 extension_l10n_util::SetProcessLocale(locale); | 461 extension_l10n_util::SetProcessLocale(locale); |
461 } | 462 } |
462 | 463 |
463 ScopedLocaleForTest::~ScopedLocaleForTest() { | 464 ScopedLocaleForTest::~ScopedLocaleForTest() { |
464 extension_l10n_util::SetProcessLocale(locale_); | 465 extension_l10n_util::SetProcessLocale(locale_); |
465 } | 466 } |
466 | 467 |
467 } // namespace extension_l10n_util | 468 } // namespace extension_l10n_util |
OLD | NEW |