1 // Copyright (c) 2012 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 CHROME_COMMON_EXTENSIONS_MESSAGE_BUNDLE_H_
6 #define CHROME_COMMON_EXTENSIONS_MESSAGE_BUNDLE_H_
12 #include "base/memory/linked_ptr.h"
15 class DictionaryValue
;
18 namespace extensions
{
20 // Contains localized extension messages for one locale. Any messages that the
21 // locale does not provide are pulled from the default locale.
24 typedef std::map
<std::string
, std::string
> SubstitutionMap
;
25 typedef std::vector
<linked_ptr
<base::DictionaryValue
> > CatalogVector
;
27 // JSON keys of interest for messages file.
28 static const char* kContentKey
;
29 static const char* kMessageKey
;
30 static const char* kPlaceholdersKey
;
32 // Begin/end markers for placeholders and messages
33 static const char* kPlaceholderBegin
;
34 static const char* kPlaceholderEnd
;
35 static const char* kMessageBegin
;
36 static const char* kMessageEnd
;
38 // Reserved message names in the dictionary.
39 // Update i18n documentation when adding new reserved value.
40 static const char* kUILocaleKey
;
41 // See http://code.google.com/apis/gadgets/docs/i18n.html#BIDI for
43 // TODO(cira): point to chrome docs once they are out.
44 static const char* kBidiDirectionKey
;
45 static const char* kBidiReversedDirectionKey
;
46 static const char* kBidiStartEdgeKey
;
47 static const char* kBidiEndEdgeKey
;
48 // Extension id gets added in the
49 // browser/renderer_host/resource_message_filter.cc to enable message
50 // replacement for non-localized extensions.
51 static const char* kExtensionIdKey
;
53 // Values for some of the reserved messages.
54 static const char* kBidiLeftEdgeValue
;
55 static const char* kBidiRightEdgeValue
;
57 // Creates MessageBundle or returns NULL if there was an error. Expects
58 // locale_catalogs to be sorted from more specific to less specific, with
59 // default catalog at the end.
60 static MessageBundle
* Create(const CatalogVector
& locale_catalogs
,
63 // Get message from the catalog with given key.
64 // Returned message has all of the internal placeholders resolved to their
66 // Returns empty string if it can't find a message.
67 // We don't use simple GetMessage name, since there is a global
68 // #define GetMessage GetMessageW override in Chrome code.
69 std::string
GetL10nMessage(const std::string
& name
) const;
71 // Get message from the given catalog with given key.
72 static std::string
GetL10nMessage(const std::string
& name
,
73 const SubstitutionMap
& dictionary
);
75 // Number of messages in the catalog.
76 // Used for unittesting only.
77 size_t size() const { return dictionary_
.size(); }
79 // Replaces all __MSG_message__ with values from the catalog.
80 // Returns false if there is a message in text that's not defined in the
82 bool ReplaceMessages(std::string
* text
, std::string
* error
) const;
83 // Static version that accepts dictionary.
84 static bool ReplaceMessagesWithExternalDictionary(
85 const SubstitutionMap
& dictionary
, std::string
* text
, std::string
* error
);
87 // Replaces each occurance of variable placeholder with its value.
88 // I.e. replaces __MSG_name__ with value from the catalog with the key "name".
89 // Returns false if for a valid message/placeholder name there is no matching
91 // Public for easier unittesting.
92 static bool ReplaceVariables(const SubstitutionMap
& variables
,
93 const std::string
& var_begin
,
94 const std::string
& var_end
,
98 // Allow only ascii 0-9, a-z, A-Z, and _ in the variable name.
99 // Returns false if the input is empty or if it has illegal characters.
100 static bool IsValidName(const std::string
& name
);
102 // Getter for dictionary_.
103 const SubstitutionMap
* dictionary() const { return &dictionary_
; }
109 friend class MessageBundleTest
;
111 // Use Create to create MessageBundle instance.
114 // Initializes the instance from the contents of vector of catalogs.
115 // If the key is not present in more specific catalog we fall back to next one
117 // Returns false on error.
118 bool Init(const CatalogVector
& locale_catalogs
, std::string
* error
);
120 // Appends locale specific reserved messages to the dictionary.
121 // Returns false if there was a conflict with user defined messages.
122 bool AppendReservedMessagesForLocale(const std::string
& application_locale
,
125 // Helper methods that navigate JSON tree and return simplified message.
126 // They replace all $PLACEHOLDERS$ with their value, and return just key/value
128 bool GetMessageValue(const std::string
& key
,
129 const base::DictionaryValue
& catalog
,
131 std::string
* error
) const;
133 // Get all placeholders for a given message from JSON subtree.
134 bool GetPlaceholders(const base::DictionaryValue
& name_tree
,
135 const std::string
& name_key
,
136 SubstitutionMap
* placeholders
,
137 std::string
* error
) const;
139 // For a given message, replaces all placeholders with their actual value.
140 // Returns false if replacement failed (see ReplaceVariables).
141 bool ReplacePlaceholders(const SubstitutionMap
& placeholders
,
142 std::string
* message
,
143 std::string
* error
) const;
145 // Holds all messages for application locale.
146 SubstitutionMap dictionary_
;
149 ///////////////////////////////////////////////////////////////////////////////
151 // Renderer helper typedefs and functions.
153 ///////////////////////////////////////////////////////////////////////////////
155 // A map of message name to message.
156 typedef std::map
<std::string
, std::string
> L10nMessagesMap
;
158 // A map of extension ID to l10n message map.
159 typedef std::map
<std::string
, L10nMessagesMap
> ExtensionToL10nMessagesMap
;
161 // Returns the extension_id to messages map.
162 ExtensionToL10nMessagesMap
* GetExtensionToL10nMessagesMap();
164 // Returns message map that matches given extension_id, or NULL.
165 L10nMessagesMap
* GetL10nMessagesMap(const std::string
& extension_id
);
167 } // namsepace extensions
169 #endif // CHROME_COMMON_EXTENSIONS_MESSAGE_BUNDLE_H_