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 #include "chrome/common/extensions/extension_l10n_util.h" | 5 #include "chrome/common/extensions/extension_l10n_util.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 valid_locales->insert(locale_name); | 160 valid_locales->insert(locale_name); |
161 } else { | 161 } else { |
162 *error = base::StringPrintf("Catalog file is missing for locale %s.", | 162 *error = base::StringPrintf("Catalog file is missing for locale %s.", |
163 locale_name.c_str()); | 163 locale_name.c_str()); |
164 return false; | 164 return false; |
165 } | 165 } |
166 | 166 |
167 return true; | 167 return true; |
168 } | 168 } |
169 | 169 |
170 std::string NormalizeLocale(const std::string& locale) { | |
171 std::string normalized_locale(locale); | |
172 std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); | |
173 | |
174 return normalized_locale; | |
175 } | |
176 | |
177 std::string CurrentLocaleOrDefault() { | 170 std::string CurrentLocaleOrDefault() { |
178 std::string current_locale = NormalizeLocale(*GetProcessLocale()); | 171 std::string current_locale = l10n_util::NormalizeLocale(*GetProcessLocale()); |
179 if (current_locale.empty()) | 172 if (current_locale.empty()) |
180 current_locale = "en"; | 173 current_locale = "en"; |
181 | 174 |
182 return current_locale; | 175 return current_locale; |
183 } | 176 } |
184 | 177 |
185 void GetParentLocales(const std::string& current_locale, | |
186 std::vector<std::string>* parent_locales) { | |
187 std::string locale(NormalizeLocale(current_locale)); | |
188 | |
189 const int kNameCapacity = 256; | |
190 char parent[kNameCapacity]; | |
191 base::strlcpy(parent, locale.c_str(), kNameCapacity); | |
192 parent_locales->push_back(parent); | |
193 UErrorCode err = U_ZERO_ERROR; | |
194 while (uloc_getParent(parent, parent, kNameCapacity, &err) > 0) { | |
195 if (U_FAILURE(err)) | |
196 break; | |
197 parent_locales->push_back(parent); | |
198 } | |
199 } | |
200 | |
201 void GetAllLocales(std::set<std::string>* all_locales) { | 178 void GetAllLocales(std::set<std::string>* all_locales) { |
202 const std::vector<std::string>& available_locales = | 179 const std::vector<std::string>& available_locales = |
203 l10n_util::GetAvailableLocales(); | 180 l10n_util::GetAvailableLocales(); |
204 // Add all parents of the current locale to the available locales set. | 181 // Add all parents of the current locale to the available locales set. |
205 // I.e. for sr_Cyrl_RS we add sr_Cyrl_RS, sr_Cyrl and sr. | 182 // I.e. for sr_Cyrl_RS we add sr_Cyrl_RS, sr_Cyrl and sr. |
206 for (size_t i = 0; i < available_locales.size(); ++i) { | 183 for (size_t i = 0; i < available_locales.size(); ++i) { |
207 std::vector<std::string> result; | 184 std::vector<std::string> result; |
208 GetParentLocales(available_locales[i], &result); | 185 l10n_util::GetParentLocales(available_locales[i], &result); |
209 all_locales->insert(result.begin(), result.end()); | 186 all_locales->insert(result.begin(), result.end()); |
210 } | 187 } |
211 } | 188 } |
212 | 189 |
213 bool GetValidLocales(const FilePath& locale_path, | 190 bool GetValidLocales(const FilePath& locale_path, |
214 std::set<std::string>* valid_locales, | 191 std::set<std::string>* valid_locales, |
215 std::string* error) { | 192 std::string* error) { |
216 static std::set<std::string> chrome_locales; | 193 static std::set<std::string> chrome_locales; |
217 GetAllLocales(&chrome_locales); | 194 GetAllLocales(&chrome_locales); |
218 | 195 |
(...skipping 14 matching lines...) Expand all Loading... |
233 } | 210 } |
234 } | 211 } |
235 | 212 |
236 if (valid_locales->empty()) { | 213 if (valid_locales->empty()) { |
237 *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed; | 214 *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed; |
238 return false; | 215 return false; |
239 } | 216 } |
240 | 217 |
241 return true; | 218 return true; |
242 } | 219 } |
| 220 |
243 // Loads contents of the messages file for given locale. If file is not found, | 221 // Loads contents of the messages file for given locale. If file is not found, |
244 // or there was parsing error we return NULL and set |error|. | 222 // or there was parsing error we return NULL and set |error|. |
245 // Caller owns the returned object. | 223 // Caller owns the returned object. |
246 static DictionaryValue* LoadMessageFile(const FilePath& locale_path, | 224 static DictionaryValue* LoadMessageFile(const FilePath& locale_path, |
247 const std::string& locale, | 225 const std::string& locale, |
248 std::string* error) { | 226 std::string* error) { |
249 std::string extension_locale = locale; | 227 std::string extension_locale = locale; |
250 FilePath file = locale_path.AppendASCII(extension_locale) | 228 FilePath file = locale_path.AppendASCII(extension_locale) |
251 .Append(Extension::kMessagesFilename); | 229 .Append(Extension::kMessagesFilename); |
252 JSONFileValueSerializer messages_serializer(file); | 230 JSONFileValueSerializer messages_serializer(file); |
(...skipping 10 matching lines...) Expand all Loading... |
263 | 241 |
264 ExtensionMessageBundle* LoadMessageCatalogs( | 242 ExtensionMessageBundle* LoadMessageCatalogs( |
265 const FilePath& locale_path, | 243 const FilePath& locale_path, |
266 const std::string& default_locale, | 244 const std::string& default_locale, |
267 const std::string& application_locale, | 245 const std::string& application_locale, |
268 const std::set<std::string>& valid_locales, | 246 const std::set<std::string>& valid_locales, |
269 std::string* error) { | 247 std::string* error) { |
270 // Order locales to load as current_locale, first_parent, ..., default_locale. | 248 // Order locales to load as current_locale, first_parent, ..., default_locale. |
271 std::vector<std::string> all_fallback_locales; | 249 std::vector<std::string> all_fallback_locales; |
272 if (!application_locale.empty() && application_locale != default_locale) | 250 if (!application_locale.empty() && application_locale != default_locale) |
273 GetParentLocales(application_locale, &all_fallback_locales); | 251 l10n_util::GetParentLocales(application_locale, &all_fallback_locales); |
274 all_fallback_locales.push_back(default_locale); | 252 all_fallback_locales.push_back(default_locale); |
275 | 253 |
276 std::vector<linked_ptr<DictionaryValue> > catalogs; | 254 std::vector<linked_ptr<DictionaryValue> > catalogs; |
277 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { | 255 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { |
278 // Skip all parent locales that are not supplied. | 256 // Skip all parent locales that are not supplied. |
279 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) | 257 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) |
280 continue; | 258 continue; |
281 linked_ptr<DictionaryValue> catalog( | 259 linked_ptr<DictionaryValue> catalog( |
282 LoadMessageFile(locale_path, all_fallback_locales[i], error)); | 260 LoadMessageFile(locale_path, all_fallback_locales[i], error)); |
283 if (!catalog.get()) { | 261 if (!catalog.get()) { |
(...skipping 21 matching lines...) Expand all Loading... |
305 if (std::find(subdir.begin(), subdir.end(), L'.') != subdir.end()) | 283 if (std::find(subdir.begin(), subdir.end(), L'.') != subdir.end()) |
306 return true; | 284 return true; |
307 | 285 |
308 if (all_locales.find(WideToASCII(subdir)) == all_locales.end()) | 286 if (all_locales.find(WideToASCII(subdir)) == all_locales.end()) |
309 return true; | 287 return true; |
310 | 288 |
311 return false; | 289 return false; |
312 } | 290 } |
313 | 291 |
314 } // namespace extension_l10n_util | 292 } // namespace extension_l10n_util |
OLD | NEW |