browser/components/translation/Translation.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/translation/Translation.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,140 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["Translation"];
    1.11 +
    1.12 +const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Promise.jsm");
    1.15 +Cu.import("resource://gre/modules/Services.jsm");
    1.16 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.17 +
    1.18 +this.Translation = {
    1.19 +  supportedSourceLanguages: ["en", "zh", "ja", "es", "de", "fr", "ru", "ar", "ko", "pt"],
    1.20 +  supportedTargetLanguages: ["en", "pl", "tr", "vi"],
    1.21 +
    1.22 +  _defaultTargetLanguage: "",
    1.23 +  get defaultTargetLanguage() {
    1.24 +    if (!this._defaultTargetLanguage) {
    1.25 +      this._defaultTargetLanguage = Cc["@mozilla.org/chrome/chrome-registry;1"]
    1.26 +                                      .getService(Ci.nsIXULChromeRegistry)
    1.27 +                                      .getSelectedLocale("global")
    1.28 +                                      .split("-")[0];
    1.29 +    }
    1.30 +    return this._defaultTargetLanguage;
    1.31 +  },
    1.32 +
    1.33 +  languageDetected: function(aBrowser, aDetectedLanguage) {
    1.34 +    if (this.supportedSourceLanguages.indexOf(aDetectedLanguage) != -1 &&
    1.35 +        aDetectedLanguage != this.defaultTargetLanguage) {
    1.36 +      if (!aBrowser.translationUI)
    1.37 +        aBrowser.translationUI = new TranslationUI(aBrowser);
    1.38 +
    1.39 +      aBrowser.translationUI.showTranslationUI(aDetectedLanguage);
    1.40 +    }
    1.41 +  }
    1.42 +};
    1.43 +
    1.44 +/* TranslationUI objects keep the information related to translation for
    1.45 + * a specific browser.  This object is passed to the translation
    1.46 + * infobar so that it can initialize itself.  The properties exposed to
    1.47 + * the infobar are:
    1.48 + * - detectedLanguage, code of the language detected on the web page.
    1.49 + * - state, the state in which the infobar should be displayed
    1.50 + * - STATE_{OFFER,TRANSLATING,TRANSLATED,ERROR} constants.
    1.51 + * - translatedFrom, if already translated, source language code.
    1.52 + * - translatedTo, if already translated, target language code.
    1.53 + * - translate, method starting the translation of the current page.
    1.54 + * - showOriginalContent, method showing the original page content.
    1.55 + * - showTranslatedContent, method showing the translation for an
    1.56 + *   already translated page whose original content is shown.
    1.57 + * - originalShown, boolean indicating if the original or translated
    1.58 + *   version of the page is shown.
    1.59 + */
    1.60 +function TranslationUI(aBrowser) {
    1.61 +  this.browser = aBrowser;
    1.62 +}
    1.63 +
    1.64 +TranslationUI.prototype = {
    1.65 +  STATE_OFFER: 0,
    1.66 +  STATE_TRANSLATING: 1,
    1.67 +  STATE_TRANSLATED: 2,
    1.68 +  STATE_ERROR: 3,
    1.69 +
    1.70 +  get doc() this.browser.contentDocument,
    1.71 +
    1.72 +  translate: function(aFrom, aTo) {
    1.73 +    this.state = this.STATE_TRANSLATING;
    1.74 +    this.translatedFrom = aFrom;
    1.75 +    this.translatedTo = aTo;
    1.76 +  },
    1.77 +
    1.78 +  showURLBarIcon: function(aTranslated) {
    1.79 +    let chromeWin = this.browser.ownerGlobal;
    1.80 +    let PopupNotifications = chromeWin.PopupNotifications;
    1.81 +    let removeId = aTranslated ? "translate" : "translated";
    1.82 +    let notification =
    1.83 +      PopupNotifications.getNotification(removeId, this.browser);
    1.84 +    if (notification)
    1.85 +      PopupNotifications.remove(notification);
    1.86 +
    1.87 +    let callback = aTopic => {
    1.88 +      if (aTopic != "showing")
    1.89 +        return false;
    1.90 +      if (!this.notificationBox.getNotificationWithValue("translation"))
    1.91 +        this.showTranslationInfoBar();
    1.92 +      return true;
    1.93 +    };
    1.94 +
    1.95 +    let addId = aTranslated ? "translated" : "translate";
    1.96 +    PopupNotifications.show(this.browser, addId, null,
    1.97 +                            addId + "-notification-icon", null, null,
    1.98 +                            {dismissed: true, eventCallback: callback});
    1.99 +  },
   1.100 +
   1.101 +  _state: 0,
   1.102 +  get state() this._state,
   1.103 +  set state(val) {
   1.104 +    let notif = this.notificationBox.getNotificationWithValue("translation");
   1.105 +    if (notif)
   1.106 +      notif.state = val;
   1.107 +    this._state = val;
   1.108 +  },
   1.109 +
   1.110 +  originalShown: true,
   1.111 +  showOriginalContent: function() {
   1.112 +    this.showURLBarIcon();
   1.113 +    this.originalShown = true;
   1.114 +  },
   1.115 +
   1.116 +  showTranslatedContent: function() {
   1.117 +    this.showURLBarIcon(true);
   1.118 +    this.originalShown = false;
   1.119 +  },
   1.120 +
   1.121 +  get notificationBox() this.browser.ownerGlobal.gBrowser.getNotificationBox(),
   1.122 +
   1.123 +  showTranslationInfoBar: function() {
   1.124 +    let notificationBox = this.notificationBox;
   1.125 +    let notif = notificationBox.appendNotification("", "translation", null,
   1.126 +                                                   notificationBox.PRIORITY_INFO_HIGH);
   1.127 +    notif.init(this);
   1.128 +    return notif;
   1.129 +  },
   1.130 +
   1.131 +  showTranslationUI: function(aDetectedLanguage) {
   1.132 +    this.detectedLanguage = aDetectedLanguage;
   1.133 +
   1.134 +    // Reset all values before showing a new translation infobar.
   1.135 +    this.state = 0;
   1.136 +    this.translatedFrom = "";
   1.137 +    this.translatedTo = "";
   1.138 +    this.originalShown = true;
   1.139 +
   1.140 +    this.showURLBarIcon();
   1.141 +    return this.showTranslationInfoBar();
   1.142 +  }
   1.143 +};

mercurial