OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // This file declares extension specific l10n utils. | 5 // This file declares extension specific l10n utils. |
6 | 6 |
7 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ | 7 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ |
8 #define CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ | 8 #define CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ |
9 | 9 |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 class FilePath; | |
15 | |
16 namespace base { | 14 namespace base { |
17 class DictionaryValue; | 15 class DictionaryValue; |
| 16 class FilePath; |
18 } | 17 } |
19 | 18 |
20 namespace extensions { | 19 namespace extensions { |
21 struct ExtensionInfo; | 20 struct ExtensionInfo; |
22 class MessageBundle; | 21 class MessageBundle; |
23 } | 22 } |
24 | 23 |
25 namespace extension_l10n_util { | 24 namespace extension_l10n_util { |
26 | 25 |
27 // Set the locale for this process to a fixed value, rather than using the | 26 // Set the locale for this process to a fixed value, rather than using the |
(...skipping 11 matching lines...) Expand all Loading... |
39 bool ShouldRelocalizeManifest(const extensions::ExtensionInfo& info); | 38 bool ShouldRelocalizeManifest(const extensions::ExtensionInfo& info); |
40 | 39 |
41 // Localize extension name, description, browser_action and other fields | 40 // Localize extension name, description, browser_action and other fields |
42 // in the manifest. | 41 // in the manifest. |
43 bool LocalizeManifest(const extensions::MessageBundle& messages, | 42 bool LocalizeManifest(const extensions::MessageBundle& messages, |
44 base::DictionaryValue* manifest, | 43 base::DictionaryValue* manifest, |
45 std::string* error); | 44 std::string* error); |
46 | 45 |
47 // Load message catalogs, localize manifest and attach message bundle to the | 46 // Load message catalogs, localize manifest and attach message bundle to the |
48 // extension. | 47 // extension. |
49 bool LocalizeExtension(const FilePath& extension_path, | 48 bool LocalizeExtension(const base::FilePath& extension_path, |
50 base::DictionaryValue* manifest, | 49 base::DictionaryValue* manifest, |
51 std::string* error); | 50 std::string* error); |
52 | 51 |
53 // Adds locale_name to the extension if it's in chrome_locales, and | 52 // Adds locale_name to the extension if it's in chrome_locales, and |
54 // if messages file is present (we don't check content of messages file here). | 53 // if messages file is present (we don't check content of messages file here). |
55 // Returns false if locale_name was not found in chrome_locales, and sets | 54 // Returns false if locale_name was not found in chrome_locales, and sets |
56 // error with locale_name. | 55 // error with locale_name. |
57 // If file name starts with . return true (helps testing extensions under svn). | 56 // If file name starts with . return true (helps testing extensions under svn). |
58 bool AddLocale(const std::set<std::string>& chrome_locales, | 57 bool AddLocale(const std::set<std::string>& chrome_locales, |
59 const FilePath& locale_folder, | 58 const base::FilePath& locale_folder, |
60 const std::string& locale_name, | 59 const std::string& locale_name, |
61 std::set<std::string>* valid_locales, | 60 std::set<std::string>* valid_locales, |
62 std::string* error); | 61 std::string* error); |
63 | 62 |
64 // Returns normalized current locale, or default locale - en_US. | 63 // Returns normalized current locale, or default locale - en_US. |
65 std::string CurrentLocaleOrDefault(); | 64 std::string CurrentLocaleOrDefault(); |
66 | 65 |
67 // Extends list of Chrome locales to them and their parents, so we can do | 66 // Extends list of Chrome locales to them and their parents, so we can do |
68 // proper fallback. | 67 // proper fallback. |
69 void GetAllLocales(std::set<std::string>* all_locales); | 68 void GetAllLocales(std::set<std::string>* all_locales); |
70 | 69 |
71 // Provides a vector of all fallback locales for message localization. | 70 // Provides a vector of all fallback locales for message localization. |
72 // The vector is ordered by priority of locale - |application_locale|, | 71 // The vector is ordered by priority of locale - |application_locale|, |
73 // first_parent, ..., |default_locale|. | 72 // first_parent, ..., |default_locale|. |
74 void GetAllFallbackLocales(const std::string& application_locale, | 73 void GetAllFallbackLocales(const std::string& application_locale, |
75 const std::string& default_locale, | 74 const std::string& default_locale, |
76 std::vector<std::string>* all_fallback_locales); | 75 std::vector<std::string>* all_fallback_locales); |
77 | 76 |
78 // Adds valid locales to the extension. | 77 // Adds valid locales to the extension. |
79 // 1. Do nothing if _locales directory is missing (not an error). | 78 // 1. Do nothing if _locales directory is missing (not an error). |
80 // 2. Get list of Chrome locales. | 79 // 2. Get list of Chrome locales. |
81 // 3. Enumerate all subdirectories of _locales directory. | 80 // 3. Enumerate all subdirectories of _locales directory. |
82 // 4. Intersect both lists, and add intersection to the extension. | 81 // 4. Intersect both lists, and add intersection to the extension. |
83 // Returns false if any of supplied locales don't match chrome list of locales. | 82 // Returns false if any of supplied locales don't match chrome list of locales. |
84 // Fills out error with offending locale name. | 83 // Fills out error with offending locale name. |
85 bool GetValidLocales(const FilePath& locale_path, | 84 bool GetValidLocales(const base::FilePath& locale_path, |
86 std::set<std::string>* locales, | 85 std::set<std::string>* locales, |
87 std::string* error); | 86 std::string* error); |
88 | 87 |
89 // Loads messages file for default locale, and application locales (application | 88 // Loads messages file for default locale, and application locales (application |
90 // locales doesn't have to exist). Application locale is current locale and its | 89 // locales doesn't have to exist). Application locale is current locale and its |
91 // parents. | 90 // parents. |
92 // Returns message bundle if it can load default locale messages file, and all | 91 // Returns message bundle if it can load default locale messages file, and all |
93 // messages are valid, else returns NULL and sets error. | 92 // messages are valid, else returns NULL and sets error. |
94 extensions::MessageBundle* LoadMessageCatalogs( | 93 extensions::MessageBundle* LoadMessageCatalogs( |
95 const FilePath& locale_path, | 94 const base::FilePath& locale_path, |
96 const std::string& default_locale, | 95 const std::string& default_locale, |
97 const std::string& app_locale, | 96 const std::string& app_locale, |
98 const std::set<std::string>& valid_locales, | 97 const std::set<std::string>& valid_locales, |
99 std::string* error); | 98 std::string* error); |
100 | 99 |
101 // Returns true if directory has "." in the name (for .svn) or if it doesn't | 100 // Returns true if directory has "." in the name (for .svn) or if it doesn't |
102 // belong to Chrome locales. | 101 // belong to Chrome locales. |
103 // |locales_path| is extension_id/_locales | 102 // |locales_path| is extension_id/_locales |
104 // |locale_path| is extension_id/_locales/xx | 103 // |locale_path| is extension_id/_locales/xx |
105 // |all_locales| is a set of all valid Chrome locales. | 104 // |all_locales| is a set of all valid Chrome locales. |
106 bool ShouldSkipValidation(const FilePath& locales_path, | 105 bool ShouldSkipValidation(const base::FilePath& locales_path, |
107 const FilePath& locale_path, | 106 const base::FilePath& locale_path, |
108 const std::set<std::string>& all_locales); | 107 const std::set<std::string>& all_locales); |
109 | 108 |
110 // Sets the process locale for the duration of the current scope, then reverts | 109 // Sets the process locale for the duration of the current scope, then reverts |
111 // back to whatever the current locale was before constructing this. | 110 // back to whatever the current locale was before constructing this. |
112 // For testing purposed only! | 111 // For testing purposed only! |
113 class ScopedLocaleForTest { | 112 class ScopedLocaleForTest { |
114 public: | 113 public: |
115 // Only revert back to current locale at end of scope, don't set locale. | 114 // Only revert back to current locale at end of scope, don't set locale. |
116 ScopedLocaleForTest(); | 115 ScopedLocaleForTest(); |
117 | 116 |
118 // Set temporary locale for the current scope | 117 // Set temporary locale for the current scope |
119 explicit ScopedLocaleForTest(const std::string& locale); | 118 explicit ScopedLocaleForTest(const std::string& locale); |
120 | 119 |
121 ~ScopedLocaleForTest(); | 120 ~ScopedLocaleForTest(); |
122 | 121 |
123 private: | 122 private: |
124 std::string locale_; // The current locale at ctor time. | 123 std::string locale_; // The current locale at ctor time. |
125 }; | 124 }; |
126 | 125 |
127 | 126 |
128 } // namespace extension_l10n_util | 127 } // namespace extension_l10n_util |
129 | 128 |
130 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ | 129 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ |
OLD | NEW |