Bug 976574 - Implement 'Translation Preferences' option in infobar, r=felipe.

--HG--
extra : rebase_source : 240d73d8d852a1868bb96e39d61fc7a80a60621b
This commit is contained in:
Florian Quèze 2014-05-13 09:57:08 +02:00
parent 421bcbdb6e
commit d22d117649
15 changed files with 540 additions and 10 deletions

View File

@ -1563,6 +1563,7 @@ pref("browser.cache.auto_delete_cache_version", 1);
pref("browser.cache.frecency_experiment", 0);
pref("browser.translation.detectLanguage", false);
pref("browser.translation.neverForLanguages", "");
// Telemetry experiments settings.
pref("experiments.enabled", true);

View File

@ -170,5 +170,16 @@ var gContentPane = {
{
document.documentElement.openSubDialog("chrome://browser/content/preferences/languages.xul",
"", null);
},
/**
* Displays the translation exceptions dialog where specific site and language
* translation preferences can be set.
*/
showTranslationExceptions: function ()
{
document.documentElement.openWindow("Browser:TranslationExceptions",
"chrome://browser/content/preferences/translation.xul",
"", null);
}
};

View File

@ -30,6 +30,11 @@
name="font.language.group"
type="wstring"
onchange="gContentPane._rebuildFonts();"/>
<!-- LANGUAGES -->
<preference id="browser.translation.detectLanguage"
name="browser.translation.detectLanguage"
type="bool"/>
</preferences>
<script type="application/javascript" src="chrome://mozapps/content/preferences/fontbuilder.js"/>
@ -125,13 +130,31 @@
<groupbox id="languagesGroup">
<caption label="&languages.label;"/>
<hbox id="languagesBox" align="center">
<description flex="1" control="chooseLanguage">&chooseLanguage.label;</description>
<button id="chooseLanguage"
label="&chooseButton.label;"
accesskey="&chooseButton.accesskey;"
oncommand="gContentPane.showLanguages();"/>
</hbox>
<grid id="languagesGrid">
<columns>
<column flex="1"/>
<column/>
</columns>
<rows id="languagesRows">
<row id="preferredLanguageRow">
<label flex="1" control="chooseLanguage">&chooseLanguage.label;</label>
<button id="chooseLanguage"
label="&chooseButton.label;"
accesskey="&chooseButton.accesskey;"
oncommand="gContentPane.showLanguages();"/>
</row>
<row hidden="true">
<checkbox id="translate" preference="browser.translation.detectLanguage" flex="1"
label="&translateWebPages.label;" accesskey="&translateWebPages.accesskey;"
onsyncfrompreference="return gContentPane.updateButtons('translateButton',
'browser.translation.detectLanguage');"/>
<button id="translateButton" label="&translateExceptions.label;"
oncommand="gContentPane.showTranslationExceptions();"
accesskey="&translateExceptions.accesskey;"/>
</row>
</rows>
</grid>
</groupbox>
</prefpane>

View File

@ -169,5 +169,15 @@ var gContentPane = {
{
openDialog("chrome://browser/content/preferences/languages.xul",
"Browser:LanguagePreferences", null);
},
/**
* Displays the translation exceptions dialog where specific site and language
* translation preferences can be set.
*/
showTranslationExceptions: function ()
{
openDialog("chrome://browser/content/preferences/translation.xul",
"Browser:TranslationExceptions", null);
}
};

View File

@ -14,6 +14,11 @@
name="font.language.group"
type="wstring"
onchange="gContentPane._rebuildFonts();"/>
<!-- Languages -->
<preference id="browser.translation.detectLanguage"
name="browser.translation.detectLanguage"
type="bool"/>
</preferences>
<script type="application/javascript"
@ -126,4 +131,14 @@
accesskey="&chooseButton.accesskey;"
oncommand="gContentPane.showLanguages();"/>
</hbox>
<hbox id="translationBox" hidden="true">
<checkbox id="translate" preference="browser.translation.detectLanguage" flex="1"
label="&translateWebPages.label;" accesskey="&translateWebPages.accesskey;"
onsyncfrompreference="return gContentPane.updateButtons('translateButton',
'browser.translation.detectLanguage');"/>
<button id="translateButton" label="&translateExceptions.label;"
oncommand="gContentPane.showTranslationExceptions();"
accesskey="&translateExceptions.accesskey;"/>
</hbox>
</groupbox>

View File

@ -45,3 +45,5 @@ browser.jar:
#endif
* content/browser/preferences/tabs.xul
* content/browser/preferences/tabs.js
* content/browser/preferences/translation.xul
content/browser/preferences/translation.js

View File

@ -0,0 +1,227 @@
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyGetter(this, "gLangBundle", () =>
Services.strings.createBundle("chrome://global/locale/languageNames.properties"));
const kPermissionType = "translate";
const kLanguagesPref = "browser.translation.neverForLanguages";
function Tree(aId, aData)
{
this._data = aData;
this._tree = document.getElementById(aId);
this._tree.treeBoxObject.view = this;
}
Tree.prototype = {
get boxObject() this._tree.treeBoxObject,
get isEmpty() !this._data.length,
get hasSelection() this.selection.count > 0,
getSelectedItems: function() {
let result = [];
let rc = this.selection.getRangeCount();
for (let i = 0; i < rc; ++i) {
let min = {}, max = {};
this.selection.getRangeAt(i, min, max);
for (let j = min.value; j <= max.value; ++j)
result.push(this._data[j]);
}
return result;
},
// nsITreeView implementation
get rowCount() this._data.length,
getCellText: function (aRow, aColumn) this._data[aRow],
isSeparator: function(aIndex) false,
isSorted: function() false,
isContainer: function(aIndex) false,
setTree: function(aTree) {},
getImageSrc: function(aRow, aColumn) {},
getProgressMode: function(aRow, aColumn) {},
getCellValue: function(aRow, aColumn) {},
cycleHeader: function(column) {},
getRowProperties: function(row) "",
getColumnProperties: function(column) "",
getCellProperties: function(row, column) "",
QueryInterface: XPCOMUtils.generateQI([Ci.nsITreeView])
};
function Lang(aCode)
{
this.langCode = aCode;
this._label = gLangBundle.GetStringFromName(aCode);
}
Lang.prototype = {
toString: function() this._label
}
let gTranslationExceptions = {
onLoad: function() {
if (this._siteTree) {
// Re-using an open dialog, clear the old observers.
this.uninit();
}
// Load site permissions into an array.
this._sites = [];
let enumerator = Services.perms.enumerator;
while (enumerator.hasMoreElements()) {
let perm = enumerator.getNext().QueryInterface(Ci.nsIPermission);
if (perm.type == kPermissionType &&
perm.capability == Services.perms.DENY_ACTION) {
this._sites.push(perm.host);
}
}
Services.obs.addObserver(this, "perm-changed", false);
this._sites.sort();
this._siteTree = new Tree("sitesTree", this._sites);
this.onSiteSelected();
this._langs = this.getLanguageExceptions();
Services.prefs.addObserver(kLanguagesPref, this, false);
this._langTree = new Tree("languagesTree", this._langs);
this.onLanguageSelected();
},
// Get the list of languages we don't translate as an array.
getLanguageExceptions: function() {
let langs = Services.prefs.getCharPref(kLanguagesPref);
if (!langs)
return [];
let result = langs.split(",").map(code => new Lang(code));
result.sort();
return result;
},
observe: function(aSubject, aTopic, aData) {
if (aTopic == "perm-changed") {
if (aData == "cleared") {
if (!this._sites.length)
return;
let removed = this._sites.splice(0, this._sites.length);
this._siteTree.boxObject.rowCountChanged(0, - removed.length);
}
else {
let perm = aSubject.QueryInterface(Ci.nsIPermission);
if (perm.type != kPermissionType)
return;
if (aData == "added") {
if (perm.capability != Services.perms.DENY_ACTION)
return;
this._sites.push(perm.host);
this._sites.sort();
let boxObject = this._siteTree.boxObject;
boxObject.rowCountChanged(0, 1);
boxObject.invalidate();
}
else if (aData == "deleted") {
let index = this._sites.indexOf(perm.host);
if (index == -1)
return;
this._sites.splice(index, 1);
this._siteTree.boxObject.rowCountChanged(index, -1);
this.onSiteSelected();
return;
}
}
this.onSiteSelected();
}
else if (aTopic == "nsPref:changed") {
this._langs = this.getLanguageExceptions();
let change = this._langs.length - this._langTree.rowCount;
this._langTree._data = this._langs;
let boxObject = this._langTree.boxObject;
if (change)
boxObject.rowCountChanged(0, change);
boxObject.invalidate();
this.onLanguageSelected();
}
},
_handleButtonDisabling: function(aTree, aIdPart) {
let empty = aTree.isEmpty;
document.getElementById("removeAll" + aIdPart + "s").disabled = empty;
document.getElementById("remove" + aIdPart).disabled =
empty || !aTree.hasSelection;
},
onLanguageSelected: function() {
this._handleButtonDisabling(this._langTree, "Language");
},
onSiteSelected: function() {
this._handleButtonDisabling(this._siteTree, "Site");
},
onLanguageDeleted: function() {
let langs = Services.prefs.getCharPref(kLanguagesPref);
if (!langs)
return;
let removed = this._langTree.getSelectedItems().map(l => l.langCode);
langs = langs.split(",").filter(l => removed.indexOf(l) == -1);
Services.prefs.setCharPref(kLanguagesPref, langs.join(","));
},
onAllLanguagesDeleted: function() {
Services.prefs.setCharPref(kLanguagesPref, "");
},
onSiteDeleted: function() {
let removedSites = this._siteTree.getSelectedItems();
for (let host of removedSites)
Services.perms.remove(host, kPermissionType);
},
onAllSitesDeleted: function() {
if (this._siteTree.isEmpty)
return;
let removedSites = this._sites.splice(0, this._sites.length);
this._siteTree.boxObject.rowCountChanged(0, -removedSites.length);
for (let host of removedSites)
Services.perms.remove(host, kPermissionType);
this.onSiteSelected();
},
onSiteKeyPress: function(aEvent) {
if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE)
this.onSiteDeleted();
},
onLanguageKeyPress: function() {
if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE)
this.onLanguageDeleted();
},
onWindowKeyPress: function(aEvent) {
if (aEvent.keyCode == KeyEvent.DOM_VK_ESCAPE)
window.close();
},
uninit: function() {
Services.obs.removeObserver(this, "perm-changed");
Services.prefs.removeObserver(kLanguagesPref, this);
}
};

View File

@ -0,0 +1,90 @@
<?xml version="1.0"?>
# -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://browser/skin/preferences/preferences.css" type="text/css"?>
<!DOCTYPE dialog SYSTEM "chrome://browser/locale/preferences/translation.dtd">
<window id="TranslationDialog" class="windowDialog"
windowtype="Browser:TranslationExceptions"
title="&window.title;"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
style="width: &window.width;;"
onload="gTranslationExceptions.onLoad();"
onunload="gTranslationExceptions.uninit();"
persist="screenX screenY width height"
onkeypress="gTranslationExceptions.onWindowKeyPress(event);">
<script src="chrome://browser/content/preferences/translation.js"/>
<stringbundle id="bundlePreferences"
src="chrome://browser/locale/preferences/preferences.properties"/>
<keyset>
<key key="&windowClose.key;" modifiers="accel" oncommand="window.close();"/>
</keyset>
<vbox class="contentPane" flex="1">
<label id="languagesLabel" control="permissionsTree">&noTranslationForLanguages.label;</label>
<separator class="thin"/>
<tree id="languagesTree" flex="1" style="height: 12em;"
hidecolumnpicker="true"
onkeypress="gTranslationExceptions.onLanguageKeyPress(event)"
onselect="gTranslationExceptions.onLanguageSelected();">
<treecols>
<treecol id="languageCol" label="&treehead.languageName.label;" flex="1"/>
</treecols>
<treechildren/>
</tree>
</vbox>
<hbox align="end">
<hbox class="actionButtons" flex="1">
<button id="removeLanguage" disabled="true"
accesskey="&removeLanguage.accesskey;"
icon="remove" label="&removeLanguage.label;"
oncommand="gTranslationExceptions.onLanguageDeleted();"/>
<button id="removeAllLanguages"
icon="clear" label="&removeAllLanguages.label;"
accesskey="&removeAllLanguages.accesskey;"
oncommand="gTranslationExceptions.onAllLanguagesDeleted();"/>
<spacer flex="1"/>
</hbox>
</hbox>
<separator/>
<vbox class="contentPane" flex="1">
<label id="languagesLabel" control="permissionsTree">&noTranslationForSites.label;</label>
<separator class="thin"/>
<tree id="sitesTree" flex="1" style="height: 12em;"
hidecolumnpicker="true"
onkeypress="gTranslationExceptions.onSiteKeyPress(event)"
onselect="gTranslationExceptions.onSiteSelected();">
<treecols>
<treecol id="siteCol" label="&treehead.siteName.label;" flex="1"/>
</treecols>
<treechildren/>
</tree>
</vbox>
<hbox align="end">
<hbox class="actionButtons" flex="1">
<button id="removeSite" disabled="true"
accesskey="&removeSite.accesskey;"
icon="remove" label="&removeSite.label;"
oncommand="gTranslationExceptions.onSiteDeleted();"/>
<button id="removeAllSites"
icon="clear" label="&removeAllSites.label;"
accesskey="&removeAllSites.accesskey;"
oncommand="gTranslationExceptions.onAllSitesDeleted();"/>
<spacer flex="1"/>
#ifndef XP_MACOSX
<button oncommand="close();" icon="close"
label="&button.close.label;" accesskey="&button.close.accesskey;"/>
#endif
</hbox>
<resizer type="window" dir="bottomend"/>
</hbox>
</window>

View File

@ -64,8 +64,6 @@ TranslationUI.prototype = {
STATE_TRANSLATED: 2,
STATE_ERROR: 3,
get doc() this.browser.contentDocument,
translate: function(aFrom, aTo) {
this.state = this.STATE_TRANSLATING;
this.translatedFrom = aFrom;
@ -125,6 +123,18 @@ TranslationUI.prototype = {
return notif;
},
shouldShowInfoBar: function(aURI, aDetectedLanguage) {
// Check if we should never show the infobar for this language.
let neverForLangs =
Services.prefs.getCharPref("browser.translation.neverForLanguages");
if (neverForLangs.split(",").indexOf(aDetectedLanguage) != -1)
return false;
// or if we should never show the infobar for this domain.
let perms = Services.perms;
return perms.testExactPermission(aURI, "translate") != perms.DENY_ACTION;
},
showTranslationUI: function(aDetectedLanguage) {
this.detectedLanguage = aDetectedLanguage;
@ -135,6 +145,10 @@ TranslationUI.prototype = {
this.originalShown = true;
this.showURLBarIcon();
if (!this.shouldShowInfoBar(this.browser.currentURI, aDetectedLanguage))
return null;
return this.showTranslationInfoBar();
}
};

View File

@ -74,7 +74,18 @@
<xul:spacer flex="1"/>
<xul:button type="menu" label="&translation.options.menu;">
<xul:menupopup/>
<xul:menupopup onpopupshowing="document.getBindingParent(this).optionsShowing();">
<xul:menuitem anonid="neverForLanguage"
oncommand="document.getBindingParent(this).neverForLanguage();"/>
<xul:menuitem anonid="neverForSite"
oncommand="document.getBindingParent(this).neverForSite();"
label="&translation.options.neverForSite.label;"
accesskey="&translation.options.neverForSite.accesskey;"/>
<xul:menuseparator/>
<xul:menuitem oncommand="openPreferences('paneContent');"
label="&translation.options.preferences.label;"
accesskey="&translation.options.preferences.accesskey;"/>
</xul:menupopup>
</xul:button>
</xul:hbox>
@ -181,6 +192,80 @@
</body>
</method>
<method name="optionsShowing">
<body>
<![CDATA[
// Get the source language name.
let lang;
if (this.state == this.translation.STATE_OFFER)
lang = this._getAnonElt("detectedLanguage").value;
else
lang = this._getAnonElt("fromLanguage").value;
let langBundle =
Cc["@mozilla.org/intl/stringbundle;1"]
.getService(Ci.nsIStringBundleService)
.createBundle("chrome://global/locale/languageNames.properties");
let langName = langBundle.GetStringFromName(lang);
// Set the label and accesskey on the menuitem.
let bundle =
Cc["@mozilla.org/intl/stringbundle;1"]
.getService(Ci.nsIStringBundleService)
.createBundle("chrome://browser/locale/translation.properties");
let item = this._getAnonElt("neverForLanguage");
const kStrId = "translation.options.neverForLanguage";
item.setAttribute("label",
bundle.formatStringFromName(kStrId + ".label",
[langName], 1));
item.setAttribute("accesskey",
bundle.GetStringFromName(kStrId + ".accesskey"));
item.langCode = lang;
// We may need to disable the menuitems if they have already been used.
// Check if translation is already disabled for this language:
let neverForLangs =
Services.prefs.getCharPref("browser.translation.neverForLanguages");
item.disabled = neverForLangs.split(",").indexOf(lang) != -1;
// Check if translation is disabled for the domain:
let uri = this.translation.browser.currentURI;
let perms = Services.perms;
item = this._getAnonElt("neverForSite");
item.disabled =
perms.testExactPermission(uri, "translate") == perms.DENY_ACTION;
]]>
</body>
</method>
<method name="neverForLanguage">
<body>
<![CDATA[
const kPrefName = "browser.translation.neverForLanguages";
let val = Services.prefs.getCharPref(kPrefName);
if (val)
val += ",";
val += this._getAnonElt("neverForLanguage").langCode;
Services.prefs.setCharPref(kPrefName, val);
this.close();
]]>
</body>
</method>
<method name="neverForSite">
<body>
<![CDATA[
let uri = this.translation.browser.currentURI;
let perms = Services.perms;
perms.add(uri, "translate", perms.DENY_ACTION);
this.close();
]]>
</body>
</method>
</implementation>
</binding>
</bindings>

View File

@ -27,3 +27,8 @@
<!ENTITY chooseLanguage.label "Choose your preferred language for displaying pages">
<!ENTITY chooseButton.label "Choose…">
<!ENTITY chooseButton.accesskey "o">
<!ENTITY translateWebPages.label "Translate web content">
<!ENTITY translateWebPages.accesskey "T">
<!ENTITY translateExceptions.label "Exceptions…">
<!ENTITY translateExceptions.accesskey "x">

View File

@ -0,0 +1,24 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<!ENTITY window.title "Exceptions - Translation">
<!ENTITY window.width "36em">
<!ENTITY windowClose.key "w">
<!ENTITY noTranslationForLanguages.label "Translation will not be offered for the following languages:">
<!ENTITY treehead.languageName.label "Languages">
<!ENTITY removeLanguage.label "Remove Language">
<!ENTITY removeLanguage.accesskey "R">
<!ENTITY removeAllLanguages.label "Remove All Languages">
<!ENTITY removeAllLanguages.accesskey "e">
<!ENTITY noTranslationForSites.label "Translation will not be offered for the following sites:">
<!ENTITY treehead.siteName.label "Sites">
<!ENTITY removeSite.label "Remove Site">
<!ENTITY removeSite.accesskey "S">
<!ENTITY removeAllSites.label "Remove All Sites">
<!ENTITY removeAllSites.accesskey "i">
<!ENTITY button.close.label "Close">
<!ENTITY button.close.accesskey "C">

View File

@ -45,3 +45,12 @@
<!ENTITY translation.tryAgain.button "Try Again">
<!ENTITY translation.options.menu "Options">
<!-- LOCALIZATION NOTE (translation.options.neverForSite.accesskey,
- translation.options.preferences.accesskey):
- The accesskey values used here should not clash with the value used for
- translation.options.neverForLanguage.accesskey in translation.properties
-->
<!ENTITY translation.options.neverForSite.label "Never translate this site">
<!ENTITY translation.options.neverForSite.accesskey "e">
<!ENTITY translation.options.preferences.label "Translation preferences">
<!ENTITY translation.options.preferences.accesskey "T">

View File

@ -0,0 +1,12 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# LOCALIZATION NOTE (translation.options.neverForLanguage.label):
# %S is a language name coming from the global/languageNames.properties file.
translation.options.neverForLanguage.label=Never translate %S
# LOCALIZATION NOTE (translation.options.neverForLanguage.accesskey):
# The accesskey value used here should not clash with the values used for
# translation.options.*.accesskey in translation.dtd
translation.options.neverForLanguage.accesskey=N

View File

@ -82,6 +82,7 @@
locale/browser/tabview.properties (%chrome/browser/tabview.properties)
locale/browser/taskbar.properties (%chrome/browser/taskbar.properties)
locale/browser/translation.dtd (%chrome/browser/translation.dtd)
locale/browser/translation.properties (%chrome/browser/translation.properties)
locale/browser/downloads/downloads.dtd (%chrome/browser/downloads/downloads.dtd)
locale/browser/downloads/downloads.properties (%chrome/browser/downloads/downloads.properties)
locale/browser/places/places.dtd (%chrome/browser/places/places.dtd)
@ -120,6 +121,7 @@
locale/browser/preferences/sync.dtd (%chrome/browser/preferences/sync.dtd)
#endif
locale/browser/preferences/tabs.dtd (%chrome/browser/preferences/tabs.dtd)
locale/browser/preferences/translation.dtd (%chrome/browser/preferences/translation.dtd)
#ifdef MOZ_SERVICES_SYNC
locale/browser/syncBrand.dtd (%chrome/browser/syncBrand.dtd)
locale/browser/syncSetup.dtd (%chrome/browser/syncSetup.dtd)