jquery-i18next.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.jqueryI18next = factory());
  5. }(this, (function () { 'use strict';
  6. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  7. var defaults = {
  8. tName: 't',
  9. i18nName: 'i18n',
  10. handleName: 'localize',
  11. selectorAttr: 'data-i18n',
  12. targetAttr: 'i18n-target',
  13. optionsAttr: 'i18n-options',
  14. useOptionsAttr: false,
  15. parseDefaultValueFromContent: true
  16. };
  17. function init(i18next, $) {
  18. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  19. options = _extends({}, defaults, options);
  20. function parse(ele, key, opts) {
  21. if (key.length === 0) return;
  22. var attr = 'text';
  23. if (key.indexOf('[') === 0) {
  24. var parts = key.split(']');
  25. key = parts[1];
  26. attr = parts[0].substr(1, parts[0].length - 1);
  27. }
  28. if (key.indexOf(';') === key.length - 1) {
  29. key = key.substr(0, key.length - 2);
  30. }
  31. function extendDefault(o, val) {
  32. if (!options.parseDefaultValueFromContent) return o;
  33. return _extends({}, o, { defaultValue: val });
  34. }
  35. if (attr === 'html') {
  36. ele.html(i18next.t(key, extendDefault(opts, ele.html())));
  37. } else if (attr === 'text') {
  38. ele.text(i18next.t(key, extendDefault(opts, ele.text())));
  39. } else if (attr === 'prepend') {
  40. ele.prepend(i18next.t(key, extendDefault(opts, ele.html())));
  41. } else if (attr === 'append') {
  42. ele.append(i18next.t(key, extendDefault(opts, ele.html())));
  43. } else if (attr.indexOf('data-') === 0) {
  44. var dataAttr = attr.substr('data-'.length);
  45. var translated = i18next.t(key, extendDefault(opts, ele.data(dataAttr)));
  46. // we change into the data cache
  47. ele.data(dataAttr, translated);
  48. // we change into the dom
  49. ele.attr(attr, translated);
  50. } else {
  51. ele.attr(attr, i18next.t(key, extendDefault(opts, ele.attr(attr))));
  52. }
  53. }
  54. function localize(ele, opts) {
  55. var key = ele.attr(options.selectorAttr);
  56. if (!key && typeof key !== 'undefined' && key !== false) key = ele.text() || ele.val();
  57. if (!key) return;
  58. var target = ele,
  59. targetSelector = ele.data(options.targetAttr);
  60. if (targetSelector) target = ele.find(targetSelector) || ele;
  61. if (!opts && options.useOptionsAttr === true) opts = ele.data(options.optionsAttr);
  62. opts = opts || {};
  63. if (key.indexOf(';') >= 0) {
  64. var keys = key.split(';');
  65. $.each(keys, function (m, k) {
  66. // .trim(): Trim the comma-separated parameters on the data-i18n attribute.
  67. if (k !== '') parse(target, k.trim(), opts);
  68. });
  69. } else {
  70. parse(target, key, opts);
  71. }
  72. if (options.useOptionsAttr === true) {
  73. var clone = {};
  74. clone = _extends({ clone: clone }, opts);
  75. delete clone.lng;
  76. ele.data(options.optionsAttr, clone);
  77. }
  78. }
  79. function handle(opts) {
  80. return this.each(function () {
  81. // localize element itself
  82. localize($(this), opts);
  83. // localize children
  84. var elements = $(this).find('[' + options.selectorAttr + ']');
  85. elements.each(function () {
  86. localize($(this), opts);
  87. });
  88. });
  89. };
  90. // $.t $.i18n shortcut
  91. $[options.tName] = i18next.t.bind(i18next);
  92. $[options.i18nName] = i18next;
  93. // selector function $(mySelector).localize(opts);
  94. $.fn[options.handleName] = handle;
  95. }
  96. var index = {
  97. init: init
  98. };
  99. return index;
  100. })));