web-internationalization 2.0.21 → 2.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +23 -21
- package/index.js +1 -1
- package/package.json +8 -8
package/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Web } from 'web-component-wrapper/Web';
|
|
|
4
4
|
import { DefaultOptions, Options, Replacement } from './type';
|
|
5
5
|
export declare const log: Logger;
|
|
6
6
|
/**
|
|
7
|
-
* This plugin holds all
|
|
7
|
+
* This plugin holds all necessary methods to extend a website for
|
|
8
8
|
* internationalization.
|
|
9
9
|
* @property _defaultOptions - Options extended by the options given to the
|
|
10
10
|
* initializer method.
|
|
@@ -23,23 +23,24 @@ export declare const log: Logger;
|
|
|
23
23
|
* descriptions.
|
|
24
24
|
* @property _defaultOptions.lockDescription - Lock description.
|
|
25
25
|
* @property _defaultOptions.preReplacementLanguagePattern - Pattern to
|
|
26
|
-
* introduce a pre
|
|
26
|
+
* introduce a pre-replacement language node.
|
|
27
27
|
* @property _defaultOptions.replaceDomNodeNames - Tag names which indicates
|
|
28
28
|
* dom nodes which should be replaced.
|
|
29
29
|
* @property _defaultOptions.replacementDomNodeNames - Dom node tag name which
|
|
30
30
|
* should be interpreted as a hidden alternate language node (contains text in
|
|
31
31
|
* another language).
|
|
32
32
|
* @property _defaultOptions.replacementLanguagePattern - Text pattern to
|
|
33
|
-
* introduce a post
|
|
33
|
+
* introduce a post-replacement node.
|
|
34
34
|
* @property _defaultOptions.selection - List of all supported languages.
|
|
35
|
-
* @property _defaultOptions.selectors - Mapping of
|
|
35
|
+
* @property _defaultOptions.selectors - Mapping of necessary dom node
|
|
36
|
+
* selectors.
|
|
36
37
|
* @property _defaultOptions.selectors.knownTranslation - Selector to find
|
|
37
38
|
* known translation sections.
|
|
38
39
|
* @property _defaultOptions.sessionDescription - Description to save current
|
|
39
40
|
* language in session storage.
|
|
40
41
|
* @property _defaultOptions.templateDelimiter - Template delimiter to
|
|
41
42
|
* recognize dynamic content.
|
|
42
|
-
* @property _defaultOptions.templateDelimiter.pre - Delimiter
|
|
43
|
+
* @property _defaultOptions.templateDelimiter.pre - Delimiter that introduces
|
|
43
44
|
* a dynamic expression.
|
|
44
45
|
* @property _defaultOptions.templateDelimiter.post - Delimiter which finishes
|
|
45
46
|
* a dynamic expression.
|
|
@@ -47,10 +48,10 @@ export declare const log: Logger;
|
|
|
47
48
|
* @property currentLanguage - Saves the current language.
|
|
48
49
|
* @property knownTranslations - Saves a mapping of known language strings and
|
|
49
50
|
* their corresponding translations, to boost language replacements or saves
|
|
50
|
-
* redundant replacements in dom tree.
|
|
51
|
+
* redundant replacements in a dom tree.
|
|
51
52
|
* @property lock - Lock instance when updating dom noes.
|
|
52
|
-
* @property _domNodesToFade - Saves all dom nodes
|
|
53
|
-
* @property _replacements - Saves all text nodes
|
|
53
|
+
* @property _domNodesToFade - Saves all dom nodes that should be animated.
|
|
54
|
+
* @property _replacements - Saves all text nodes that should be replaced.
|
|
54
55
|
* @property _textNodesWithKnownTranslation - Saves a mapping of known text
|
|
55
56
|
* snippets to their corresponding $-extended dom nodes.
|
|
56
57
|
*/
|
|
@@ -71,7 +72,7 @@ export declare class WebInternationalization<TElement = HTMLElement, ExternalPro
|
|
|
71
72
|
_replacements: Array<Replacement>;
|
|
72
73
|
_textNodesWithKnownTranslation: Mapping<Array<HTMLItem>>;
|
|
73
74
|
/**
|
|
74
|
-
* Defines dynamic getter and setter interface and resolves configuration
|
|
75
|
+
* Defines dynamic getter and setter interface and resolves a configuration
|
|
75
76
|
* object. Initializes the map implementation.
|
|
76
77
|
*/
|
|
77
78
|
constructor();
|
|
@@ -80,25 +81,26 @@ export declare class WebInternationalization<TElement = HTMLElement, ExternalPro
|
|
|
80
81
|
* configured dom content.
|
|
81
82
|
* @param name - Attribute name which was updates.
|
|
82
83
|
* @param newValue - New updated value.
|
|
84
|
+
* @returns Returns when attribute has been updated.
|
|
83
85
|
*/
|
|
84
|
-
onUpdateAttribute(name: string, newValue: string): void
|
|
86
|
+
onUpdateAttribute(name: string, newValue: string): Promise<void>;
|
|
85
87
|
/**
|
|
86
88
|
* Updates controlled dom elements.
|
|
87
89
|
* @param reason - Why an update has been triggered.
|
|
88
90
|
* @param resolveRendering - Indicates whether rendering should be resolved
|
|
89
91
|
* finally. Should be set to "false" via super calls in inherited render
|
|
90
|
-
* methods which do further dom manipulations
|
|
92
|
+
* methods which do further dom manipulations afterward and resolve the
|
|
91
93
|
* rendering process by their own.
|
|
92
94
|
* @returns A promise resolving when rendering has finished. A promise may
|
|
93
95
|
* be needed for classes inheriting from this class.
|
|
94
96
|
*/
|
|
95
97
|
render(reason?: string, resolveRendering?: boolean): Promise<void>;
|
|
96
98
|
/**
|
|
97
|
-
* Switches the current language to given language. This method is
|
|
98
|
-
* synchronized.
|
|
99
|
+
* Switches the current language to a given language. This method is
|
|
100
|
+
* mutually synchronized.
|
|
99
101
|
* @param language - New language as string or "true". If set to "true" it
|
|
100
102
|
* indicates that the dom tree should be checked again current language to
|
|
101
|
-
* ensure every text node has right content.
|
|
103
|
+
* ensure every text node has the right content.
|
|
102
104
|
* @param ensure - Indicates if a switch effect should be avoided.
|
|
103
105
|
* @returns Returns the current instance wrapped in a promise.
|
|
104
106
|
*/
|
|
@@ -122,12 +124,12 @@ export declare class WebInternationalization<TElement = HTMLElement, ExternalPro
|
|
|
122
124
|
*/
|
|
123
125
|
_handleSwitchEffect(language: string, ensure: boolean): Promise<void>;
|
|
124
126
|
/**
|
|
125
|
-
* Moves pre
|
|
126
|
-
* text to use the same translation algorithm for both.
|
|
127
|
+
* Moves pre-replacement dom nodes into the next dom node behind the
|
|
128
|
+
* translation text to use the same translation algorithm for both.
|
|
127
129
|
*/
|
|
128
130
|
_movePreReplacementNodes(): void;
|
|
129
131
|
/**
|
|
130
|
-
* Collects all text nodes
|
|
132
|
+
* Collects all text nodes that should be replaced later.
|
|
131
133
|
* @param language - New language to use.
|
|
132
134
|
* @param ensure - Indicates if the whole dom should be checked again
|
|
133
135
|
* current language to ensure every text node has right content.
|
|
@@ -150,7 +152,7 @@ export declare class WebInternationalization<TElement = HTMLElement, ExternalPro
|
|
|
150
152
|
*/
|
|
151
153
|
_determineUsefulLanguage(): string;
|
|
152
154
|
/**
|
|
153
|
-
* Registers a text node to change its content with given replacement.
|
|
155
|
+
* Registers a text node to change its content with a given replacement.
|
|
154
156
|
* @param currentTextNodeToTranslate - Text node with content to
|
|
155
157
|
* translate.
|
|
156
158
|
* @param currentDomNode - A comment node with replacement content.
|
|
@@ -160,8 +162,8 @@ export declare class WebInternationalization<TElement = HTMLElement, ExternalPro
|
|
|
160
162
|
*/
|
|
161
163
|
_registerTextNodeToChange(currentTextNodeToTranslate: HTMLItem, currentDomNode: HTMLItem | null, match: Array<string>, currentLanguageDomNode: HTMLItem | null): void;
|
|
162
164
|
/**
|
|
163
|
-
* Checks if last text node has a language indication comment node.
|
|
164
|
-
* function is called after each parsed dom text node.
|
|
165
|
+
* Checks if the last text node has a language indication comment node.
|
|
166
|
+
* This function is called after each parsed dom text node.
|
|
165
167
|
* @param lastTextNodeToTranslate - Last text node to check.
|
|
166
168
|
* @param lastLanguageDomNode - A potential given language indication
|
|
167
169
|
* commend node.
|
|
@@ -172,7 +174,7 @@ export declare class WebInternationalization<TElement = HTMLElement, ExternalPro
|
|
|
172
174
|
*/
|
|
173
175
|
_ensureLastTextNodeHavingLanguageIndicator(lastTextNodeToTranslate: HTMLItem | null, lastLanguageDomNode: HTMLItem | null, ensure: boolean): HTMLItem | null;
|
|
174
176
|
/**
|
|
175
|
-
* Performs the low
|
|
177
|
+
* Performs the low-level text replacements for switching to a given
|
|
176
178
|
* language.
|
|
177
179
|
* @param language - The new language to switch to.
|
|
178
180
|
*/
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";if("undefined"!=typeof module&&null!==module&&"undefined"!==eval("typeof require")&&null!==eval("require")&&"main"in eval("require")&&"undefined"!==eval("typeof require.main")&&null!==eval("require.main")){var ORIGINAL_MAIN_MODULE=module;module!==eval("require.main")&&"paths"in module&&"paths"in eval("require.main")&&"undefined"!=typeof __dirname&&null!==__dirname&&(module.paths=eval("require.main.paths").concat(module.paths.filter((function(path){return eval("require.main.paths").includes(path)}))))}if(null==window)var window="undefined"==typeof global||null===global?{}:global;!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@babel/runtime/helpers/initializerDefineProperty"),require("@babel/runtime/helpers/applyDecoratedDescriptor"),require("@babel/runtime/helpers/initializerWarningHelper"),require("clientnode"),require("clientnode/property-types"),require("web-component-wrapper/decorator"),require("web-component-wrapper/Web"));else if("function"==typeof define&&define.amd)define(["@babel/runtime/helpers/initializerDefineProperty","@babel/runtime/helpers/applyDecoratedDescriptor","@babel/runtime/helpers/initializerWarningHelper","clientnode","clientnode/property-types","web-component-wrapper/decorator","web-component-wrapper/Web"],t);else{var n="object"==typeof exports?t(require("@babel/runtime/helpers/initializerDefineProperty"),require("@babel/runtime/helpers/applyDecoratedDescriptor"),require("@babel/runtime/helpers/initializerWarningHelper"),require("clientnode"),require("clientnode/property-types"),require("web-component-wrapper/decorator"),require("web-component-wrapper/Web")):t(e["@babel/runtime/helpers/initializerDefineProperty"],e["@babel/runtime/helpers/applyDecoratedDescriptor"],e["@babel/runtime/helpers/initializerWarningHelper"],e.clientnode,e["clientnode/property-types"],e["web-component-wrapper/decorator"],e["web-component-wrapper/Web"]);for(var o in n)("object"==typeof exports?exports:e)[o]=n[o]}}(this,((e,t,n,o,i,r,a)=>(()=>{var s=[,t=>{t.exports=e},e=>{e.exports=t},e=>{e.exports=n},e=>{e.exports=o},e=>{e.exports=i},e=>{e.exports=r},e=>{e.exports=a}],l={};function p(e){var t=l[e];if(void 0!==t)return t.exports;var n=l[e]={exports:{}};return s[e](n,n.exports,p),n.exports}p.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return p.d(t,{a:t}),t},p.d=(e,t)=>{for(var n in t)p.o(t,n)&&!p.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},p.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),p.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var c={};p.r(c),p.d(c,{WebInternationalization:()=>k,api:()=>q,default:()=>I,log:()=>E});var u,h,d,g,m,f,w,b,x,N,T,y,L=p(1),_=p.n(L),D=p(2),C=p.n(D),P=(p(3),p(4)),v=p(5),O=p(6),S=p(7);const E=new P.Logger({name:"web-internationalization"});let k=(u=(0,O.property)({type:v.object}),h=(0,O.property)({type:v.func}),d=(0,O.property)({type:v.func}),g=(0,O.property)({type:v.func}),m=(0,O.property)({type:v.func}),y=class e extends S.Web{constructor(){super(),this.self=e,_()(this,"options",w,this),_()(this,"onEnsure",b,this),_()(this,"onSwitch",x,this),_()(this,"onEnsured",N,this),_()(this,"onSwitched",T,this),this.switchLanguageButtonDomNodes=null,this.currentLanguage="enUS",this.knownTranslations={},this.lock=new P.Lock,this._domNodesToFade=[],this._replacements=[],this._textNodesWithKnownTranslation={},this.defineGetterAndSetterInterface()}onUpdateAttribute(e,t){super.onUpdateAttribute(e,t),"options"===e&&this._extendOptions()}async render(e="unknown",t=!0){await super.render(e,!1),0===Object.keys(this.options).length&&this._extendOptions(),this.options.preReplacementLanguagePattern=(0,P.format)(this.options.preReplacementLanguagePattern,this.options.replacementLanguagePattern.substring(1,this.options.replacementLanguagePattern.length-1)),this.options.lockDescription=(0,P.format)(this.options.lockDescription,this.self._name),this.options.sessionDescription=(0,P.format)(this.options.sessionDescription,this.self._name),await this.waitForNestedComponentRendering(),this.switchLanguageButtonDomNodes=this.hostDomNode.querySelectorAll(`a[href^="#${this.options.languageHashPrefix}"]`),this._movePreReplacementNodes(),this.currentLanguage=this._normalizeLanguage(this.options.default);const n=this._determineUsefulLanguage(),o=0===this.options.selection.length;for(const e of this.switchLanguageButtonDomNodes){o&&this.options.selection.push(e.getAttribute("href").substring(`#${this.options.languageHashPrefix}`.length));const t=e=>{e.preventDefault();const t=e.target?.getAttribute("href");t&&this.switch(t.substring(this.options.languageHashPrefix.length+1))};this.addSecureEventListener(e,"click",t)}this.currentLanguage===n?this._switchCurrentLanguageIndicator(n):await this.switch(n,!0),await this.resolveRenderingPromiseIfSet(e,t)}async switch(e,t=!1){if(!0===e||!this.options.selection.length||this.options.selection.includes(e)){if(await this.lock.acquire(this.options.lockDescription),!0===e?(t=!0,e=this.currentLanguage):e=this._normalizeLanguage(e),t&&e!==this.options.default||this.currentLanguage!==e){let n="Switch to";return t&&(n="Ensure"),E.debug(`${n} "${e}".`),this._switchCurrentLanguageIndicator(e),t?await this.onEnsure(this.currentLanguage):await this.onSwitch(this.currentLanguage,e),this._domNodesToFade=[],this._replacements=[],this._collectTextNodesToReplace(e,t),void await this._handleSwitchEffect(e,t)}E.debug(`"${e}" is already current selected language.`),this.lock.release(this.options.lockDescription)}else E.debug(`"${e}" isn't one of the allowed languages.`)}refresh(){return this._movePreReplacementNodes(),this.switch(!0)}_extendOptions(){this.setPropertyValue("options",(0,P.extend)(!0,{},this.self._defaultOptions,this.options))}async _handleSwitchEffect(e,t){const n=this.currentLanguage;if(!t&&this.options.useEffect&&this._domNodesToFade.length>0)return await Promise.all(this._domNodesToFade.map((e=>(0,P.fadeOut)(e)))),this._switchLanguage(e),await Promise.all(this._domNodesToFade.map((e=>(0,P.fadeIn)(e)))),this.onSwitched(n,e),void this.lock.release(this.options.lockDescription);this._switchLanguage(e),t?this.onEnsured(e):this.onSwitched(n,e),this.lock.release(this.options.lockDescription)}_movePreReplacementNodes(){for(const e of(0,P.getAll)(this.hostDomNode)){const t=e.nodeName.toLowerCase();if(this.options.replacementDomNodeNames.includes(t)){["#comment","#text"].includes(t)||(e.style.visibility="hidden");const n=new RegExp(this.options.preReplacementLanguagePattern),o=e.textContent?.match(n);if(e.textContent&&o&&o[0]&&(e.textContent=e.textContent.replace(n,o[1]),e.parentElement)){let t=!1;for(const n of(0,P.getAll)(e.parentElement)){if(t&&(0,P.getText)(n).length>0){n.appendChild(e);break}e===n&&(t=!0)}}}}}_collectTextNodesToReplace(e,t){let n=null,o=null;this.knownTranslations={};for(const i of(0,P.getAll)(this.hostDomNode)){const r=i.nodeName.toLowerCase();if((0!==(0,P.getText)(i).length||i.nodeType===Node.COMMENT_NODE&&""!==(i.nodeValue||"").trim())&&!n?.contains(i)&&!(0,P.getParents)(i).some((e=>this.options.replaceDomNodeNames.concat(this.options.replacementDomNodeNames).includes(e.nodeName.toLowerCase()))))if(this.options.replaceDomNodeNames.includes(r))n=i;else if(n){if(this.options.replacementDomNodeNames.includes(r)){const a="#comment"===r?i.textContent:i.innerHTML,s=a?.match(new RegExp(this.options.replacementLanguagePattern));Array.isArray(s)&&s[1]===e?(this.knownTranslations[(0,P.getText)(n).join(" ")]=s[2].trim(),o=this._ensureLastTextNodeHavingLanguageIndicator(n,o,t),this._registerTextNodeToChange(n,i,s,o),n=null,o=null):i.textContent?.match(new RegExp(this.options.currentLanguagePattern))&&(o=i);continue}n=null,o=null}}this._registerKnownTextNodes()}_registerKnownTextNodes(){this._textNodesWithKnownTranslation={};for(const e of this.hostDomNode.querySelectorAll(this.options.selectors.knownTranslation))for(const t of(0,P.getAll)(e)){const e=(0,P.getText)(t).join(" ");e&&!this.options.replaceDomNodeNames.includes(t.nodeName.toLowerCase())&&!(0,P.getParents)(t).some((e=>this.options.replaceDomNodeNames.includes(e.nodeName.toLowerCase())))&&Object.prototype.hasOwnProperty.call(this.knownTranslations,e)&&(this._domNodesToFade.push(t.parentElement),Object.prototype.hasOwnProperty.call(this._textNodesWithKnownTranslation,this.knownTranslations[e])?this._textNodesWithKnownTranslation[this.knownTranslations[e]].push(t):this._textNodesWithKnownTranslation[this.knownTranslations[e]]=[t])}}_normalizeLanguage(e){for(const[t,n]of Object.entries(this.options.languageMapping))if(n.includes(t.toLowerCase())||n.push(t.toLowerCase()),n.includes(e.toLowerCase()))return t;return this.options.default}_determineUsefulLanguage(){let e;return this.options.initial?e=this.options.initial:Object.prototype.hasOwnProperty.call(P.globalContext,"window")&&(P.globalContext.window?.localStorage.getItem(this.options.sessionDescription)?(e=P.globalContext.window.localStorage.getItem(this.options.sessionDescription),E.debug(`Determine "${e}", because of local storage`,"information.")):P.globalContext.window?.navigator.language&&(e=P.globalContext.window.navigator.language,E.debug(`Determine "${e}", because of browser settings.`))),e||(e=this.options.default,E.debug(`Determine "${e}", because of default option.`)),e=this._normalizeLanguage(e),this.options.selection.length&&!this.options.selection.includes(e)&&(E.debug(`"${e}" isn't one of the allowed languages. Set`,`language to "${this.options.selection[0]}".`),e=this.options.selection[0]),P.globalContext.window?.localStorage&&P.globalContext.window.localStorage.setItem(this.options.sessionDescription,e),e}_registerTextNodeToChange(e,t,n,o){this._domNodesToFade.push(e.parentElement),t&&this._replacements.push({textNodeToTranslate:e,nodeToReplaceWith:t,textToReplaceWith:n[2],currentLanguageDomNode:o})}_ensureLastTextNodeHavingLanguageIndicator(e,t,n){if(e&&!t){let o=this.currentLanguage;n&&(o=this.options.default||this.currentLanguage),(t=P.globalContext.document?.createComment(o)||null)&&e.after(t)}return t}_switchLanguage(e){for(const t of this._replacements){const n=Object.prototype.hasOwnProperty.call(t.textNodeToTranslate,"innerHTML")?t.textNodeToTranslate.innerHTML:t.textNodeToTranslate.textContent,o=n.trim();if(!this.options.templateDelimiter||!o.endsWith(this.options.templateDelimiter.post)&&this.options.templateDelimiter.post){let o=t.currentLanguageDomNode;if(!o){o=document.body;let e=!1;for(const n of(0,P.getAll)(t.textNodeToTranslate.parentElement)){if(e){t.currentLanguageDomNode=o=n;break}n===t.textNodeToTranslate&&(e=!0)}}const i=o.textContent;i&&e===i&&E.warn(`Text node "${t.textToReplaceWith}" is`,`marked as "${i}" and has same`,"translation language as it already is.");const r=t.nodeToReplaceWith.nodeName.toLowerCase();if("#comment"===r)t.textNodeToTranslate.after(P.globalContext.document.createComment(`${i}:${n}`));else{const e=P.globalContext.document.createElement(r);e.textContent=`${i}:${n}`,e.style.visibility="hidden",t.textNodeToTranslate.after(e)}t.textNodeToTranslate.after(P.globalContext.document.createComment(e)),"innerHTML"in t.textNodeToTranslate?t.textNodeToTranslate.innerHTML=t.textToReplaceWith:t.textNodeToTranslate.textContent=t.textToReplaceWith,o.remove(),t.nodeToReplaceWith.remove()}}for(const[e,t]of Object.entries(this._textNodesWithKnownTranslation))for(const n of t)n.textContent=e;P.globalContext.localStorage&&P.globalContext.localStorage.setItem(this.options.sessionDescription,e),this.currentLanguage=e}_switchCurrentLanguageIndicator(e){for(const e of this.hostDomNode.querySelectorAll(`a[href="#${this.options.languageHashPrefix}${this.currentLanguage}"].`+this.options.currentLanguageIndicatorClassName))e.classList.remove(this.options.currentLanguageIndicatorClassName);for(const t of this.hostDomNode.querySelectorAll(`a[href="#${this.options.languageHashPrefix}${e}"]`))t.classList.add(this.options.currentLanguageIndicatorClassName)}},y._name="WebInternationalization",y._defaultOptions={currentLanguageIndicatorClassName:"current",currentLanguagePattern:"^[a-z]{2}[A-Z]{2}$",default:"enUS",useEffect:!0,initial:null,languageHashPrefix:"lang-",languageMapping:{deDE:["de","de_de","de-de","german","deutsch"],enUS:["en","en_us","en-us"],enEN:["en_en","en-en","english"],frFR:["fr","fr_fr","fr-fr","french"]},lockDescription:"{1}Switch",preReplacementLanguagePattern:"^\\|({1})$",replaceDomNodeNames:["#text","lang-replace"],replacementDomNodeNames:["#comment","lang-replacement"],replacementLanguagePattern:"^([a-z]{2}[A-Z]{2}):((.|\\s)*)$",selection:[],selectors:{knownTranslation:".web-internationalization-generated-content"},sessionDescription:"{1}",templateDelimiter:{pre:"{{",post:"}}"}},f=y,w=C()(f.prototype,"options",[u],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return{}}}),b=C()(f.prototype,"onEnsure",[h],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return P.NOOP}}),x=C()(f.prototype,"onSwitch",[d],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return P.NOOP}}),N=C()(f.prototype,"onEnsured",[g],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return P.NOOP}}),T=C()(f.prototype,"onSwitched",[m],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return P.NOOP}}),f);const q={component:k,register:(e=(0,P.camelCaseToDelimited)(k._name))=>{customElements.define(e,k)}},I=k;return P.globalContext.AUTO_DEFINE_WEB_INTERNATIONALIZATION&&q.register(),c})()));
|
|
1
|
+
"use strict";if("undefined"!=typeof module&&null!==module&&"undefined"!==eval("typeof require")&&null!==eval("require")&&"main"in eval("require")&&"undefined"!==eval("typeof require.main")&&null!==eval("require.main")){var ORIGINAL_MAIN_MODULE=module;module!==eval("require.main")&&"paths"in module&&"paths"in eval("require.main")&&"undefined"!=typeof __dirname&&null!==__dirname&&(module.paths=eval("require.main.paths").concat(module.paths.filter((function(path){return eval("require.main.paths").includes(path)}))))}if(null==window)var window="undefined"==typeof global||null===global?{}:global;!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@babel/runtime/helpers/initializerDefineProperty"),require("@babel/runtime/helpers/applyDecoratedDescriptor"),require("@babel/runtime/helpers/initializerWarningHelper"),require("clientnode"),require("clientnode/property-types"),require("web-component-wrapper/decorator"),require("web-component-wrapper/Web"));else if("function"==typeof define&&define.amd)define(["@babel/runtime/helpers/initializerDefineProperty","@babel/runtime/helpers/applyDecoratedDescriptor","@babel/runtime/helpers/initializerWarningHelper","clientnode","clientnode/property-types","web-component-wrapper/decorator","web-component-wrapper/Web"],t);else{var n="object"==typeof exports?t(require("@babel/runtime/helpers/initializerDefineProperty"),require("@babel/runtime/helpers/applyDecoratedDescriptor"),require("@babel/runtime/helpers/initializerWarningHelper"),require("clientnode"),require("clientnode/property-types"),require("web-component-wrapper/decorator"),require("web-component-wrapper/Web")):t(e["@babel/runtime/helpers/initializerDefineProperty"],e["@babel/runtime/helpers/applyDecoratedDescriptor"],e["@babel/runtime/helpers/initializerWarningHelper"],e.clientnode,e["clientnode/property-types"],e["web-component-wrapper/decorator"],e["web-component-wrapper/Web"]);for(var o in n)("object"==typeof exports?exports:e)[o]=n[o]}}(this,((e,t,n,o,i,r,a)=>(()=>{var s=[,t=>{t.exports=e},e=>{e.exports=t},e=>{e.exports=n},e=>{e.exports=o},e=>{e.exports=i},e=>{e.exports=r},e=>{e.exports=a}],l={};function p(e){var t=l[e];if(void 0!==t)return t.exports;var n=l[e]={exports:{}};return s[e](n,n.exports,p),n.exports}p.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return p.d(t,{a:t}),t},p.d=(e,t)=>{for(var n in t)p.o(t,n)&&!p.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},p.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),p.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var c={};p.r(c),p.d(c,{WebInternationalization:()=>k,api:()=>q,default:()=>I,log:()=>E});var u,h,d,g,m,f,w,b,x,N,T,y,L=p(1),_=p.n(L),D=p(2),C=p.n(D),P=(p(3),p(4)),v=p(5),O=p(6),S=p(7);const E=new P.Logger({name:"web-internationalization"});let k=(u=(0,O.property)({type:v.object}),h=(0,O.property)({type:v.func}),d=(0,O.property)({type:v.func}),g=(0,O.property)({type:v.func}),m=(0,O.property)({type:v.func}),y=class e extends S.Web{constructor(){super(),this.self=e,_()(this,"options",w,this),_()(this,"onEnsure",b,this),_()(this,"onSwitch",x,this),_()(this,"onEnsured",N,this),_()(this,"onSwitched",T,this),this.switchLanguageButtonDomNodes=null,this.currentLanguage="enUS",this.knownTranslations={},this.lock=new P.Lock,this._domNodesToFade=[],this._replacements=[],this._textNodesWithKnownTranslation={},this.defineGetterAndSetterInterface()}async onUpdateAttribute(e,t){await super.onUpdateAttribute(e,t),"options"===e&&this._extendOptions()}async render(e="unknown",t=!0){await super.render(e,!1),0===Object.keys(this.options).length&&this._extendOptions(),this.options.preReplacementLanguagePattern=(0,P.format)(this.options.preReplacementLanguagePattern,this.options.replacementLanguagePattern.substring(1,this.options.replacementLanguagePattern.length-1)),this.options.lockDescription=(0,P.format)(this.options.lockDescription,this.self._name),this.options.sessionDescription=(0,P.format)(this.options.sessionDescription,this.self._name),await this.waitForNestedComponentRendering(),this.switchLanguageButtonDomNodes=this.hostDomNode.querySelectorAll(`a[href^="#${this.options.languageHashPrefix}"]`),this._movePreReplacementNodes(),this.currentLanguage=this._normalizeLanguage(this.options.default);const n=this._determineUsefulLanguage(),o=0===this.options.selection.length;for(const e of this.switchLanguageButtonDomNodes){o&&this.options.selection.push(e.getAttribute("href").substring(`#${this.options.languageHashPrefix}`.length));const t=e=>{e.preventDefault();const t=e.target?.getAttribute("href");t&&this.switch(t.substring(this.options.languageHashPrefix.length+1))};this.addSecureEventListener(e,"click",t)}this.currentLanguage===n?this._switchCurrentLanguageIndicator(n):await this.switch(n,!0),await this.resolveRenderingPromiseIfSet(e,t)}async switch(e,t=!1){if(!0===e||!this.options.selection.length||this.options.selection.includes(e)){if(await this.lock.acquire(this.options.lockDescription),!0===e?(t=!0,e=this.currentLanguage):e=this._normalizeLanguage(e),t&&e!==this.options.default||this.currentLanguage!==e){let n="Switch to";return t&&(n="Ensure"),E.debug(`${n} "${e}".`),this._switchCurrentLanguageIndicator(e),t?await this.onEnsure(this.currentLanguage):await this.onSwitch(this.currentLanguage,e),this._domNodesToFade=[],this._replacements=[],this._collectTextNodesToReplace(e,t),void await this._handleSwitchEffect(e,t)}E.debug(`"${e}" is already current selected language.`),this.lock.release(this.options.lockDescription)}else E.debug(`"${e}" isn't one of the allowed languages.`)}refresh(){return this._movePreReplacementNodes(),this.switch(!0)}_extendOptions(){this.setPropertyValue("options",(0,P.extend)(!0,{},this.self._defaultOptions,this.options))}async _handleSwitchEffect(e,t){const n=this.currentLanguage;if(!t&&this.options.useEffect&&this._domNodesToFade.length>0)return await Promise.all(this._domNodesToFade.map((e=>(0,P.fadeOut)(e)))),this._switchLanguage(e),await Promise.all(this._domNodesToFade.map((e=>(0,P.fadeIn)(e)))),this.onSwitched(n,e),void this.lock.release(this.options.lockDescription);this._switchLanguage(e),t?this.onEnsured(e):this.onSwitched(n,e),this.lock.release(this.options.lockDescription)}_movePreReplacementNodes(){for(const e of(0,P.getAll)(this.hostDomNode)){const t=e.nodeName.toLowerCase();if(this.options.replacementDomNodeNames.includes(t)){["#comment","#text"].includes(t)||(e.style.visibility="hidden");const n=new RegExp(this.options.preReplacementLanguagePattern),o=e.textContent?.match(n);if(e.textContent&&o&&o[0]&&(e.textContent=e.textContent.replace(n,o[1]),e.parentElement)){let t=!1;for(const n of(0,P.getAll)(e.parentElement)){if(t&&(0,P.getText)(n).length>0){n.appendChild(e);break}e===n&&(t=!0)}}}}}_collectTextNodesToReplace(e,t){let n=null,o=null;this.knownTranslations={};for(const i of(0,P.getAll)(this.hostDomNode)){const r=i.nodeName.toLowerCase();if((0!==(0,P.getText)(i).length||i.nodeType===Node.COMMENT_NODE&&""!==(i.nodeValue||"").trim())&&!n?.contains(i)&&!(0,P.getParents)(i).some((e=>this.options.replaceDomNodeNames.concat(this.options.replacementDomNodeNames).includes(e.nodeName.toLowerCase()))))if(this.options.replaceDomNodeNames.includes(r))n=i;else if(n){if(this.options.replacementDomNodeNames.includes(r)){const a="#comment"===r?i.textContent:i.innerHTML,s=a?.match(new RegExp(this.options.replacementLanguagePattern));Array.isArray(s)&&s[1]===e?(this.knownTranslations[(0,P.getText)(n).join(" ")]=s[2].trim(),o=this._ensureLastTextNodeHavingLanguageIndicator(n,o,t),this._registerTextNodeToChange(n,i,s,o),n=null,o=null):i.textContent?.match(new RegExp(this.options.currentLanguagePattern))&&(o=i);continue}n=null,o=null}}this._registerKnownTextNodes()}_registerKnownTextNodes(){this._textNodesWithKnownTranslation={};for(const e of this.hostDomNode.querySelectorAll(this.options.selectors.knownTranslation))for(const t of(0,P.getAll)(e)){const e=(0,P.getText)(t).join(" ");e&&!this.options.replaceDomNodeNames.includes(t.nodeName.toLowerCase())&&!(0,P.getParents)(t).some((e=>this.options.replaceDomNodeNames.includes(e.nodeName.toLowerCase())))&&Object.prototype.hasOwnProperty.call(this.knownTranslations,e)&&(this._domNodesToFade.push(t.parentElement),Object.prototype.hasOwnProperty.call(this._textNodesWithKnownTranslation,this.knownTranslations[e])?this._textNodesWithKnownTranslation[this.knownTranslations[e]].push(t):this._textNodesWithKnownTranslation[this.knownTranslations[e]]=[t])}}_normalizeLanguage(e){for(const[t,n]of Object.entries(this.options.languageMapping))if(n.includes(t.toLowerCase())||n.push(t.toLowerCase()),n.includes(e.toLowerCase()))return t;return this.options.default}_determineUsefulLanguage(){let e;return this.options.initial?e=this.options.initial:Object.prototype.hasOwnProperty.call(P.globalContext,"window")&&(P.globalContext.window?.localStorage.getItem(this.options.sessionDescription)?(e=P.globalContext.window.localStorage.getItem(this.options.sessionDescription),E.debug(`Determine "${e}", because of local storage`,"information.")):P.globalContext.window?.navigator.language&&(e=P.globalContext.window.navigator.language,E.debug(`Determine "${e}", because of browser settings.`))),e||(e=this.options.default,E.debug(`Determine "${e}", because of default option.`)),e=this._normalizeLanguage(e),this.options.selection.length&&!this.options.selection.includes(e)&&(E.debug(`"${e}" isn't one of the allowed languages. Set`,`language to "${this.options.selection[0]}".`),e=this.options.selection[0]),P.globalContext.window?.localStorage&&P.globalContext.window.localStorage.setItem(this.options.sessionDescription,e),e}_registerTextNodeToChange(e,t,n,o){this._domNodesToFade.push(e.parentElement),t&&this._replacements.push({textNodeToTranslate:e,nodeToReplaceWith:t,textToReplaceWith:n[2],currentLanguageDomNode:o})}_ensureLastTextNodeHavingLanguageIndicator(e,t,n){if(e&&!t){let o=this.currentLanguage;n&&(o=this.options.default||this.currentLanguage),(t=P.globalContext.document?.createComment(o)||null)&&e.after(t)}return t}_switchLanguage(e){for(const t of this._replacements){const n=Object.prototype.hasOwnProperty.call(t.textNodeToTranslate,"innerHTML")?t.textNodeToTranslate.innerHTML:t.textNodeToTranslate.textContent,o=n.trim();if(!this.options.templateDelimiter||!o.endsWith(this.options.templateDelimiter.post)&&this.options.templateDelimiter.post){let o=t.currentLanguageDomNode;if(!o){o=document.body;let e=!1;for(const n of(0,P.getAll)(t.textNodeToTranslate.parentElement)){if(e){t.currentLanguageDomNode=o=n;break}n===t.textNodeToTranslate&&(e=!0)}}const i=o.textContent;i&&e===i&&E.warn(`Text node "${t.textToReplaceWith}" is`,`marked as "${i}" and has same`,"translation language as it already is.");const r=t.nodeToReplaceWith.nodeName.toLowerCase();if("#comment"===r)t.textNodeToTranslate.after(P.globalContext.document.createComment(`${i}:${n}`));else{const e=P.globalContext.document.createElement(r);e.textContent=`${i}:${n}`,e.style.visibility="hidden",t.textNodeToTranslate.after(e)}t.textNodeToTranslate.after(P.globalContext.document.createComment(e)),"innerHTML"in t.textNodeToTranslate?t.textNodeToTranslate.innerHTML=t.textToReplaceWith:t.textNodeToTranslate.textContent=t.textToReplaceWith,o.remove(),t.nodeToReplaceWith.remove()}}for(const[e,t]of Object.entries(this._textNodesWithKnownTranslation))for(const n of t)n.textContent=e;P.globalContext.localStorage&&P.globalContext.localStorage.setItem(this.options.sessionDescription,e),this.currentLanguage=e}_switchCurrentLanguageIndicator(e){for(const e of this.hostDomNode.querySelectorAll(`a[href="#${this.options.languageHashPrefix}${this.currentLanguage}"].`+this.options.currentLanguageIndicatorClassName))e.classList.remove(this.options.currentLanguageIndicatorClassName);for(const t of this.hostDomNode.querySelectorAll(`a[href="#${this.options.languageHashPrefix}${e}"]`))t.classList.add(this.options.currentLanguageIndicatorClassName)}},y._name="WebInternationalization",y._defaultOptions={currentLanguageIndicatorClassName:"current",currentLanguagePattern:"^[a-z]{2}[A-Z]{2}$",default:"enUS",useEffect:!0,initial:null,languageHashPrefix:"lang-",languageMapping:{deDE:["de","de_de","de-de","german","deutsch"],enUS:["en","en_us","en-us"],enEN:["en_en","en-en","english"],frFR:["fr","fr_fr","fr-fr","french"]},lockDescription:"{1}Switch",preReplacementLanguagePattern:"^\\|({1})$",replaceDomNodeNames:["#text","lang-replace"],replacementDomNodeNames:["#comment","lang-replacement"],replacementLanguagePattern:"^([a-z]{2}[A-Z]{2}):((.|\\s)*)$",selection:[],selectors:{knownTranslation:".web-internationalization-generated-content"},sessionDescription:"{1}",templateDelimiter:{pre:"{{",post:"}}"}},f=y,w=C()(f.prototype,"options",[u],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return{}}}),b=C()(f.prototype,"onEnsure",[h],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return P.NOOP}}),x=C()(f.prototype,"onSwitch",[d],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return P.NOOP}}),N=C()(f.prototype,"onEnsured",[g],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return P.NOOP}}),T=C()(f.prototype,"onSwitched",[m],{configurable:!0,enumerable:!0,writable:!0,initializer:function(){return P.NOOP}}),f);const q={component:k,register:(e=(0,P.camelCaseToDelimited)(k._name))=>{customElements.define(e,k)}},I=k;return P.globalContext.AUTO_DEFINE_WEB_INTERNATIONALIZATION&&q.register(),c})()));
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.0.
|
|
2
|
+
"version": "2.0.22",
|
|
3
3
|
"name": "web-internationalization",
|
|
4
4
|
"description": "Expand your audience",
|
|
5
5
|
"keywords": [
|
|
@@ -57,11 +57,11 @@
|
|
|
57
57
|
"@types/ejs": "^3.1.5",
|
|
58
58
|
"@types/html-minifier": "^4.0.6",
|
|
59
59
|
"@types/imagemin": "^9.0.1",
|
|
60
|
-
"@types/jsdom": "^28.0.
|
|
61
|
-
"@types/node": "^25.
|
|
60
|
+
"@types/jsdom": "^28.0.2",
|
|
61
|
+
"@types/node": "^25.7.0",
|
|
62
62
|
"@types/webpack-env": "^1.18.8",
|
|
63
|
-
"@typescript-eslint/eslint-plugin": "^8.59.
|
|
64
|
-
"@typescript-eslint/parser": "^8.59.
|
|
63
|
+
"@typescript-eslint/eslint-plugin": "^8.59.3",
|
|
64
|
+
"@typescript-eslint/parser": "^8.59.3",
|
|
65
65
|
"@webcomponents/webcomponentsjs": "^2.8.0",
|
|
66
66
|
"clientnode": "^4.0.1395",
|
|
67
67
|
"eslint": "^10.3.0",
|
|
@@ -72,11 +72,11 @@
|
|
|
72
72
|
"jest": "^30.3.0",
|
|
73
73
|
"jsdoc": "^4.0.5",
|
|
74
74
|
"prop-types": "^15.8.1",
|
|
75
|
-
"typescript-eslint": "^8.59.
|
|
76
|
-
"web-component-wrapper": "^0.0.
|
|
75
|
+
"typescript-eslint": "^8.59.3",
|
|
76
|
+
"web-component-wrapper": "^0.0.591",
|
|
77
77
|
"web-documentation": "^1.0.9",
|
|
78
78
|
"weboptimizer": "^3.0.22",
|
|
79
|
-
"webpack-dev-server": "^5.2.
|
|
79
|
+
"webpack-dev-server": "^5.2.4"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|
|
82
82
|
"@babel/runtime": "*",
|