1 // Copyright (c) 2010 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 // This file declares extension specific l10n utils. 6 7 #ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ 8 #define CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ 9 #pragma once 10 11 #include <set> 12 #include <string> 13 #include <vector> 14 15 class DictionaryValue; 16 class Extension; 17 class ExtensionMessageBundle; 18 class FilePath; 19 class GURL; 20 class ResourceDispatcherHostRequestInfo; 21 struct ExtensionInfo; 22 23 namespace extension_l10n_util { 24 25 // Set the locale for this process to a fixed value, rather than using the 26 // normal file-based lookup mechanisms. This is used to set the locale inside 27 // the sandboxed utility process, where file reading is not allowed. 28 void SetProcessLocale(const std::string& locale); 29 30 // Returns default locale in form "en-US" or "sr" or empty string if 31 // "default_locale" section was not defined in the manifest.json file. 32 std::string GetDefaultLocaleFromManifest(const DictionaryValue& manifest, 33 std::string* error); 34 35 // Returns true iff the extension was localized, and the current locale 36 // doesn't match the locale written into info.extension_manifest. 37 bool ShouldRelocalizeManifest(const ExtensionInfo& info); 38 39 // Localize extension name, description, browser_action and other fields 40 // in the manifest. 41 bool LocalizeManifest(const ExtensionMessageBundle& messages, 42 DictionaryValue* manifest, 43 std::string* error); 44 45 // Load message catalogs, localize manifest and attach message bundle to the 46 // extension. 47 bool LocalizeExtension(const FilePath& extension_path, 48 DictionaryValue* manifest, 49 std::string* error); 50 51 // Adds locale_name to the extension if it's in chrome_locales, and 52 // if messages file is present (we don't check content of messages file here). 53 // Returns false if locale_name was not found in chrome_locales, and sets 54 // error with locale_name. 55 // If file name starts with . return true (helps testing extensions under svn). 56 bool AddLocale(const std::set<std::string>& chrome_locales, 57 const FilePath& locale_folder, 58 const std::string& locale_name, 59 std::set<std::string>* valid_locales, 60 std::string* error); 61 62 // Returns normalized current locale, or default locale - en_US. 63 std::string CurrentLocaleOrDefault(); 64 65 // Extends list of Chrome locales to them and their parents, so we can do 66 // proper fallback. 67 void GetAllLocales(std::set<std::string>* all_locales); 68 69 // Adds valid locales to the extension. 70 // 1. Do nothing if _locales directory is missing (not an error). 71 // 2. Get list of Chrome locales. 72 // 3. Enumerate all subdirectories of _locales directory. 73 // 4. Intersect both lists, and add intersection to the extension. 74 // Returns false if any of supplied locales don't match chrome list of locales. 75 // Fills out error with offending locale name. 76 bool GetValidLocales(const FilePath& locale_path, 77 std::set<std::string>* locales, 78 std::string* error); 79 80 // Loads messages file for default locale, and application locales (application 81 // locales doesn't have to exist). Application locale is current locale and its 82 // parents. 83 // Returns message bundle if it can load default locale messages file, and all 84 // messages are valid, else returns NULL and sets error. 85 ExtensionMessageBundle* LoadMessageCatalogs( 86 const FilePath& locale_path, 87 const std::string& default_locale, 88 const std::string& app_locale, 89 const std::set<std::string>& valid_locales, 90 std::string* error); 91 92 // Returns true if directory has "." in the name (for .svn) or if it doesn't 93 // belong to Chrome locales. 94 // |locales_path| is extension_id/_locales 95 // |locale_path| is extension_id/_locales/xx 96 // |all_locales| is a set of all valid Chrome locales. 97 bool ShouldSkipValidation(const FilePath& locales_path, 98 const FilePath& locale_path, 99 const std::set<std::string>& all_locales); 100 101 } // namespace extension_l10n_util 102 103 #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ 104