
window.t10 = (function (window) {
    'use strict';

    var $ = window.jQuery;

    var exports = function () {};

    var t10Pt = exports.prototype;

    /**
     * Set the translation resource
     * @param {object} resource to set
     */
    t10Pt.setResource = function (resource) {
        this.resource = resource;
    };

    /**
     * Translate a key
     * @param {string} key to translate
     */
    t10Pt.t = function (key) {
        var value = this.resource[key];

        if (value != null) {
            return value;
        }

        return key;
    };

    /**
     * Scan <t> tags and .t-text and .t-attr class elements and translate its contents
     * @return object
     * @subreturn {number} object['t-tag'] the count of translated <t> tags
     * @subreturn {number} object['t-text'] the count of translated .t-text tags
     * @subreturn {number} object['t-attr'] the count of translated .t-attr tags' attributes
     */
    t10Pt.scan = function (dom) {
        var t = this.scanTTag(dom);
        var tText = this.scanTText(dom);
        var tAttr = this.scanTAttr(dom);

        return {
            't-tag': t,
            't-text': tText,
            't-attr': tAttr
        };
    };

    /**
     * remove <t> tag and insert string for key
     * @return translated key count
     */
    t10Pt.scanTTag = function (dom) {

        var self = this;

        var list = $('t', dom).each(function () {

            var t = $(this);

            t.after(self.t(t.text())).remove();

        });

        return list.length;

    };

    /**
     * scan .t-text class and replace text with translated string
     * @return translated key count
     */
    t10Pt.scanTText = function (dom) {

        var self = this;

        var count = 0;

        $('.t-text', dom).each(function () {

            var elm = $(this);

            // replace text with translated string
            elm.text(self.t(elm.text()));

            elm.removeClass('t-text').addClass('t-text-done');

            // increment translation count
            count ++;
        });

        return count;
    };

    var T_ATTR_REGEXP = /^t:/; // translatable attribute starts with 't:'

    /**
     * scan .t-attr class and translate its attr starts with 't:' prefix
     * @return translated key count
     */
    t10Pt.scanTAttr = function (dom) {

        var self = this;

        var count = 0;

        $('.t-attr', dom).each(function () {

            $.each(this.attributes, function (i, attr) {
                var label = attr.value;

                if (T_ATTR_REGEXP.test(label)) {
                    label = label.replace(T_ATTR_REGEXP, '');

                    // replace attribute value with translated string
                    attr.value = self.t(label);

                    // increment translation count
                    count ++;
                }

            });

            $(this).removeClass('t-attr').addClass('t-attr-done');

        });

        return count;
    };

    t10Pt.setAvailableLanguages = function (array) {
        this.availables = array;

        this.defaultLanguage = array[0];
    };

    t10Pt.setLanguage = function (language) {
        this.language = language;
    };

    t10Pt.pickUsableLanguage = function (language) {
        language = language || this.language;

        if (language == null) {
            return this.defaultLanguage;
        }

        var select = null;

        for (var i = 0; i < this.availables.length; i++) {
            var available = this.availables[i];

            var foundPos = language.indexOf(available);

            if (foundPos === 0) {
                if (select == null || select.length < available.length) {
                    select = available;
                }
            }
        }

        if (select == null) {
            return this.defaultLanguage;
        }

        return select;

    };

    t10Pt.loadScript = function (urlPattern) {
        return $.getScript(urlPattern.replace('{LANGUAGE}', this.pickUsableLanguage()));
    };

    t10Pt.loadJson = function (urlPattern) {
        return $.getJSON(urlPattern.replace('{LANGUAGE}', this.pickUsableLanguage())).pipe(function (resource) {
            this.setResources(resource);
        });
    };

    return new exports();

}(window));

@@ -1,8 +1,116 @@
+%0A/**%0A * @class t10%0A * @singleton%0A *%0A * t10 class provides basic translation functionalities in Browsers.%0A */
 %0Awindow.
@@ -298,16 +298,23 @@
 esource%0A
+     *%0A
      * @
@@ -320,17 +320,17 @@
 @param %7B
-o
+O
 bject%7D r
@@ -341,14 +341,92 @@
 rce 
-to set
+The mapping from key to translated string%0A     * @return %7Bt10%7D The t10 object itself
 %0A   
@@ -509,16 +509,38 @@
 esource;
+%0A%0A        return this;
 %0A    %7D;%0A
@@ -567,24 +567,31 @@
 slate a key%0A
+     *%0A
      * @para
@@ -597,15 +597,23 @@
 am %7B
-s
+S
 tring%7D
+ key The
  key
@@ -626,16 +626,62 @@
 anslate%0A
+     * @return %7BString%7D The translated string%0A
      */%0A
@@ -941,16 +941,23 @@
 ontents%0A
+     *%0A
      * @
@@ -981,33 +981,33 @@
   * @subreturn %7B
-n
+N
 umber%7D object%5B't
@@ -1057,33 +1057,33 @@
   * @subreturn %7B
-n
+N
 umber%7D object%5B't
@@ -1146,17 +1146,17 @@
 return %7B
-n
+N
 umber%7D o
@@ -1550,24 +1550,31 @@
 ing for key%0A
+     *%0A
      * @retu
@@ -1916,32 +1916,39 @@
 anslated string%0A
+     *%0A
      * @return t
@@ -2484,32 +2484,32 @@
 h 't:'%0A%0A    /**%0A
-
      * scan .t-a
@@ -2565,16 +2565,23 @@
  prefix%0A
+     *%0A
      * @

Add comments and make setResource chainable