| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_l10n_util.h" |
| 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "app/l10n_util.h" |
| 12 #include "base/file_util.h" |
| 13 #include "base/string_util.h" |
| 14 #include "base/values.h" |
| 15 #include "chrome/common/extensions/extension.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" |
| 17 |
| 18 namespace keys = extension_manifest_keys; |
| 19 |
| 20 namespace extension_l10n_util { |
| 21 |
| 22 bool ValidateDefaultLocale(const Extension* extension) { |
| 23 std::string default_locale = extension->default_locale(); |
| 24 |
| 25 if (extension->supported_locales().find(default_locale) != |
| 26 extension->supported_locales().end()) { |
| 27 return true; |
| 28 } else { |
| 29 return false; |
| 30 } |
| 31 } |
| 32 |
| 33 |
| 34 bool AddLocale(const std::set<std::string>& chrome_locales, |
| 35 const FilePath& locale_folder, |
| 36 Extension* extension, |
| 37 std::string* locale_name, |
| 38 std::string* error) { |
| 39 // Normalize underscores to hyphens because that's what our locale files use. |
| 40 std::replace(locale_name->begin(), locale_name->end(), '_', '-'); |
| 41 if (chrome_locales.find(*locale_name) == chrome_locales.end()) { |
| 42 // Fail if there is an extension locale that's not in the Chrome list. |
| 43 *error = StringPrintf("Supplied locale %s is not supported.", |
| 44 locale_name->c_str()); |
| 45 return false; |
| 46 } |
| 47 // Check if messages file is actually present (but don't check content). |
| 48 if (file_util::PathExists( |
| 49 locale_folder.AppendASCII(Extension::kMessagesFilename))) { |
| 50 extension->AddSupportedLocale(*locale_name); |
| 51 } else { |
| 52 *error = StringPrintf("Catalog file is missing for locale %s.", |
| 53 locale_name->c_str()); |
| 54 return false; |
| 55 } |
| 56 |
| 57 return true; |
| 58 } |
| 59 |
| 60 bool AddValidLocales(const FilePath& locale_path, |
| 61 Extension* extension, |
| 62 std::string* error) { |
| 63 // Get available chrome locales as a set. |
| 64 const std::vector<std::string>& available_locales = |
| 65 l10n_util::GetAvailableLocales(); |
| 66 static std::set<std::string> chrome_locales(available_locales.begin(), |
| 67 available_locales.end()); |
| 68 // Enumerate all supplied locales in the extension. |
| 69 file_util::FileEnumerator locales(locale_path, |
| 70 false, |
| 71 file_util::FileEnumerator::DIRECTORIES); |
| 72 FilePath locale_folder; |
| 73 while (!(locale_folder = locales.Next()).empty()) { |
| 74 std::string locale_name = |
| 75 WideToASCII(locale_folder.BaseName().ToWStringHack()); |
| 76 if (!AddLocale(chrome_locales, locale_folder, |
| 77 extension, &locale_name, error)) { |
| 78 return false; |
| 79 } |
| 80 } |
| 81 |
| 82 if (extension->supported_locales().empty()) { |
| 83 *error = extension_manifest_errors::kLocalesNoValidLocaleNamesListed; |
| 84 return false; |
| 85 } |
| 86 |
| 87 return true; |
| 88 } |
| 89 |
| 90 } // namespace extension_l10n_util |
| OLD | NEW |