Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/pootle_it/html/js/mt/apertium.js
blob: 375b6172bc17aa41cd33eea15382202118f8fb75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(function ($) {
  window.PTL.editor.mt = window.PTL.editor.mt || {};

  PTL.editor.mt.apertium = {

    buttonClassName: "apertium",
    imageUri: m("images/apertium.png"),
    hint: "Apertium",
    validatePair: true,

    url: "http://api.apertium.org/JSLibrary.js",
    cookieName: "apertium_pairs",
    cookieOptions: {path: '/', expires: 15},

    init: function (apiKey) {
      var _this = PTL.editor.mt.apertium;
      /* Load Apertium library */
      _this.url = apiKey == undefined ? _this.url : _this.url + '?key=' + apiKey;
      $.getScript(_this.url, function () {
        /* Init variables */
        var _this = PTL.editor.mt.apertium;
        _this.targetLang = PTL.editor.normalizeCode($("div#target_lang").text());

        _this.pairs = $.cookie(_this.cookieName);
        if (!_this.pairs) {
          var pairs = apertium.getSupportedLanguagePairs();
          _this.pairs = $.map(pairs, function (obj, i) {
            return {source: obj.source, target: obj.target};
          });
          var cookie_data = JSON.stringify(_this.pairs);
          $.cookie(_this.cookieName, cookie_data, _this.cookieOptions);
        } else {
          _this.pairs = $.parseJSON(_this.pairs);
        }

        /* Bind event handler */
        $(".apertium").live("click", _this.translate);
      });
    },

    ready: function () {
      PTL.editor.addMTButtons(PTL.editor.mt.apertium);
    },

    translate: function () {
      PTL.editor.translate(this, function(sourceText, langFrom, langTo, resultCallback) {
        var content = new Object()
        content.text = sourceText;
        content.type = "txt";
        apertium.translate(content, langFrom, langTo, function (result) {
          if (result.translation) {
            resultCallback(result.translation);
          } else {
            resultCallback(false, "Apertium Error: " + result.error.message);
          }
        });
      });
    }

  };
})(jQuery);