extension_l10n_util.h revision 8ae428e0fb7feea16d79853f29447469a93bedff
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 15class DictionaryValue; 16class Extension; 17class ExtensionMessageBundle; 18class FilePath; 19class GURL; 20class ResourceDispatcherHostRequestInfo; 21struct ExtensionInfo; 22 23namespace 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. 28void 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. 32std::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. 37bool ShouldRelocalizeManifest(const ExtensionInfo& info); 38 39// Localize extension name, description, browser_action and other fields 40// in the manifest. 41bool 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. 47bool LocalizeExtension(Extension* extension, 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). 56bool 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// Converts all - into _, to be consistent with ICU and file system names. 63std::string NormalizeLocale(const std::string& locale); 64 65// Returns normalized current locale, or default locale - en_US. 66std::string CurrentLocaleOrDefault(); 67 68// Produce a vector of parent locales for given locale. 69// It includes the current locale in the result. 70// sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. 71void GetParentLocales(const std::string& current_locale, 72 std::vector<std::string>* parent_locales); 73 74// Extends list of Chrome locales to them and their parents, so we can do 75// proper fallback. 76void GetAllLocales(std::set<std::string>* all_locales); 77 78// Adds valid locales to the extension. 79// 1. Do nothing if _locales directory is missing (not an error). 80// 2. Get list of Chrome locales. 81// 3. Enumerate all subdirectories of _locales directory. 82// 4. Intersect both lists, and add intersection to the extension. 83// Returns false if any of supplied locales don't match chrome list of locales. 84// Fills out error with offending locale name. 85bool GetValidLocales(const FilePath& locale_path, 86 std::set<std::string>* locales, 87 std::string* error); 88 89// Loads messages file for default locale, and application locales (application 90// locales doesn't have to exist). Application locale is current locale and its 91// parents. 92// Returns message bundle if it can load default locale messages file, and all 93// messages are valid, else returns NULL and sets error. 94ExtensionMessageBundle* LoadMessageCatalogs( 95 const FilePath& locale_path, 96 const std::string& default_locale, 97 const std::string& app_locale, 98 const std::set<std::string>& valid_locales, 99 std::string* error); 100 101// Returns true if directory has "." in the name (for .svn) or if it doesn't 102// belong to Chrome locales. 103// |locales_path| is extension_id/_locales 104// |locale_path| is extension_id/_locales/xx 105// |all_locales| is a set of all valid Chrome locales. 106bool ShouldSkipValidation(const FilePath& locales_path, 107 const FilePath& locale_path, 108 const std::set<std::string>& all_locales); 109 110} // namespace extension_l10n_util 111 112#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_L10N_UTIL_H_ 113