OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/hash_tables.h" | |
11 #include "base/values.h" | |
12 | |
13 // Contains localized extension messages for one locale. Any messages that the | |
14 // locale does not provide are pulled from the default locale. | |
15 class ExtensionMessageBundle { | |
16 public: | |
17 typedef base::hash_map<std::string, std::string> SubstitutionMap; | |
18 | |
19 // JSON keys of interest for messages file. | |
20 static const wchar_t* kContentKey; | |
21 static const wchar_t* kMessageKey; | |
22 static const wchar_t* kPlaceholdersKey; | |
23 | |
24 // Begin/end markers for placeholders and messages | |
25 static const char* kPlaceholderBegin; | |
26 static const char* kPlaceholderEnd; | |
27 static const char* kMessageBegin; | |
28 static const char* kMessageEnd; | |
29 | |
30 // Extension name and description message names | |
31 static const char* kExtensionName; | |
32 static const char* kExtensionDescription; | |
33 | |
34 // Creates ExtensionMessageBundle or returns NULL if there was an error. | |
35 static ExtensionMessageBundle* Create( | |
36 const DictionaryValue& default_locale_catalog, | |
37 const DictionaryValue& current_locale_catalog, | |
38 std::string* error); | |
39 | |
40 // Get message from the catalog with given key. | |
41 // Returned message has all of the internal placeholders resolved to their | |
42 // value (content). | |
43 // Returns empty string if it can't find a message. | |
44 // We don't use simple GetMessage name, since there is a global | |
45 // #define GetMessage GetMessageW override in Chrome code. | |
46 std::string GetL10nMessage(const std::string& name) const; | |
47 | |
48 // Number of messages in the catalog. | |
49 // Used for unittesting only. | |
50 size_t size() const { return dictionary_.size(); } | |
51 | |
52 // Replaces all __MSG_message__ with values from the catalog. | |
53 // Returns false if there is a message in text that's not defined in the | |
54 // dictionary. | |
55 bool ReplaceMessages(std::string* text, std::string* error) const; | |
56 | |
57 // Replaces each occurance of variable placeholder with its value. | |
58 // I.e. replaces __MSG_name__ with value from the catalog with the key "name". | |
59 // Returns false if for a valid message/placeholder name there is no matching | |
60 // replacement. | |
61 // Public for easier unittesting. | |
62 static bool ReplaceVariables(const SubstitutionMap& variables, | |
63 const std::string& var_begin, | |
64 const std::string& var_end, | |
65 std::string* message, | |
66 std::string* error); | |
67 | |
68 // Allow only ascii 0-9, a-z, A-Z, and _ in the variable name. | |
69 // Returns false if the input is empty or if it has illegal characters. | |
70 // Public for easier unittesting. | |
71 template<typename str> | |
72 static bool IsValidName(const str& name); | |
73 | |
74 private: | |
75 // Use Create to create ExtensionMessageBundle instance. | |
76 ExtensionMessageBundle(); | |
77 | |
78 // Initializes the instance from the contents of two catalogs. If a key is not | |
79 // present in current_locale_catalog, the value from default_local_catalog is | |
80 // used instead. | |
81 // Returns false on error. | |
82 bool Init(const DictionaryValue& default_locale_catalog, | |
83 const DictionaryValue& current_locale_catalog, | |
84 std::string* error); | |
85 | |
86 // Helper methods that navigate JSON tree and return simplified message. | |
87 // They replace all $PLACEHOLDERS$ with their value, and return just key/value | |
88 // of the message. | |
89 bool GetMessageValue(const std::wstring& wkey, | |
90 const DictionaryValue& catalog, | |
91 std::string* value, | |
92 std::string* error) const; | |
93 | |
94 // Get all placeholders for a given message from JSON subtree. | |
95 bool GetPlaceholders(const DictionaryValue& name_tree, | |
96 const std::string& name_key, | |
97 SubstitutionMap* placeholders, | |
98 std::string* error) const; | |
99 | |
100 // For a given message, replaces all placeholders with their actual value. | |
101 // Returns false if replacement failed (see ReplaceVariables). | |
102 bool ReplacePlaceholders(const SubstitutionMap& placeholders, | |
103 std::string* message, | |
104 std::string* error) const; | |
105 | |
106 // Holds all messages for application locale. | |
107 SubstitutionMap dictionary_; | |
108 }; | |
109 | |
110 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ | |
OLD | NEW |