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 #include "chrome/common/extensions/extension_l10n_util.h" | 5 #include "chrome/common/extensions/extension_l10n_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 l10n_util::GetAvailableLocales(); | 230 l10n_util::GetAvailableLocales(); |
231 // Add all parents of the current locale to the available locales set. | 231 // Add all parents of the current locale to the available locales set. |
232 // I.e. for sr_Cyrl_RS we add sr_Cyrl_RS, sr_Cyrl and sr. | 232 // I.e. for sr_Cyrl_RS we add sr_Cyrl_RS, sr_Cyrl and sr. |
233 for (size_t i = 0; i < available_locales.size(); ++i) { | 233 for (size_t i = 0; i < available_locales.size(); ++i) { |
234 std::vector<std::string> result; | 234 std::vector<std::string> result; |
235 l10n_util::GetParentLocales(available_locales[i], &result); | 235 l10n_util::GetParentLocales(available_locales[i], &result); |
236 all_locales->insert(result.begin(), result.end()); | 236 all_locales->insert(result.begin(), result.end()); |
237 } | 237 } |
238 } | 238 } |
239 | 239 |
| 240 void GetAllFallbackLocales(const std::string& application_locale, |
| 241 const std::string& default_locale, |
| 242 std::vector<std::string>* all_fallback_locales) { |
| 243 DCHECK(all_fallback_locales); |
| 244 if (!application_locale.empty() && application_locale != default_locale) |
| 245 l10n_util::GetParentLocales(application_locale, all_fallback_locales); |
| 246 all_fallback_locales->push_back(default_locale); |
| 247 } |
| 248 |
240 bool GetValidLocales(const FilePath& locale_path, | 249 bool GetValidLocales(const FilePath& locale_path, |
241 std::set<std::string>* valid_locales, | 250 std::set<std::string>* valid_locales, |
242 std::string* error) { | 251 std::string* error) { |
243 std::set<std::string> chrome_locales; | 252 std::set<std::string> chrome_locales; |
244 GetAllLocales(&chrome_locales); | 253 GetAllLocales(&chrome_locales); |
245 | 254 |
246 // Enumerate all supplied locales in the extension. | 255 // Enumerate all supplied locales in the extension. |
247 file_util::FileEnumerator locales(locale_path, | 256 file_util::FileEnumerator locales(locale_path, |
248 false, | 257 false, |
249 file_util::FileEnumerator::DIRECTORIES); | 258 file_util::FileEnumerator::DIRECTORIES); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 300 |
292 return static_cast<DictionaryValue*>(dictionary); | 301 return static_cast<DictionaryValue*>(dictionary); |
293 } | 302 } |
294 | 303 |
295 extensions::MessageBundle* LoadMessageCatalogs( | 304 extensions::MessageBundle* LoadMessageCatalogs( |
296 const FilePath& locale_path, | 305 const FilePath& locale_path, |
297 const std::string& default_locale, | 306 const std::string& default_locale, |
298 const std::string& application_locale, | 307 const std::string& application_locale, |
299 const std::set<std::string>& valid_locales, | 308 const std::set<std::string>& valid_locales, |
300 std::string* error) { | 309 std::string* error) { |
301 // Order locales to load as current_locale, first_parent, ..., default_locale. | |
302 std::vector<std::string> all_fallback_locales; | 310 std::vector<std::string> all_fallback_locales; |
303 if (!application_locale.empty() && application_locale != default_locale) | 311 GetAllFallbackLocales(application_locale, default_locale, |
304 l10n_util::GetParentLocales(application_locale, &all_fallback_locales); | 312 &all_fallback_locales); |
305 all_fallback_locales.push_back(default_locale); | |
306 | 313 |
307 std::vector<linked_ptr<DictionaryValue> > catalogs; | 314 std::vector<linked_ptr<DictionaryValue> > catalogs; |
308 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { | 315 for (size_t i = 0; i < all_fallback_locales.size(); ++i) { |
309 // Skip all parent locales that are not supplied. | 316 // Skip all parent locales that are not supplied. |
310 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) | 317 if (valid_locales.find(all_fallback_locales[i]) == valid_locales.end()) |
311 continue; | 318 continue; |
312 linked_ptr<DictionaryValue> catalog( | 319 linked_ptr<DictionaryValue> catalog( |
313 LoadMessageFile(locale_path, all_fallback_locales[i], error)); | 320 LoadMessageFile(locale_path, all_fallback_locales[i], error)); |
314 if (!catalog.get()) { | 321 if (!catalog.get()) { |
315 // If locale is valid, but messages.json is corrupted or missing, return | 322 // If locale is valid, but messages.json is corrupted or missing, return |
(...skipping 25 matching lines...) Expand all Loading... |
341 if (std::find(subdir.begin(), subdir.end(), '.') != subdir.end()) | 348 if (std::find(subdir.begin(), subdir.end(), '.') != subdir.end()) |
342 return true; | 349 return true; |
343 | 350 |
344 if (all_locales.find(subdir) == all_locales.end()) | 351 if (all_locales.find(subdir) == all_locales.end()) |
345 return true; | 352 return true; |
346 | 353 |
347 return false; | 354 return false; |
348 } | 355 } |
349 | 356 |
350 } // namespace extension_l10n_util | 357 } // namespace extension_l10n_util |
OLD | NEW |