Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Copyright © Magento, Inc. All rights reserved.
  3.  * See COPYING.txt for license details.
  4.  */
  5.  
  6. define([
  7.     'jquery',
  8.     'ko',
  9.     'module',
  10.     '../template/renderer',
  11.     'mage/translate'
  12. ], function ($, ko, module, renderer) {
  13.     'use strict';
  14.  
  15.     var locations = {
  16.             'legend': 'Caption for the fieldset element',
  17.             'label': 'Label for an input element.',
  18.             'button': 'Push button',
  19.             'a': 'Link label',
  20.             'b': 'Bold text',
  21.             'strong': 'Strong emphasized text',
  22.             'i': 'Italic text',
  23.             'em': 'Emphasized text',
  24.             'u': 'Underlined text',
  25.             'sup': 'Superscript text',
  26.             'sub': 'Subscript text',
  27.             'span': 'Span element',
  28.             'small': 'Smaller text',
  29.             'big': 'Bigger text',
  30.             'address': 'Contact information',
  31.             'blockquote': 'Long quotation',
  32.             'q': 'Short quotation',
  33.             'cite': 'Citation',
  34.             'caption': 'Table caption',
  35.             'abbr': 'Abbreviated phrase',
  36.             'acronym': 'An acronym',
  37.             'var': 'Variable part of a text',
  38.             'dfn': 'Term',
  39.             'strike': 'Strikethrough text',
  40.             'del': 'Deleted text',
  41.             'ins': 'Inserted text',
  42.             'h1': 'Heading level 1',
  43.             'h2': 'Heading level 2',
  44.             'h3': 'Heading level 3',
  45.             'h4': 'Heading level 4',
  46.             'h5': 'Heading level 5',
  47.             'h6': 'Heading level 6',
  48.             'center': 'Centered text',
  49.             'select': 'List options',
  50.             'img': 'Image',
  51.             'input': 'Form element'
  52.         },
  53.  
  54.         /**
  55.          * Generates [data-translate] attribute's value
  56.          * @param {Object} translationData
  57.          * @param {String} location
  58.          */
  59.         composeTranslateAttr = function (translationData, location) {
  60.             var obj = [{
  61.                 'shown': translationData.shown,
  62.                 'translated': translationData.translated,
  63.                 'original': translationData.original,
  64.                 'location': locations[location] || 'Text'
  65.             }];
  66.  
  67.             return JSON.stringify(obj);
  68.         },
  69.  
  70.         /**
  71.          * Sets text for the element
  72.          * @param {Object} el
  73.          * @param {String} text
  74.          */
  75.         setText = function (el, text) {
  76.             $(el).text(text);
  77.         },
  78.  
  79.         /**
  80.          * Sets [data-translate] attribute for the element
  81.          * @param {Object} el - The element which is binded
  82.          * @param {String} original - The original value of the element
  83.          */
  84.         setTranslateProp = function (el, original) {
  85.             var location = $(el).prop('tagName').toLowerCase(),
  86.                 translated = $.mage.__(original),
  87.                 translationData = {
  88.                     shown: translated,
  89.                     translated: translated,
  90.                     original: original
  91.                 },
  92.                 translateAttr = composeTranslateAttr(translationData, location);
  93.  
  94.             $(el).attr('data-translate', translateAttr);
  95.  
  96.             setText(el, translationData.shown);
  97.         },
  98.  
  99.         /**
  100.          * Checks if node represents ko virtual node (nodeType === 8, nodeName === '#comment').
  101.          *
  102.          * @param {HTMLElement} node
  103.          * @returns {Boolean}
  104.          */
  105.         isVirtualElement = function (node) {
  106.             return node.nodeType === 8;
  107.         },
  108.  
  109.         /**
  110.         * Checks if it's real DOM element
  111.         * in case of virtual element, returns span wrapper
  112.         * @param {Object} el
  113.         * @param {bool} isUpdate
  114.         * @return {Object} el
  115.         */
  116.         getRealElement = function (el, isUpdate) {
  117.             if (isVirtualElement(el)) {
  118.                 if (isUpdate) {
  119.                     return $(el).next('span');
  120.                 }
  121.  
  122.                 return $('<span/>').insertAfter(el);
  123.             }
  124.  
  125.             return el;
  126.         },
  127.  
  128.         /**
  129.          * execute i18n binding
  130.          * @param {Object} element
  131.          * @param {Function} valueAccessor
  132.          * @param {bool} isUpdate
  133.          */
  134.         execute = function (element, valueAccessor, isUpdate) {
  135.             var original = ko.unwrap(valueAccessor() || ''),
  136.                 el = getRealElement(element, isUpdate),
  137.                 inlineTranslation = (module.config() || {}).inlineTranslation;
  138.  
  139.             if (inlineTranslation) {
  140.                 setTranslateProp(el, original);
  141.             } else {
  142.                 setText(el, $.mage.__(original));
  143.             }
  144.         };
  145.  
  146.     /**
  147.      * i18n binding
  148.      * @property {Function}  init
  149.      * @property {Function}  update
  150.      */
  151.     ko.bindingHandlers.i18n = {
  152.  
  153.         /**
  154.          * init i18n binding
  155.          * @param {Object} element
  156.          * @param {Function} valueAccessor
  157.          */
  158.         init: function (element, valueAccessor) {
  159.             execute(element, valueAccessor);
  160.         },
  161.  
  162.         /**
  163.          * update i18n binding
  164.          * @param {Object} element
  165.          * @param {Function} valueAccessor
  166.          */
  167.         update: function (element, valueAccessor) {
  168.             execute(element, valueAccessor, true);
  169.         }
  170.     };
  171.  
  172.     ko.virtualElements.allowedBindings.i18n = true;
  173.  
  174.     renderer
  175.         .addNode('translate', {
  176.             binding: 'i18n'
  177.         })
  178.         .addAttribute('translate', {
  179.             binding: 'i18n'
  180.         });
  181. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement