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.
5 #ifndef EXTENSIONS_COMMON_MESSAGE_BUNDLE_H_
6 #define EXTENSIONS_COMMON_MESSAGE_BUNDLE_H_
12 #include "base/memory/linked_ptr.h"
15 class DictionaryValue
;
19 namespace extensions
{
21 // Contains localized extension messages for one locale. Any messages that the
22 // locale does not provide are pulled from the default locale.
25 typedef std::map
<std::string
, std::string
> SubstitutionMap
;
26 typedef std::vector
<linked_ptr
<base::DictionaryValue
> > CatalogVector
;
28 // JSON keys of interest for messages file.
29 static const char* kContentKey
;
30 static const char* kMessageKey
;
31 static const char* kPlaceholdersKey
;
33 // Begin/end markers for placeholders and messages
34 static const char* kPlaceholderBegin
;
35 static const char* kPlaceholderEnd
;
36 static const char* kMessageBegin
;
37 static const char* kMessageEnd
;
39 // Reserved message names in the dictionary.
40 // Update i18n documentation when adding new reserved value.
41 static const char* kUILocaleKey
;
42 // See http://code.google.com/apis/gadgets/docs/i18n.html#BIDI for
44 // TODO(cira): point to chrome docs once they are out.
45 static const char* kBidiDirectionKey
;
46 static const char* kBidiReversedDirectionKey
;
47 static const char* kBidiStartEdgeKey
;
48 static const char* kBidiEndEdgeKey
;
49 // Extension id gets added in the
50 // browser/renderer_host/resource_message_filter.cc to enable message
51 // replacement for non-localized extensions.
52 static const char* kExtensionIdKey
;
54 // Values for some of the reserved messages.
55 static const char* kBidiLeftEdgeValue
;
56 static const char* kBidiRightEdgeValue
;
58 // Creates MessageBundle or returns NULL if there was an error. Expects
59 // locale_catalogs to be sorted from more specific to less specific, with
60 // default catalog at the end.
61 static MessageBundle
* Create(const CatalogVector
& locale_catalogs
,
64 // Get message from the catalog with given key.
65 // Returned message has all of the internal placeholders resolved to their
67 // Returns empty string if it can't find a message.
68 // We don't use simple GetMessage name, since there is a global
69 // #define GetMessage GetMessageW override in Chrome code.
70 std::string
GetL10nMessage(const std::string
& name
) const;
72 // Get message from the given catalog with given key.
73 static std::string
GetL10nMessage(const std::string
& name
,
74 const SubstitutionMap
& dictionary
);
76 // Number of messages in the catalog.
77 // Used for unittesting only.
78 size_t size() const { return dictionary_
.size(); }
80 // Replaces all __MSG_message__ with values from the catalog.
81 // Returns false if there is a message in text that's not defined in the
83 bool ReplaceMessages(std::string
* text
, std::string
* error
) const;
84 // Static version that accepts dictionary.
85 static bool ReplaceMessagesWithExternalDictionary(
86 const SubstitutionMap
& dictionary
, std::string
* text
, std::string
* error
);
88 // Replaces each occurance of variable placeholder with its value.
89 // I.e. replaces __MSG_name__ with value from the catalog with the key "name".
90 // Returns false if for a valid message/placeholder name there is no matching
92 // Public for easier unittesting.
93 static bool ReplaceVariables(const SubstitutionMap
& variables
,
94 const std::string
& var_begin
,
95 const std::string
& var_end
,
99 // Allow only ascii 0-9, a-z, A-Z, and _ in the variable name.
100 // Returns false if the input is empty or if it has illegal characters.
101 static bool IsValidName(const std::string
& name
);
103 // Getter for dictionary_.
104 const SubstitutionMap
* dictionary() const { return &dictionary_
; }
110 friend class MessageBundleTest
;
112 // Use Create to create MessageBundle instance.
115 // Initializes the instance from the contents of vector of catalogs.
116 // If the key is not present in more specific catalog we fall back to next one
118 // Returns false on error.
119 bool Init(const CatalogVector
& locale_catalogs
, std::string
* error
);
121 // Appends locale specific reserved messages to the dictionary.
122 // Returns false if there was a conflict with user defined messages.
123 bool AppendReservedMessagesForLocale(const std::string
& application_locale
,
126 // Helper methods that navigate JSON tree and return simplified message.
127 // They replace all $PLACEHOLDERS$ with their value, and return just key/value
129 bool GetMessageValue(const std::string
& key
,
130 const base::Value
& name_value
,
132 std::string
* error
) const;
134 // Get all placeholders for a given message from JSON subtree.
135 bool GetPlaceholders(const base::DictionaryValue
& name_tree
,
136 const std::string
& name_key
,
137 SubstitutionMap
* placeholders
,
138 std::string
* error
) const;
140 // For a given message, replaces all placeholders with their actual value.
141 // Returns false if replacement failed (see ReplaceVariables).
142 bool ReplacePlaceholders(const SubstitutionMap
& placeholders
,
143 std::string
* message
,
144 std::string
* error
) const;
146 // Holds all messages for application locale.
147 SubstitutionMap dictionary_
;
150 ///////////////////////////////////////////////////////////////////////////////
152 // Renderer helper typedefs and functions.
154 ///////////////////////////////////////////////////////////////////////////////
156 // A map of message name to message.
157 typedef std::map
<std::string
, std::string
> L10nMessagesMap
;
159 // A map of extension ID to l10n message map.
160 typedef std::map
<std::string
, L10nMessagesMap
> ExtensionToL10nMessagesMap
;
162 // Returns the extension_id to messages map.
163 ExtensionToL10nMessagesMap
* GetExtensionToL10nMessagesMap();
165 // Returns message map that matches given extension_id, or NULL.
166 L10nMessagesMap
* GetL10nMessagesMap(const std::string
& extension_id
);
168 // Erases the L10nMessagesMap for the given |extension_id|.
169 void EraseL10nMessagesMap(const std::string
& extension_id
);
171 } // namespace extensions
173 #endif // EXTENSIONS_COMMON_MESSAGE_BUNDLE_H_