l10n_file_util.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "extensions/browser/l10n_file_util.h"
  5. #include <utility>
  6. #include "base/files/file_path.h"
  7. #include "extensions/common/file_util.h"
  8. namespace extensions::l10n_file_util {
  9. MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMap(
  10. const base::FilePath& extension_path,
  11. const ExtensionId& extension_id,
  12. const std::string& default_locale,
  13. extension_l10n_util::GzippedMessagesPermission gzip_permission) {
  14. return LoadMessageBundleSubstitutionMapFromPaths(
  15. {extension_path}, extension_id, default_locale, gzip_permission);
  16. }
  17. MessageBundle::SubstitutionMap* LoadNonLocalizedMessageBundleSubstitutionMap(
  18. const ExtensionId& extension_id) {
  19. MessageBundle::SubstitutionMap* return_value =
  20. new MessageBundle::SubstitutionMap();
  21. // Add @@extension_id reserved message here.
  22. return_value->insert(
  23. std::make_pair(MessageBundle::kExtensionIdKey, extension_id));
  24. return return_value;
  25. }
  26. MessageBundle::SubstitutionMap* LoadMessageBundleSubstitutionMapFromPaths(
  27. const std::vector<base::FilePath>& paths,
  28. const ExtensionId& extension_id,
  29. const std::string& default_locale,
  30. extension_l10n_util::GzippedMessagesPermission gzip_permission) {
  31. MessageBundle::SubstitutionMap* return_value =
  32. LoadNonLocalizedMessageBundleSubstitutionMap(extension_id);
  33. // Touch disk only if extension is localized.
  34. if (default_locale.empty())
  35. return return_value;
  36. std::string error;
  37. for (const base::FilePath& path : paths) {
  38. std::unique_ptr<MessageBundle> bundle(file_util::LoadMessageBundle(
  39. path, default_locale, gzip_permission, &error));
  40. if (bundle) {
  41. for (const auto& iter : *bundle->dictionary()) {
  42. // |insert| only adds new entries, and does not replace entries in
  43. // the main extension or previously processed imports.
  44. return_value->insert(std::make_pair(iter.first, iter.second));
  45. }
  46. }
  47. }
  48. return return_value;
  49. }
  50. } // namespace extensions::l10n_file_util