summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/extension_l10n_util.cc
diff options
context:
space:
mode:
authorcira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-16 21:11:06 +0000
committercira@chromium.org <cira@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-16 21:11:06 +0000
commitc6d474f8e04b08306349ea514d529b8f25ffd4b2 (patch)
tree756833e85392a8b487cf3ee3cefd0d81ab8c51fa /chrome/common/extensions/extension_l10n_util.cc
parent538a1ac2bc5ad7f171e15642edb4e8c1dd34348d (diff)
downloadchromium_src-c6d474f8e04b08306349ea514d529b8f25ffd4b2.zip
chromium_src-c6d474f8e04b08306349ea514d529b8f25ffd4b2.tar.gz
chromium_src-c6d474f8e04b08306349ea514d529b8f25ffd4b2.tar.bz2
Localizing manifest for installed or unpacked extensions.
1. Use _MSG_ format for manifest fields instead of hard-coded names. 2. Localize manifest during installation procedure -> save localized version to prefs. 3. Add current_locale field to manifest so we can detect when chrome locale changes and re-localize manifest. 4. Adds prefs method to MigratePrefs&Write to disk. 5. General refactor of extension_file_util wrt l10n code. 6. Don't localize themes (we can revert this by adding default_locale and current_locale keys to allowed keys for theme section). BUG=27360 Review URL: http://codereview.chromium.org/434015 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34749 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions/extension_l10n_util.cc')
-rw-r--r--chrome/common/extensions/extension_l10n_util.cc102
1 files changed, 101 insertions, 1 deletions
diff --git a/chrome/common/extensions/extension_l10n_util.cc b/chrome/common/extensions/extension_l10n_util.cc
index 4157083..9d0510e 100644
--- a/chrome/common/extensions/extension_l10n_util.cc
+++ b/chrome/common/extensions/extension_l10n_util.cc
@@ -13,6 +13,8 @@
#include "base/linked_ptr.h"
#include "base/string_util.h"
#include "base/values.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/extensions/extension_file_util.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_message_bundle.h"
@@ -43,6 +45,96 @@ std::string GetDefaultLocaleFromManifest(const DictionaryValue& manifest,
return default_locale;
}
+bool ShouldRelocalizeManifest(const ExtensionInfo& info) {
+ DictionaryValue* manifest = info.extension_manifest.get();
+ if (!manifest)
+ return false;
+
+ if (!manifest->HasKey(keys::kDefaultLocale))
+ return false;
+
+ std::string manifest_current_locale;
+ manifest->GetString(keys::kCurrentLocale, &manifest_current_locale);
+ return manifest_current_locale != CurrentLocaleOrDefault();
+}
+
+// Localizes manifest value for a given key.
+static bool LocalizeManifestValue(const std::wstring& key,
+ const ExtensionMessageBundle& messages,
+ DictionaryValue* manifest,
+ std::string* error) {
+ std::string result;
+ if (!manifest->GetString(key, &result))
+ return true;
+
+ if (!messages.ReplaceMessages(&result, error))
+ return false;
+
+ manifest->SetString(key, result);
+ return true;
+}
+
+bool LocalizeManifest(const ExtensionMessageBundle& messages,
+ DictionaryValue* manifest,
+ std::string* error) {
+ // Don't localize themes.
+ if (manifest->HasKey(keys::kTheme))
+ return true;
+
+ // Initialize name.
+ std::string result;
+ if (!manifest->GetString(keys::kName, &result)) {
+ *error = errors::kInvalidName;
+ return false;
+ }
+ if (!LocalizeManifestValue(keys::kName, messages, manifest, error)) {
+ return false;
+ }
+
+ // Initialize description.
+ if (!LocalizeManifestValue(keys::kDescription, messages, manifest, error))
+ return false;
+
+ // Initialize browser_action.default_title
+ std::wstring key(keys::kBrowserAction);
+ key.append(L".");
+ key.append(keys::kPageActionDefaultTitle);
+ if (!LocalizeManifestValue(key, messages, manifest, error))
+ return false;
+
+ // Initialize page_action.default_title
+ key.assign(keys::kPageAction);
+ key.append(L".");
+ key.append(keys::kPageActionDefaultTitle);
+ if (!LocalizeManifestValue(key, messages, manifest, error))
+ return false;
+
+ // Add current locale key to the manifest, so we can overwrite prefs
+ // with new manifest when chrome locale changes.
+ manifest->SetString(keys::kCurrentLocale, CurrentLocaleOrDefault());
+ return true;
+}
+
+bool LocalizeExtension(Extension* extension,
+ DictionaryValue* manifest,
+ std::string* error) {
+ ExtensionMessageBundle* message_bundle =
+ extension_file_util::LoadExtensionMessageBundle(extension->path(),
+ *manifest,
+ error);
+ if (!message_bundle && !error->empty())
+ return false;
+
+ // TODO(cira): remove ExtensionMessageBundle object from Extension class
+ // after we implement IPC that requests message bundles on demand.
+ extension->set_message_bundle(message_bundle);
+
+ if (message_bundle && !LocalizeManifest(*message_bundle, manifest, error))
+ return false;
+
+ return true;
+}
+
bool AddLocale(const std::set<std::string>& chrome_locales,
const FilePath& locale_folder,
const std::string& locale_name,
@@ -78,6 +170,15 @@ std::string NormalizeLocale(const std::string& locale) {
return normalized_locale;
}
+std::string CurrentLocaleOrDefault() {
+ std::string current_locale =
+ NormalizeLocale(g_browser_process->GetApplicationLocale());
+ if (current_locale.empty())
+ current_locale = "en";
+
+ return current_locale;
+}
+
void GetParentLocales(const std::string& current_locale,
std::vector<std::string>* parent_locales) {
std::string locale(NormalizeLocale(current_locale));
@@ -145,7 +246,6 @@ bool GetValidLocales(const FilePath& locale_path,
static DictionaryValue* LoadMessageFile(const FilePath& locale_path,
const std::string& locale,
std::string* error) {
-
std::string extension_locale = locale;
FilePath file = locale_path.AppendASCII(extension_locale)
.AppendASCII(Extension::kMessagesFilename);