browser/components/translation/Translation.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["Translation"];
michael@0 8
michael@0 9 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 12 Cu.import("resource://gre/modules/Services.jsm");
michael@0 13 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 14
michael@0 15 this.Translation = {
michael@0 16 supportedSourceLanguages: ["en", "zh", "ja", "es", "de", "fr", "ru", "ar", "ko", "pt"],
michael@0 17 supportedTargetLanguages: ["en", "pl", "tr", "vi"],
michael@0 18
michael@0 19 _defaultTargetLanguage: "",
michael@0 20 get defaultTargetLanguage() {
michael@0 21 if (!this._defaultTargetLanguage) {
michael@0 22 this._defaultTargetLanguage = Cc["@mozilla.org/chrome/chrome-registry;1"]
michael@0 23 .getService(Ci.nsIXULChromeRegistry)
michael@0 24 .getSelectedLocale("global")
michael@0 25 .split("-")[0];
michael@0 26 }
michael@0 27 return this._defaultTargetLanguage;
michael@0 28 },
michael@0 29
michael@0 30 languageDetected: function(aBrowser, aDetectedLanguage) {
michael@0 31 if (this.supportedSourceLanguages.indexOf(aDetectedLanguage) != -1 &&
michael@0 32 aDetectedLanguage != this.defaultTargetLanguage) {
michael@0 33 if (!aBrowser.translationUI)
michael@0 34 aBrowser.translationUI = new TranslationUI(aBrowser);
michael@0 35
michael@0 36 aBrowser.translationUI.showTranslationUI(aDetectedLanguage);
michael@0 37 }
michael@0 38 }
michael@0 39 };
michael@0 40
michael@0 41 /* TranslationUI objects keep the information related to translation for
michael@0 42 * a specific browser. This object is passed to the translation
michael@0 43 * infobar so that it can initialize itself. The properties exposed to
michael@0 44 * the infobar are:
michael@0 45 * - detectedLanguage, code of the language detected on the web page.
michael@0 46 * - state, the state in which the infobar should be displayed
michael@0 47 * - STATE_{OFFER,TRANSLATING,TRANSLATED,ERROR} constants.
michael@0 48 * - translatedFrom, if already translated, source language code.
michael@0 49 * - translatedTo, if already translated, target language code.
michael@0 50 * - translate, method starting the translation of the current page.
michael@0 51 * - showOriginalContent, method showing the original page content.
michael@0 52 * - showTranslatedContent, method showing the translation for an
michael@0 53 * already translated page whose original content is shown.
michael@0 54 * - originalShown, boolean indicating if the original or translated
michael@0 55 * version of the page is shown.
michael@0 56 */
michael@0 57 function TranslationUI(aBrowser) {
michael@0 58 this.browser = aBrowser;
michael@0 59 }
michael@0 60
michael@0 61 TranslationUI.prototype = {
michael@0 62 STATE_OFFER: 0,
michael@0 63 STATE_TRANSLATING: 1,
michael@0 64 STATE_TRANSLATED: 2,
michael@0 65 STATE_ERROR: 3,
michael@0 66
michael@0 67 get doc() this.browser.contentDocument,
michael@0 68
michael@0 69 translate: function(aFrom, aTo) {
michael@0 70 this.state = this.STATE_TRANSLATING;
michael@0 71 this.translatedFrom = aFrom;
michael@0 72 this.translatedTo = aTo;
michael@0 73 },
michael@0 74
michael@0 75 showURLBarIcon: function(aTranslated) {
michael@0 76 let chromeWin = this.browser.ownerGlobal;
michael@0 77 let PopupNotifications = chromeWin.PopupNotifications;
michael@0 78 let removeId = aTranslated ? "translate" : "translated";
michael@0 79 let notification =
michael@0 80 PopupNotifications.getNotification(removeId, this.browser);
michael@0 81 if (notification)
michael@0 82 PopupNotifications.remove(notification);
michael@0 83
michael@0 84 let callback = aTopic => {
michael@0 85 if (aTopic != "showing")
michael@0 86 return false;
michael@0 87 if (!this.notificationBox.getNotificationWithValue("translation"))
michael@0 88 this.showTranslationInfoBar();
michael@0 89 return true;
michael@0 90 };
michael@0 91
michael@0 92 let addId = aTranslated ? "translated" : "translate";
michael@0 93 PopupNotifications.show(this.browser, addId, null,
michael@0 94 addId + "-notification-icon", null, null,
michael@0 95 {dismissed: true, eventCallback: callback});
michael@0 96 },
michael@0 97
michael@0 98 _state: 0,
michael@0 99 get state() this._state,
michael@0 100 set state(val) {
michael@0 101 let notif = this.notificationBox.getNotificationWithValue("translation");
michael@0 102 if (notif)
michael@0 103 notif.state = val;
michael@0 104 this._state = val;
michael@0 105 },
michael@0 106
michael@0 107 originalShown: true,
michael@0 108 showOriginalContent: function() {
michael@0 109 this.showURLBarIcon();
michael@0 110 this.originalShown = true;
michael@0 111 },
michael@0 112
michael@0 113 showTranslatedContent: function() {
michael@0 114 this.showURLBarIcon(true);
michael@0 115 this.originalShown = false;
michael@0 116 },
michael@0 117
michael@0 118 get notificationBox() this.browser.ownerGlobal.gBrowser.getNotificationBox(),
michael@0 119
michael@0 120 showTranslationInfoBar: function() {
michael@0 121 let notificationBox = this.notificationBox;
michael@0 122 let notif = notificationBox.appendNotification("", "translation", null,
michael@0 123 notificationBox.PRIORITY_INFO_HIGH);
michael@0 124 notif.init(this);
michael@0 125 return notif;
michael@0 126 },
michael@0 127
michael@0 128 showTranslationUI: function(aDetectedLanguage) {
michael@0 129 this.detectedLanguage = aDetectedLanguage;
michael@0 130
michael@0 131 // Reset all values before showing a new translation infobar.
michael@0 132 this.state = 0;
michael@0 133 this.translatedFrom = "";
michael@0 134 this.translatedTo = "";
michael@0 135 this.originalShown = true;
michael@0 136
michael@0 137 this.showURLBarIcon();
michael@0 138 return this.showTranslationInfoBar();
michael@0 139 }
michael@0 140 };

mercurial