scribe-translator
Version:
Lightweight translator for javascript
1 lines • 1.31 kB
JavaScript
!function(a){if("object"==typeof module&&"object"==typeof module.exports){var b=a(require,exports);void 0!==b&&(module.exports=b)}else"function"==typeof define&&define.amd&&define(["require","exports"],a)}(function(a,b){"use strict";var c=function(){function a(a,b){void 0===b&&(b=":"),this.translations=a,this.placeholderIdentifier=b}return a.prototype.has=function(a){return this.get(a)===a},a.prototype.get=function(a,b){var c=this.getLine(a);return c?b?this.makeReplacements(c,b):c:a},a.prototype.choice=function(a,b,c){var d=this.get(a,c).split("|");return d[0]===a?a:1===d.length||b<=1?d[0]:d[1]},a.prototype.getLine=function(a){var b=a.split(".");return b.reduce(function(a,b){if(a)return a[b]},this.translations)},a.prototype.makeReplacements=function(a,b){var c=this,d=this.getSortedReplacementKeys(b);return d.forEach(function(d){var e=b[d];a=Array.isArray(e)?c.makeArrayReplacements(a,d,e):a.replace(new RegExp(""+c.placeholderIdentifier+d,"g"),e)}),a},a.prototype.makeArrayReplacements=function(a,b,c){var d=this;return c.reduce(function(a,c){return a.replace(new RegExp(""+d.placeholderIdentifier+b),c)},a)},a.prototype.getSortedReplacementKeys=function(a){return Object.keys(a).sort(function(a,b){return b.length-a.length})},a}();Object.defineProperty(b,"__esModule",{value:!0}),b["default"]=c});