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