generaltranslation
Version:
A language toolkit for AI developers
3 lines (2 loc) • 12.5 kB
JavaScript
import{parse as t,TYPE as r}from"@formatjs/icu-messageformat-parser";import{TYPE as n}from"@formatjs/icu-messageformat-parser/types.js";import{printAST as e}from"@formatjs/icu-messageformat-parser/printer.js";import o from"fast-json-stable-stringify";import i from"crypto-js";var a="https://cdn.gtx.dev",u="https://api2.gtx.dev",l="https://runtime2.gtx.dev",s="en",c=6e4,d=["singular","plural","dual","zero","one","two","few","many","other"];function f(t){return d.includes(t)}var v=function(){return v=Object.assign||function(t){for(var r,n=1,e=arguments.length;n<e;n++)for(var o in r=arguments[n])Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o]);return t},v.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var p,h,m="ellipsis",g="DEFAULT_TERMINATOR_KEY",y={ellipsis:(p={fr:{terminator:"…",separator:" "},zh:{terminator:"……",separator:void 0},ja:{terminator:"……",separator:void 0}},p[g]={terminator:"…",separator:void 0},p),none:(h={},h[g]={terminator:void 0,separator:void 0},h)},b=function(){function t(t,r){var n,e,o,i,a,u,l,c,d;void 0===r&&(r={});try{var f=t?Array.isArray(t)?t.map(function(t){return String(t)}):[String(t)]:[s],v=Intl.getCanonicalLocales(f);this.locale=v.length?v[0]:s}catch(t){this.locale=s}if(!y[null!==(n=r.style)&&void 0!==n?n:m])throw new Error(function(t){return"generaltranslation Formatting Error: Invalid cutoff style: ".concat(t,".")}(null!==(e=r.style)&&void 0!==e?e:m));if(void 0!==r.maxChars){c=null!==(o=r.style)&&void 0!==o?o:m;var p=new Intl.Locale(this.locale).language;d=y[c][p]||y[c][g]}var h=null!==(i=r.terminator)&&void 0!==i?i:null==d?void 0:d.terminator,b=null!=h?null!==(a=r.separator)&&void 0!==a?a:null==d?void 0:d.separator:void 0;this.additionLength=(null!==(u=null==h?void 0:h.length)&&void 0!==u?u:0)+(null!==(l=null==b?void 0:b.length)&&void 0!==l?l:0),void 0!==r.maxChars&&Math.abs(r.maxChars)<this.additionLength&&(h=void 0,b=void 0),this.options={maxChars:r.maxChars,style:c,terminator:h,separator:b}}return t.prototype.format=function(t){return this.formatToParts(t).join("")},t.prototype.formatToParts=function(t){var r=this.options,n=r.maxChars,e=r.terminator,o=r.separator,i=void 0===n||Math.abs(n)>=t.length?n:n>=0?Math.max(0,n-this.additionLength):Math.min(0,n+this.additionLength),a=void 0!==i&&i>-1?t.slice(0,i):t.slice(i);return null==n||null==i||0===i||null==e||t.length<=Math.abs(n)?[a]:i>0?null!=o?[a,o,e]:[a,e]:null!=o?[e,o,a]:[e,a]},t.prototype.resolvedOptions=function(){return this.options},t}(),j={Collator:Intl.Collator,DateTimeFormat:Intl.DateTimeFormat,DisplayNames:Intl.DisplayNames,ListFormat:Intl.ListFormat,Locale:Intl.Locale,NumberFormat:Intl.NumberFormat,PluralRules:Intl.PluralRules,RelativeTimeFormat:Intl.RelativeTimeFormat,Segmenter:Intl.Segmenter,CutoffFormat:b},w=function(){function t(){this.cache={}}return t.prototype._generateKey=function(t,r){void 0===r&&(r={});var n=t?Array.isArray(t)?t.map(function(t){return String(t)}).join(","):String(t):"undefined",e=r?JSON.stringify(r,Object.keys(r).sort()):"{}";return"".concat(n,":").concat(e)},t.prototype.get=function(t){for(var r,n,e=[],o=1;o<arguments.length;o++)e[o-1]=arguments[o];var i=e[0],a=void 0===i?s:i,u=e[1],l=void 0===u?{}:u,c=this._generateKey(a,l),d=null===(n=this.cache[t])||void 0===n?void 0:n[c];return void 0===d&&(d=new((r=j[t]).bind.apply(r,function(t,r,n){if(n||2===arguments.length)for(var e,o=0,i=r.length;o<i;o++)!e&&o in r||(e||(e=Array.prototype.slice.call(r,0,o)),e[o]=r[o]);return t.concat(e||Array.prototype.slice.call(r))}([void 0],e,!1))),this.cache[t]||(this.cache[t]={}),this.cache[t][c]=d),d},t}(),S=new w;function O(t,r,n){void 0===r&&(r=d),void 0===n&&(n=[s]);var e=S.get("PluralRules",n).select(t),o=Math.abs(t);if(0===o&&r.includes("zero"))return"zero";if(1===o){if(r.includes("singular"))return"singular";if(r.includes("one"))return"one"}if("one"===e&&r.includes("singular"))return"singular";if(2===o){if(r.includes("dual"))return"dual";if(r.includes("two"))return"two"}return"two"===e&&r.includes("dual")?"dual":r.includes(e)?e:"two"===e&&r.includes("dual")?"dual":"two"===e&&r.includes("plural")?"plural":"two"===e&&r.includes("other")?"other":"few"===e&&r.includes("plural")?"plural":"few"===e&&r.includes("other")?"other":"many"===e&&r.includes("plural")?"plural":"many"===e&&r.includes("other")?"other":"other"===e&&r.includes("plural")?"plural":""}function x(t){var r=t;if(r&&"object"==typeof r&&"string"==typeof r.k){var n=Object.keys(r);if(1===n.length)return!0;if(2===n.length){if("number"==typeof r.i)return!0;if("string"==typeof r.v)return!0}if(3===n.length&&"string"==typeof r.v&&"number"==typeof r.i)return!0}return!1}var I={variable:"v",number:"n",datetime:"d",currency:"c"};function A(t){return I[t]}function E(t){if("undefined"!=typeof Buffer)return Buffer.from(t,"utf8").toString("base64");for(var r=(new TextEncoder).encode(t),n="",e=0;e<r.length;e++)n+=String.fromCharCode(r[e]);return btoa(n)}function _(t){if("undefined"!=typeof Buffer)return Buffer.from(t,"base64").toString("utf8");for(var r=atob(t),n=new Uint8Array(r.length),e=0;e<r.length;e++)n[e]=r.charCodeAt(e);return(new TextDecoder).decode(n)}function F(n){var e=n.icuString,o=n.shouldVisit,i=n.visitor,a=n.options,u=a.recurseIntoVisited,l=void 0===u||u,s=function(t,r){var n={};for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&r.indexOf(e)<0&&(n[e]=t[e]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(e=Object.getOwnPropertySymbols(t);o<e.length;o++)r.indexOf(e[o])<0&&Object.prototype.propertyIsEnumerable.call(t,e[o])&&(n[e[o]]=t[e[o]])}return n}(a,["recurseIntoVisited"]),c=t(e,s);return d(c),c;function d(t){t.map(f)}function f(t){var n=!1;o(t)&&(i(t),n=!0),n&&!l||(t.type===r.select||t.type===r.plural?Object.values(t.options).map(function(t){return t.value}).map(d):t.type===r.tag&&d(t.children))}}var k="_gt_",C="_gt_var_name",L=new RegExp("^".concat(k,"\\d+$")),T=new RegExp("^".concat(k,"$"));function P(t){var r;return t.type===n.select&&L.test(t.value)&&!!t.options.other&&(0===t.options.other.value.length||t.options.other.value.length>0&&(null===(r=t.options.other.value[0])||void 0===r?void 0:r.type)===n.literal)}function V(t){var r;return t.type===n.select&&T.test(t.value)&&!!t.options.other&&(0===t.options.other.value.length||t.options.other.value.length>0&&(null===(r=t.options.other.value[0])||void 0===r?void 0:r.type)===n.literal)}function R(t){if(!t.includes(k))return t;var r=[];F({icuString:t,shouldVisit:V,visitor:function(t){var n,e,o,i;r.push({start:null!==(e=null===(n=t.location)||void 0===n?void 0:n.start.offset)&&void 0!==e?e:0,end:null!==(i=null===(o=t.location)||void 0===o?void 0:o.end.offset)&&void 0!==i?i:0,value:t.options.other.value.length>0?t.options.other.value[0].value:""})},options:{recurseIntoVisited:!1,captureLocation:!0}});for(var n=0,e=[],o=0;o<r.length;o++)e.push(t.slice(n,r[o].start)),e.push(r[o].value),n=r[o].end;return n<t.length&&e.push(t.slice(n)),e.join("")}function M(t){var r=t.replace(/['\']/g,"''"),n=/[{}<>]/,e=r.search(n);if(-1===e)return r;for(var o=-1,i=r.length-1;i>=0;i--)if(n.test(r[i])){o=i;break}return r=r.slice(0,e)+"'"+r.slice(e,o+1)+"'"+r.slice(o+1)}function D(t,r){var n=M(String(null!=t?t:"")),e=" other {".concat(n,"}"),o="";if(null==r?void 0:r.$name){var i=M(r.$name);o=" ".concat(C," {").concat(i,"}")}return"{".concat(k,", select,").concat(e).concat(o,"}")}function N(t){return t}function z(t){if(!t.includes(k))return t;var r=[];F({icuString:t,shouldVisit:V,visitor:function(t){var n,e,o,i,a,u,l,s;r.push({start:null!==(e=null===(n=t.location)||void 0===n?void 0:n.start.offset)&&void 0!==e?e:0,end:null!==(i=null===(o=t.location)||void 0===o?void 0:o.end.offset)&&void 0!==i?i:0,otherStart:null!==(u=null===(a=t.options.other.location)||void 0===a?void 0:a.start.offset)&&void 0!==u?u:0,otherEnd:null!==(s=null===(l=t.options.other.location)||void 0===l?void 0:l.end.offset)&&void 0!==s?s:0})},options:{recurseIntoVisited:!1,captureLocation:!0}});for(var n=[],e=0,o=0;o<r.length;o++){var i=r[o],a=i.start,u=i.end,l=i.otherStart,s=i.otherEnd;n.push(t.slice(e,a)),n.push(t.slice(a,a+4+1)),n.push(String(o+1)),n.push(t.slice(a+4+1,l)),n.push("{}"),n.push(t.slice(s,u)),e=u}return n.push(t.slice(e,t.length)),n.join("")}function B(t){if(!t.includes(k))return{};var r=1,n={};return F({icuString:t,shouldVisit:V,visitor:function(t){var e;n[t.value+r]=t.options.other.value.length?null===(e=t.options.other.value[0])||void 0===e?void 0:e.value:"",r+=1},options:{recurseIntoVisited:!1}}),n}function $(t){if(!t.includes(k))return t;var r=F({icuString:t,shouldVisit:P,visitor:function(t){t.type=n.argument,delete t.options},options:{recurseIntoVisited:!1}});return e(r)}function K(t){return"object"==typeof t&&null!=t&&"key"in t}function H(t){return"object"==typeof t&&null!=t&&"k"in t}function U(t){return"string"==typeof t||(!!K(t)||function(t){return"object"==typeof t&&null!=t&&"type"in t&&"props"in t}(t))}function Y(t){return"string"==typeof t||(Array.isArray(t)?!t.some(function(t){return!U(t)}):U(t))}function J(t){return"string"==typeof t?t:K(t)?X(t):G(t)}function q(t){return"string"==typeof t?t:Array.isArray(t)?t.map(J):J(t)}function G(t){var r;if("string"==typeof t)return t;var n=void 0;null!=t.type&&(n=t.type);var e=void 0;return null!=(null===(r=t.props)||void 0===r?void 0:r.children)&&(e=q(t.props.children)),v(v(v({},n&&{t:n}),e&&{c:e}),{d:Z(t.props["data-_gt"]),i:t.props["data-_gt"].id})}function Q(t){return"branch"===t?"b":"p"}function W(t){switch(t){case"number":return"n";case"variable":default:return"v";case"datetime":return"d";case"currency":return"c"}}function X(t){var r=void 0;null!=t.variable&&(r=W(t.variable));var n=void 0;return null!=t.id&&(n=t.id),v(v({k:t.key},r&&{v:r}),n&&{i:n})}function Z(t){var r,n=void 0;return t.branches&&(n=Object.fromEntries(Object.entries(t.branches).map(function(t){return[t[0],q(t[1])]}))),t.transformation&&(r=Q(t.transformation)),v(v({},n&&{b:n}),r&&{t:r})}function tt(t){return"string"==typeof t?t:H(t)?it(t):nt(t)}function rt(t){return Y(t)||"string"==typeof t?t:Array.isArray(t)?t.map(tt):tt(t)}function nt(t){var r=t.t,n=void 0;return null!=t.c&&(n=rt(t.c)),{type:r,props:{children:n,"data-_gt":at(t.d||{},t.i)}}}function et(t){return"b"===t?"branch":"plural"}function ot(t){switch(t){case"n":return"number";case"v":default:return"variable";case"d":return"datetime";case"c":return"currency"}}function it(t){var r=void 0;null!=t.v&&(r=ot(t.v));var n=void 0;return null!=t.i&&(n=t.i),v(v({key:t.k},r&&{variable:r}),n&&{id:n})}function at(t,r){var n=void 0;null!=t.t&&(n=et(t.t));var e=void 0;return null!=t.b&&(e=Object.fromEntries(Object.entries(t.b).map(function(t){return[t[0],rt(t[1])]}))),v(v({id:r},n&&{transformation:n}),e&&{branches:e})}function ut(t){return i.SHA256(t).toString(i.enc.Hex)}function lt(t,r){var n=t.source,e=t.context,i=t.id,a=t.dataFormat;return void 0===r&&(r=ut),r(o(v(v(v({source:ct(n)},i&&{id:i}),e&&{context:e}),a&&{dataFormat:a})))}var st=function(t){var r,n,e,o;if(t&&"object"==typeof t){if("props"in t){var i={},a=null===(r=null==t?void 0:t.props)||void 0===r?void 0:r["data-_gt"];return(null==a?void 0:a.branches)&&(i.branches=Object.fromEntries(Object.entries(a.branches).map(function(t){return[t[0],ct(t[1])]}))),(null===(n=null==t?void 0:t.props)||void 0===n?void 0:n.children)&&(i.children=ct(t.props.children)),(null===(o=null===(e=null==t?void 0:t.props)||void 0===e?void 0:e["data-_gt"])||void 0===o?void 0:o.transformation)&&(i.transformation=t.props["data-_gt"].transformation),i}if("key"in t)return v({key:t.key},t.variable&&{variable:t.variable})}return t};function ct(t){return Array.isArray(t)?t.map(st):st(t)}export{k as VAR_IDENTIFIER,C as VAR_NAME_IDENTIFIER,$ as condenseVars,N as declareStatic,D as declareVar,_ as decode,R as decodeVars,u as defaultBaseUrl,a as defaultCacheUrl,l as defaultRuntimeApiUrl,c as defaultTimeout,E as encode,B as extractVars,Q as getNewBranchType,Z as getNewGTProp,J as getNewJsxChild,q as getNewJsxChildren,G as getNewJsxElement,X as getNewVariableObject,W as getNewVariableType,et as getOldBranchType,at as getOldGTProp,tt as getOldJsxChild,rt as getOldJsxChildren,nt as getOldJsxElement,it as getOldVariableObject,ot as getOldVariableType,O as getPluralForm,z as indexVars,f as isAcceptedPluralForm,H as isNewVariableObject,Y as isOldJsxChildren,K as isOldVariableObject,x as isVariable,s as libraryDefaultLocale,A as minifyVariableType,lt as oldHashJsxChildren,ut as oldHashString,d as pluralForms};
//# sourceMappingURL=internal.esm.min.mjs.map