message_bundle.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  3. // found in the LICENSE file.
  4. #ifndef EXTENSIONS_COMMON_MESSAGE_BUNDLE_H_
  5. #define EXTENSIONS_COMMON_MESSAGE_BUNDLE_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. namespace base {
  12. class DictionaryValue;
  13. class Value;
  14. }
  15. namespace extensions {
  16. // Contains localized extension messages for one locale. Any messages that the
  17. // locale does not provide are pulled from the default locale.
  18. class MessageBundle {
  19. public:
  20. using SubstitutionMap = std::map<std::string, std::string>;
  21. using CatalogVector = std::vector<std::unique_ptr<base::DictionaryValue>>;
  22. // JSON keys of interest for messages file.
  23. static const char kContentKey[];
  24. static const char kMessageKey[];
  25. static const char kPlaceholdersKey[];
  26. // Begin/end markers for placeholders and messages
  27. static const char kPlaceholderBegin[];
  28. static const char kPlaceholderEnd[];
  29. static const char kMessageBegin[];
  30. static const char kMessageEnd[];
  31. // Reserved message names in the dictionary.
  32. // Update i18n documentation when adding new reserved value.
  33. static const char kUILocaleKey[];
  34. // See http://code.google.com/apis/gadgets/docs/i18n.html#BIDI for
  35. // description.
  36. // TODO(cira): point to chrome docs once they are out.
  37. static const char kBidiDirectionKey[];
  38. static const char kBidiReversedDirectionKey[];
  39. static const char kBidiStartEdgeKey[];
  40. static const char kBidiEndEdgeKey[];
  41. // Extension id gets added in the
  42. // browser/renderer_host/resource_message_filter.cc to enable message
  43. // replacement for non-localized extensions.
  44. static const char kExtensionIdKey[];
  45. // Values for some of the reserved messages.
  46. static const char kBidiLeftEdgeValue[];
  47. static const char kBidiRightEdgeValue[];
  48. // Creates MessageBundle or returns NULL if there was an error. Expects
  49. // locale_catalogs to be sorted from more specific to less specific, with
  50. // default catalog at the end.
  51. static MessageBundle* Create(const CatalogVector& locale_catalogs,
  52. std::string* error);
  53. // Get message from the catalog with given key.
  54. // Returned message has all of the internal placeholders resolved to their
  55. // value (content).
  56. // Returns empty string if it can't find a message.
  57. // We don't use simple GetMessage name, since there is a global
  58. // #define GetMessage GetMessageW override in Chrome code.
  59. std::string GetL10nMessage(const std::string& name) const;
  60. // Get message from the given catalog with given key.
  61. static std::string GetL10nMessage(const std::string& name,
  62. const SubstitutionMap& dictionary);
  63. // Number of messages in the catalog.
  64. // Used for unittesting only.
  65. size_t size() const { return dictionary_.size(); }
  66. // Replaces all __MSG_message__ with values from the catalog.
  67. // Returns false if there is a message in text that's not defined in the
  68. // dictionary.
  69. bool ReplaceMessages(std::string* text, std::string* error) const;
  70. // Static version that accepts dictionary.
  71. static bool ReplaceMessagesWithExternalDictionary(
  72. const SubstitutionMap& dictionary, std::string* text, std::string* error);
  73. // Replaces each occurance of variable placeholder with its value.
  74. // I.e. replaces __MSG_name__ with value from the catalog with the key "name".
  75. // Returns false if for a valid message/placeholder name there is no matching
  76. // replacement.
  77. // Public for easier unittesting.
  78. static bool ReplaceVariables(const SubstitutionMap& variables,
  79. const std::string& var_begin,
  80. const std::string& var_end,
  81. std::string* message,
  82. std::string* error);
  83. // Allow only ascii 0-9, a-z, A-Z, and _ in the variable name.
  84. // Returns false if the input is empty or if it has illegal characters.
  85. static bool IsValidName(const std::string& name);
  86. // Getter for dictionary_.
  87. const SubstitutionMap* dictionary() const { return &dictionary_; }
  88. ~MessageBundle();
  89. private:
  90. // Testing friend.
  91. friend class MessageBundleTest;
  92. // Use Create to create MessageBundle instance.
  93. MessageBundle();
  94. // Initializes the instance from the contents of vector of catalogs.
  95. // If the key is not present in more specific catalog we fall back to next one
  96. // (less specific).
  97. // Returns false on error.
  98. bool Init(const CatalogVector& locale_catalogs, std::string* error);
  99. // Appends locale specific reserved messages to the dictionary.
  100. // Returns false if there was a conflict with user defined messages.
  101. bool AppendReservedMessagesForLocale(const std::string& application_locale,
  102. std::string* error);
  103. // Helper methods that navigate JSON tree and return simplified message.
  104. // They replace all $PLACEHOLDERS$ with their value, and return just key/value
  105. // of the message.
  106. bool GetMessageValue(const std::string& key,
  107. const base::Value& name_value,
  108. std::string* value,
  109. std::string* error) const;
  110. // Get all placeholders for a given message from JSON subtree.
  111. bool GetPlaceholders(const base::DictionaryValue& name_tree,
  112. const std::string& name_key,
  113. SubstitutionMap* placeholders,
  114. std::string* error) const;
  115. // For a given message, replaces all placeholders with their actual value.
  116. // Returns false if replacement failed (see ReplaceVariables).
  117. bool ReplacePlaceholders(const SubstitutionMap& placeholders,
  118. std::string* message,
  119. std::string* error) const;
  120. // Holds all messages for application locale.
  121. SubstitutionMap dictionary_;
  122. };
  123. } // namespace extensions
  124. #endif // EXTENSIONS_COMMON_MESSAGE_BUNDLE_H_