| OLD | NEW |
| 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_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ | 5 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ |
| 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ | 6 #define CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> |
| 10 | 11 |
| 12 #include "base/linked_ptr.h" |
| 11 #include "base/values.h" | 13 #include "base/values.h" |
| 12 | 14 |
| 13 // Contains localized extension messages for one locale. Any messages that the | 15 // Contains localized extension messages for one locale. Any messages that the |
| 14 // locale does not provide are pulled from the default locale. | 16 // locale does not provide are pulled from the default locale. |
| 15 class ExtensionMessageBundle { | 17 class ExtensionMessageBundle { |
| 16 public: | 18 public: |
| 17 typedef std::map<std::string, std::string> SubstitutionMap; | 19 typedef std::map<std::string, std::string> SubstitutionMap; |
| 20 typedef std::vector<linked_ptr<DictionaryValue> > CatalogVector; |
| 18 | 21 |
| 19 // JSON keys of interest for messages file. | 22 // JSON keys of interest for messages file. |
| 20 static const wchar_t* kContentKey; | 23 static const wchar_t* kContentKey; |
| 21 static const wchar_t* kMessageKey; | 24 static const wchar_t* kMessageKey; |
| 22 static const wchar_t* kPlaceholdersKey; | 25 static const wchar_t* kPlaceholdersKey; |
| 23 | 26 |
| 24 // Begin/end markers for placeholders and messages | 27 // Begin/end markers for placeholders and messages |
| 25 static const char* kPlaceholderBegin; | 28 static const char* kPlaceholderBegin; |
| 26 static const char* kPlaceholderEnd; | 29 static const char* kPlaceholderEnd; |
| 27 static const char* kMessageBegin; | 30 static const char* kMessageBegin; |
| 28 static const char* kMessageEnd; | 31 static const char* kMessageEnd; |
| 29 | 32 |
| 30 // Extension name and description message names | 33 // Extension name and description message names |
| 31 static const char* kExtensionName; | 34 static const char* kExtensionName; |
| 32 static const char* kExtensionDescription; | 35 static const char* kExtensionDescription; |
| 33 | 36 |
| 34 // Creates ExtensionMessageBundle or returns NULL if there was an error. | 37 // Creates ExtensionMessageBundle or returns NULL if there was an error. |
| 35 static ExtensionMessageBundle* Create( | 38 // Expects locale_catalogs to be sorted from more specific to less specific, |
| 36 const DictionaryValue& default_locale_catalog, | 39 // with default catalog at the end. |
| 37 const DictionaryValue& current_locale_catalog, | 40 static ExtensionMessageBundle* Create(const CatalogVector& locale_catalogs, |
| 38 std::string* error); | 41 std::string* error); |
| 39 | 42 |
| 40 // Get message from the catalog with given key. | 43 // Get message from the catalog with given key. |
| 41 // Returned message has all of the internal placeholders resolved to their | 44 // Returned message has all of the internal placeholders resolved to their |
| 42 // value (content). | 45 // value (content). |
| 43 // Returns empty string if it can't find a message. | 46 // Returns empty string if it can't find a message. |
| 44 // We don't use simple GetMessage name, since there is a global | 47 // We don't use simple GetMessage name, since there is a global |
| 45 // #define GetMessage GetMessageW override in Chrome code. | 48 // #define GetMessage GetMessageW override in Chrome code. |
| 46 std::string GetL10nMessage(const std::string& name) const; | 49 std::string GetL10nMessage(const std::string& name) const; |
| 47 | 50 |
| 48 // Get message from the given catalog with given key. | 51 // Get message from the given catalog with given key. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 75 template<typename str> | 78 template<typename str> |
| 76 static bool IsValidName(const str& name); | 79 static bool IsValidName(const str& name); |
| 77 | 80 |
| 78 // Getter for dictionary_. | 81 // Getter for dictionary_. |
| 79 const SubstitutionMap* dictionary() const { return &dictionary_; } | 82 const SubstitutionMap* dictionary() const { return &dictionary_; } |
| 80 | 83 |
| 81 private: | 84 private: |
| 82 // Use Create to create ExtensionMessageBundle instance. | 85 // Use Create to create ExtensionMessageBundle instance. |
| 83 ExtensionMessageBundle(); | 86 ExtensionMessageBundle(); |
| 84 | 87 |
| 85 // Initializes the instance from the contents of two catalogs. If a key is not | 88 // Initializes the instance from the contents of vector of catalogs. |
| 86 // present in current_locale_catalog, the value from default_local_catalog is | 89 // If the key is not present in more specific catalog we fall back to next one |
| 87 // used instead. | 90 // (less specific). |
| 88 // Returns false on error. | 91 // Returns false on error. |
| 89 bool Init(const DictionaryValue& default_locale_catalog, | 92 bool Init(const CatalogVector& locale_catalogs, std::string* error); |
| 90 const DictionaryValue& current_locale_catalog, | |
| 91 std::string* error); | |
| 92 | 93 |
| 93 // Helper methods that navigate JSON tree and return simplified message. | 94 // Helper methods that navigate JSON tree and return simplified message. |
| 94 // They replace all $PLACEHOLDERS$ with their value, and return just key/value | 95 // They replace all $PLACEHOLDERS$ with their value, and return just key/value |
| 95 // of the message. | 96 // of the message. |
| 96 bool GetMessageValue(const std::wstring& wkey, | 97 bool GetMessageValue(const std::wstring& wkey, |
| 97 const DictionaryValue& catalog, | 98 const DictionaryValue& catalog, |
| 98 std::string* value, | 99 std::string* value, |
| 99 std::string* error) const; | 100 std::string* error) const; |
| 100 | 101 |
| 101 // Get all placeholders for a given message from JSON subtree. | 102 // Get all placeholders for a given message from JSON subtree. |
| 102 bool GetPlaceholders(const DictionaryValue& name_tree, | 103 bool GetPlaceholders(const DictionaryValue& name_tree, |
| 103 const std::string& name_key, | 104 const std::string& name_key, |
| 104 SubstitutionMap* placeholders, | 105 SubstitutionMap* placeholders, |
| 105 std::string* error) const; | 106 std::string* error) const; |
| 106 | 107 |
| 107 // For a given message, replaces all placeholders with their actual value. | 108 // For a given message, replaces all placeholders with their actual value. |
| 108 // Returns false if replacement failed (see ReplaceVariables). | 109 // Returns false if replacement failed (see ReplaceVariables). |
| 109 bool ReplacePlaceholders(const SubstitutionMap& placeholders, | 110 bool ReplacePlaceholders(const SubstitutionMap& placeholders, |
| 110 std::string* message, | 111 std::string* message, |
| 111 std::string* error) const; | 112 std::string* error) const; |
| 112 | 113 |
| 113 // Holds all messages for application locale. | 114 // Holds all messages for application locale. |
| 114 SubstitutionMap dictionary_; | 115 SubstitutionMap dictionary_; |
| 115 }; | 116 }; |
| 116 | 117 |
| 117 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ | 118 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_MESSAGE_BUNDLE_H_ |
| OLD | NEW |