| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/files/file_enumerator.h" | 14 #include "base/files/file_enumerator.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/json/json_file_value_serializer.h" | 16 #include "base/json/json_file_value_serializer.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/memory/linked_ptr.h" | 18 #include "base/memory/linked_ptr.h" |
| 19 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/values.h" | 22 #include "base/values.h" |
| 22 #include "extensions/common/constants.h" | 23 #include "extensions/common/constants.h" |
| 23 #include "extensions/common/error_utils.h" | 24 #include "extensions/common/error_utils.h" |
| 24 #include "extensions/common/file_util.h" | 25 #include "extensions/common/file_util.h" |
| 25 #include "extensions/common/manifest_constants.h" | 26 #include "extensions/common/manifest_constants.h" |
| 26 #include "extensions/common/message_bundle.h" | 27 #include "extensions/common/message_bundle.h" |
| 27 #include "third_party/icu/source/common/unicode/uloc.h" | 28 #include "third_party/icu/source/common/unicode/uloc.h" |
| 28 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 base::DictionaryValue* manifest, | 264 base::DictionaryValue* manifest, |
| 264 std::string* error) { | 265 std::string* error) { |
| 265 DCHECK(manifest); | 266 DCHECK(manifest); |
| 266 | 267 |
| 267 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); | 268 std::string default_locale = GetDefaultLocaleFromManifest(*manifest, error); |
| 268 | 269 |
| 269 std::unique_ptr<extensions::MessageBundle> message_bundle( | 270 std::unique_ptr<extensions::MessageBundle> message_bundle( |
| 270 extensions::file_util::LoadMessageBundle(extension_path, default_locale, | 271 extensions::file_util::LoadMessageBundle(extension_path, default_locale, |
| 271 error)); | 272 error)); |
| 272 | 273 |
| 273 if (!message_bundle.get() && !error->empty()) | 274 if (!message_bundle && !error->empty()) |
| 274 return false; | 275 return false; |
| 275 | 276 |
| 276 if (message_bundle.get() && | 277 if (message_bundle && !LocalizeManifest(*message_bundle, manifest, error)) |
| 277 !LocalizeManifest(*message_bundle, manifest, error)) | |
| 278 return false; | 278 return false; |
| 279 | 279 |
| 280 return true; | 280 return true; |
| 281 } | 281 } |
| 282 | 282 |
| 283 bool AddLocale(const std::set<std::string>& chrome_locales, | 283 bool AddLocale(const std::set<std::string>& chrome_locales, |
| 284 const base::FilePath& locale_folder, | 284 const base::FilePath& locale_folder, |
| 285 const std::string& locale_name, | 285 const std::string& locale_name, |
| 286 std::set<std::string>* valid_locales, | 286 std::set<std::string>* valid_locales, |
| 287 std::string* error) { | 287 std::string* error) { |
| 288 // Accept name that starts with a . but don't add it to the list of supported | 288 // Accept name that starts with a . but don't add it to the list of supported |
| 289 // locales. | 289 // locales. |
| 290 if (locale_name.find(".") == 0) | 290 if (base::StartsWith(locale_name, ".", base::CompareCase::SENSITIVE)) |
| 291 return true; | 291 return true; |
| 292 if (chrome_locales.find(locale_name) == chrome_locales.end()) { | 292 if (chrome_locales.find(locale_name) == chrome_locales.end()) { |
| 293 // Warn if there is an extension locale that's not in the Chrome list, | 293 // Warn if there is an extension locale that's not in the Chrome list, |
| 294 // but don't fail. | 294 // but don't fail. |
| 295 DLOG(WARNING) << base::StringPrintf("Supplied locale %s is not supported.", | 295 DLOG(WARNING) << base::StringPrintf("Supplied locale %s is not supported.", |
| 296 locale_name.c_str()); | 296 locale_name.c_str()); |
| 297 return true; | 297 return true; |
| 298 } | 298 } |
| 299 // Check if messages file is actually present (but don't check content). | 299 // Check if messages file is actually present (but don't check content). |
| 300 if (base::PathExists(locale_folder.Append(extensions::kMessagesFilename))) { | 300 if (!base::PathExists(locale_folder.Append(extensions::kMessagesFilename))) { |
| 301 valid_locales->insert(locale_name); | |
| 302 } else { | |
| 303 *error = base::StringPrintf("Catalog file is missing for locale %s.", | 301 *error = base::StringPrintf("Catalog file is missing for locale %s.", |
| 304 locale_name.c_str()); | 302 locale_name.c_str()); |
| 305 return false; | 303 return false; |
| 306 } | 304 } |
| 307 | 305 |
| 306 valid_locales->insert(locale_name); |
| 308 return true; | 307 return true; |
| 309 } | 308 } |
| 310 | 309 |
| 311 std::string CurrentLocaleOrDefault() { | 310 std::string CurrentLocaleOrDefault() { |
| 312 std::string current_locale = l10n_util::NormalizeLocale(GetProcessLocale()); | 311 std::string current_locale = l10n_util::NormalizeLocale(GetProcessLocale()); |
| 313 if (current_locale.empty()) | 312 if (current_locale.empty()) |
| 314 current_locale = "en"; | 313 current_locale = "en"; |
| 315 | 314 |
| 316 return current_locale; | 315 return current_locale; |
| 317 } | 316 } |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) | 458 ScopedLocaleForTest::ScopedLocaleForTest(const std::string& locale) |
| 460 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { | 459 : locale_(extension_l10n_util::CurrentLocaleOrDefault()) { |
| 461 extension_l10n_util::SetProcessLocale(locale); | 460 extension_l10n_util::SetProcessLocale(locale); |
| 462 } | 461 } |
| 463 | 462 |
| 464 ScopedLocaleForTest::~ScopedLocaleForTest() { | 463 ScopedLocaleForTest::~ScopedLocaleForTest() { |
| 465 extension_l10n_util::SetProcessLocale(locale_); | 464 extension_l10n_util::SetProcessLocale(locale_); |
| 466 } | 465 } |
| 467 | 466 |
| 468 } // namespace extension_l10n_util | 467 } // namespace extension_l10n_util |
| OLD | NEW |