Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: chrome/common/extensions/extension_message_bundle.h

Issue 225009: Implementing chrome.i18n.getMessage call, that loads message from the extensi... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_
7 7
8 #include <map>
8 #include <string> 9 #include <string>
9 10
10 #include "base/hash_tables.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 12
13 // Contains localized extension messages for one locale. Any messages that the 13 // Contains localized extension messages for one locale. Any messages that the
14 // locale does not provide are pulled from the default locale. 14 // locale does not provide are pulled from the default locale.
15 class ExtensionMessageBundle { 15 class ExtensionMessageBundle {
16 public: 16 public:
17 typedef base::hash_map<std::string, std::string> SubstitutionMap; 17 typedef std::map<std::string, std::string> SubstitutionMap;
18 18
19 // JSON keys of interest for messages file. 19 // JSON keys of interest for messages file.
20 static const wchar_t* kContentKey; 20 static const wchar_t* kContentKey;
21 static const wchar_t* kMessageKey; 21 static const wchar_t* kMessageKey;
22 static const wchar_t* kPlaceholdersKey; 22 static const wchar_t* kPlaceholdersKey;
23 23
24 // Begin/end markers for placeholders and messages 24 // Begin/end markers for placeholders and messages
25 static const char* kPlaceholderBegin; 25 static const char* kPlaceholderBegin;
26 static const char* kPlaceholderEnd; 26 static const char* kPlaceholderEnd;
27 static const char* kMessageBegin; 27 static const char* kMessageBegin;
(...skipping 10 matching lines...) Expand all
38 std::string* error); 38 std::string* error);
39 39
40 // Get message from the catalog with given key. 40 // Get message from the catalog with given key.
41 // Returned message has all of the internal placeholders resolved to their 41 // Returned message has all of the internal placeholders resolved to their
42 // value (content). 42 // value (content).
43 // Returns empty string if it can't find a message. 43 // Returns empty string if it can't find a message.
44 // We don't use simple GetMessage name, since there is a global 44 // We don't use simple GetMessage name, since there is a global
45 // #define GetMessage GetMessageW override in Chrome code. 45 // #define GetMessage GetMessageW override in Chrome code.
46 std::string GetL10nMessage(const std::string& name) const; 46 std::string GetL10nMessage(const std::string& name) const;
47 47
48 // Get message from the given catalog with given key.
49 static std::string GetL10nMessage(const std::string& name,
50 const SubstitutionMap& dictionary);
51
48 // Number of messages in the catalog. 52 // Number of messages in the catalog.
49 // Used for unittesting only. 53 // Used for unittesting only.
50 size_t size() const { return dictionary_.size(); } 54 size_t size() const { return dictionary_.size(); }
51 55
52 // Replaces all __MSG_message__ with values from the catalog. 56 // 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 57 // Returns false if there is a message in text that's not defined in the
54 // dictionary. 58 // dictionary.
55 bool ReplaceMessages(std::string* text, std::string* error) const; 59 bool ReplaceMessages(std::string* text, std::string* error) const;
56 60
57 // Replaces each occurance of variable placeholder with its value. 61 // Replaces each occurance of variable placeholder with its value.
58 // I.e. replaces __MSG_name__ with value from the catalog with the key "name". 62 // 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 63 // Returns false if for a valid message/placeholder name there is no matching
60 // replacement. 64 // replacement.
61 // Public for easier unittesting. 65 // Public for easier unittesting.
62 static bool ReplaceVariables(const SubstitutionMap& variables, 66 static bool ReplaceVariables(const SubstitutionMap& variables,
63 const std::string& var_begin, 67 const std::string& var_begin,
64 const std::string& var_end, 68 const std::string& var_end,
65 std::string* message, 69 std::string* message,
66 std::string* error); 70 std::string* error);
67 71
68 // Allow only ascii 0-9, a-z, A-Z, and _ in the variable name. 72 // 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. 73 // Returns false if the input is empty or if it has illegal characters.
70 // Public for easier unittesting. 74 // Public for easier unittesting.
71 template<typename str> 75 template<typename str>
72 static bool IsValidName(const str& name); 76 static bool IsValidName(const str& name);
73 77
78 // Getter for dictionary_.
79 const SubstitutionMap* dictionary() const { return &dictionary_; }
80
74 private: 81 private:
75 // Use Create to create ExtensionMessageBundle instance. 82 // Use Create to create ExtensionMessageBundle instance.
76 ExtensionMessageBundle(); 83 ExtensionMessageBundle();
77 84
78 // Initializes the instance from the contents of two catalogs. If a key is not 85 // 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 86 // present in current_locale_catalog, the value from default_local_catalog is
80 // used instead. 87 // used instead.
81 // Returns false on error. 88 // Returns false on error.
82 bool Init(const DictionaryValue& default_locale_catalog, 89 bool Init(const DictionaryValue& default_locale_catalog,
83 const DictionaryValue& current_locale_catalog, 90 const DictionaryValue& current_locale_catalog,
(...skipping 16 matching lines...) Expand all
100 // For a given message, replaces all placeholders with their actual value. 107 // For a given message, replaces all placeholders with their actual value.
101 // Returns false if replacement failed (see ReplaceVariables). 108 // Returns false if replacement failed (see ReplaceVariables).
102 bool ReplacePlaceholders(const SubstitutionMap& placeholders, 109 bool ReplacePlaceholders(const SubstitutionMap& placeholders,
103 std::string* message, 110 std::string* message,
104 std::string* error) const; 111 std::string* error) const;
105 112
106 // Holds all messages for application locale. 113 // Holds all messages for application locale.
107 SubstitutionMap dictionary_; 114 SubstitutionMap dictionary_;
108 }; 115 };
109 116
110 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ 117 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698