react-localization 2.0.3 → 2.0.4
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.
@@ -0,0 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* Simple module to localize the React interface using the same syntax
|
3
|
+
* used in the ReactNativeLocalization module
|
4
|
+
* (https://github.com/stefalda/ReactNativeLocalization)
|
5
|
+
*
|
6
|
+
* Originally developed by Stefano Falda (stefano.falda@gmail.com)
|
7
|
+
*
|
8
|
+
* It uses a call to the Navigator/Browser object to get the current interface language,
|
9
|
+
* then display the correct language strings or the default language (the first
|
10
|
+
* one if a match is not found).
|
11
|
+
*
|
12
|
+
* This library has been refactored to use the newly created localized-strings package so to
|
13
|
+
* unify the code and make it easier to mantain
|
14
|
+
*
|
15
|
+
* How to use:
|
16
|
+
* Check the instructions at:
|
17
|
+
* https://github.com/stefalda/react-localization
|
18
|
+
*/
|
19
|
+
import LocalizedStrings from 'localized-strings';
|
20
|
+
export default LocalizedStrings;
|
@@ -0,0 +1,188 @@
|
|
1
|
+
import c from "react";
|
2
|
+
var p = Object.defineProperty, _ = (n, e, t) => e in n ? p(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t, o = (n, e, t) => _(n, typeof e != "symbol" ? e + "" : e, t);
|
3
|
+
function d() {
|
4
|
+
const n = "en-US";
|
5
|
+
if (typeof navigator > "u")
|
6
|
+
return n;
|
7
|
+
const e = navigator;
|
8
|
+
if (e) {
|
9
|
+
if (e.language)
|
10
|
+
return e.language;
|
11
|
+
if (e.languages && e.languages[0])
|
12
|
+
return e.languages[0];
|
13
|
+
if ("userLanguage" in e)
|
14
|
+
return e.userLanguage;
|
15
|
+
if ("browserLanguage" in e)
|
16
|
+
return e.browserLanguage;
|
17
|
+
}
|
18
|
+
return n;
|
19
|
+
}
|
20
|
+
function L(n, e) {
|
21
|
+
if (e[n]) return n;
|
22
|
+
const t = n.indexOf("-"), a = t >= 0 ? n.substring(0, t) : n;
|
23
|
+
return e[a] ? a : Object.keys(e)[0];
|
24
|
+
}
|
25
|
+
function b(n) {
|
26
|
+
const e = [
|
27
|
+
"_interfaceLanguage",
|
28
|
+
"_language",
|
29
|
+
"_defaultLanguage",
|
30
|
+
"_defaultLanguageFirstLevelKeys",
|
31
|
+
"_props"
|
32
|
+
];
|
33
|
+
n.forEach((t) => {
|
34
|
+
if (e.indexOf(t) !== -1)
|
35
|
+
throw new Error(`${t} cannot be used as a key. It is a reserved word.`);
|
36
|
+
});
|
37
|
+
}
|
38
|
+
function y(n) {
|
39
|
+
let e = "";
|
40
|
+
const t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
41
|
+
for (let a = 0; a < n; a += 1)
|
42
|
+
e += t.charAt(Math.floor(Math.random() * t.length));
|
43
|
+
return e;
|
44
|
+
}
|
45
|
+
const l = /(\{[\d|\w]+\})/, f = /(\$ref\{[\w|.]+\})/;
|
46
|
+
class m {
|
47
|
+
/**
|
48
|
+
* Constructor used to provide the strings objects in various language and the optional callback to get
|
49
|
+
* the interface language
|
50
|
+
* @param props - the strings object
|
51
|
+
* @param options - configuration options
|
52
|
+
*/
|
53
|
+
constructor(e, t) {
|
54
|
+
o(this, "_opts"), o(this, "_interfaceLanguage"), o(this, "_language"), o(this, "_defaultLanguage"), o(this, "_defaultLanguageFirstLevelKeys"), o(this, "_props"), o(this, "_availableLanguages"), typeof t == "function" && (t = { customLanguageInterface: t }), this._opts = {
|
55
|
+
customLanguageInterface: d,
|
56
|
+
pseudo: !1,
|
57
|
+
pseudoMultipleLanguages: !1,
|
58
|
+
logsEnabled: !0,
|
59
|
+
...t
|
60
|
+
}, this._interfaceLanguage = this._opts.customLanguageInterface(), this._language = this._interfaceLanguage, this.setContent(e);
|
61
|
+
}
|
62
|
+
/**
|
63
|
+
* Set the strings objects based on the parameter passed in the constructor
|
64
|
+
*/
|
65
|
+
setContent(e) {
|
66
|
+
const [t] = Object.keys(e);
|
67
|
+
this._defaultLanguage = t, this._defaultLanguageFirstLevelKeys = [], this._props = e, b(Object.keys(e[this._defaultLanguage])), Object.keys(this._props[this._defaultLanguage]).forEach((a) => {
|
68
|
+
typeof this._props[this._defaultLanguage][a] == "string" && this._defaultLanguageFirstLevelKeys.push(a);
|
69
|
+
}), this.setLanguage(this._interfaceLanguage), this._opts.pseudo && this._pseudoAllValues(this._props);
|
70
|
+
}
|
71
|
+
/**
|
72
|
+
* Replace all strings to pseudo value
|
73
|
+
*/
|
74
|
+
_pseudoAllValues(e) {
|
75
|
+
Object.keys(e).forEach((t) => {
|
76
|
+
if (typeof e[t] == "object")
|
77
|
+
this._pseudoAllValues(e[t]);
|
78
|
+
else if (typeof e[t] == "string") {
|
79
|
+
if (e[t].indexOf("[") === 0 && e[t].lastIndexOf("]") === e[t].length - 1)
|
80
|
+
return;
|
81
|
+
const a = e[t].split(" ");
|
82
|
+
for (let s = 0; s < a.length; s += 1) {
|
83
|
+
if (a[s].match(l) || a[s].match(f))
|
84
|
+
continue;
|
85
|
+
let r = a[s].length;
|
86
|
+
this._opts.pseudoMultipleLanguages && (r = Math.floor(r * 1.4)), a[s] = y(r);
|
87
|
+
}
|
88
|
+
e[t] = `[${a.join(" ")}]`;
|
89
|
+
}
|
90
|
+
});
|
91
|
+
}
|
92
|
+
/**
|
93
|
+
* Can be used from outside the class to force a particular language
|
94
|
+
* independently from the interface one
|
95
|
+
*/
|
96
|
+
setLanguage(e) {
|
97
|
+
const t = L(e, this._props), a = Object.keys(this._props)[0];
|
98
|
+
if (this._language = t, this._props[t]) {
|
99
|
+
for (const r of this._defaultLanguageFirstLevelKeys)
|
100
|
+
delete this[r];
|
101
|
+
let s = { ...this._props[this._language] };
|
102
|
+
Object.keys(s).forEach((r) => {
|
103
|
+
this[r] = s[r];
|
104
|
+
}), a !== this._language && (s = this._props[a], this._fallbackValues(s, this));
|
105
|
+
}
|
106
|
+
}
|
107
|
+
/**
|
108
|
+
* Load fallback values for missing translations
|
109
|
+
*/
|
110
|
+
_fallbackValues(e, t) {
|
111
|
+
Object.keys(e).forEach((a) => {
|
112
|
+
Object.prototype.hasOwnProperty.call(e, a) && !t[a] && t[a] !== "" ? (t[a] = e[a], this._opts.logsEnabled && console.log(
|
113
|
+
`🚧 👷 key '${a}' not found in localizedStrings for language ${this._language} 🚧`
|
114
|
+
)) : typeof t[a] != "string" && this._fallbackValues(e[a], t[a]);
|
115
|
+
});
|
116
|
+
}
|
117
|
+
getLanguage() {
|
118
|
+
return this._language;
|
119
|
+
}
|
120
|
+
getInterfaceLanguage() {
|
121
|
+
return this._interfaceLanguage;
|
122
|
+
}
|
123
|
+
getAvailableLanguages() {
|
124
|
+
return this._availableLanguages || (this._availableLanguages = Object.keys(this._props)), this._availableLanguages;
|
125
|
+
}
|
126
|
+
formatString(e, ...t) {
|
127
|
+
let a = e || "";
|
128
|
+
return typeof a == "string" && (a = this.getString(e, null, !0) || a), a.split(f).filter(Boolean).map((s) => {
|
129
|
+
if (s.match(f)) {
|
130
|
+
const r = s.slice(5, -1);
|
131
|
+
return this.getString(r) || (this._opts.logsEnabled && console.log(
|
132
|
+
`No Localization ref found for '${s}' in string '${e}'`
|
133
|
+
), `$ref(id:${r})`);
|
134
|
+
}
|
135
|
+
return s;
|
136
|
+
}).join("").split(l).filter(Boolean).map((s) => {
|
137
|
+
if (s.match(l)) {
|
138
|
+
const r = s.slice(1, -1);
|
139
|
+
let i = t[r];
|
140
|
+
return i === void 0 && t[0] && (i = t[0][r]), i;
|
141
|
+
}
|
142
|
+
return s;
|
143
|
+
}).join("");
|
144
|
+
}
|
145
|
+
getString(e, t, a = !1) {
|
146
|
+
try {
|
147
|
+
let s = this._props[t || this._language];
|
148
|
+
const r = e.split(".");
|
149
|
+
for (const i of r) {
|
150
|
+
if (s[i] === void 0)
|
151
|
+
throw new Error(i);
|
152
|
+
s = s[i];
|
153
|
+
}
|
154
|
+
return s;
|
155
|
+
} catch (s) {
|
156
|
+
!a && this._opts.logsEnabled && console.log(
|
157
|
+
`No localization found for key '${e}' and language '${t}', failed on ${s.message}`
|
158
|
+
);
|
159
|
+
}
|
160
|
+
return null;
|
161
|
+
}
|
162
|
+
getContent() {
|
163
|
+
return this._props;
|
164
|
+
}
|
165
|
+
}
|
166
|
+
const h = /(\{[\d|\w]+\})/;
|
167
|
+
m.prototype.formatString = (n, ...e) => {
|
168
|
+
let t = !1;
|
169
|
+
const a = (n || "").split(h).filter((s) => !!s).map((s, r) => {
|
170
|
+
if (s.match(h)) {
|
171
|
+
const i = s.slice(1, -1);
|
172
|
+
let u = e[i];
|
173
|
+
if (u == null) {
|
174
|
+
const g = e[0] ? e[0][i] : void 0;
|
175
|
+
if (g !== void 0)
|
176
|
+
u = g;
|
177
|
+
else
|
178
|
+
return u;
|
179
|
+
}
|
180
|
+
return c.isValidElement(u) ? (t = !0, c.Children.toArray(u).map((g) => ({ ...g, key: r.toString() }))) : u;
|
181
|
+
}
|
182
|
+
return s;
|
183
|
+
});
|
184
|
+
return t ? a : a.join("");
|
185
|
+
};
|
186
|
+
export {
|
187
|
+
m as default
|
188
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
(function(o,g){typeof exports=="object"&&typeof module<"u"?module.exports=g(require("react")):typeof define=="function"&&define.amd?define(["react"],g):(o=typeof globalThis<"u"?globalThis:o||self,o.ReactLocalization=g(o.React))})(this,function(o){"use strict";var g=Object.defineProperty,_=(s,e,t)=>e in s?g(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,u=(s,e,t)=>_(s,typeof e!="symbol"?e+"":e,t);function L(){const s="en-US";if(typeof navigator>"u")return s;const e=navigator;if(e){if(e.language)return e.language;if(e.languages&&e.languages[0])return e.languages[0];if("userLanguage"in e)return e.userLanguage;if("browserLanguage"in e)return e.browserLanguage}return s}function y(s,e){if(e[s])return s;const t=s.indexOf("-"),a=t>=0?s.substring(0,t):s;return e[a]?a:Object.keys(e)[0]}function b(s){const e=["_interfaceLanguage","_language","_defaultLanguage","_defaultLanguageFirstLevelKeys","_props"];s.forEach(t=>{if(e.indexOf(t)!==-1)throw new Error(`${t} cannot be used as a key. It is a reserved word.`)})}function m(s){let e="";const t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(let a=0;a<s;a+=1)e+=t.charAt(Math.floor(Math.random()*t.length));return e}const c=/(\{[\d|\w]+\})/,h=/(\$ref\{[\w|.]+\})/;class p{constructor(e,t){u(this,"_opts"),u(this,"_interfaceLanguage"),u(this,"_language"),u(this,"_defaultLanguage"),u(this,"_defaultLanguageFirstLevelKeys"),u(this,"_props"),u(this,"_availableLanguages"),typeof t=="function"&&(t={customLanguageInterface:t}),this._opts={customLanguageInterface:L,pseudo:!1,pseudoMultipleLanguages:!1,logsEnabled:!0,...t},this._interfaceLanguage=this._opts.customLanguageInterface(),this._language=this._interfaceLanguage,this.setContent(e)}setContent(e){const[t]=Object.keys(e);this._defaultLanguage=t,this._defaultLanguageFirstLevelKeys=[],this._props=e,b(Object.keys(e[this._defaultLanguage])),Object.keys(this._props[this._defaultLanguage]).forEach(a=>{typeof this._props[this._defaultLanguage][a]=="string"&&this._defaultLanguageFirstLevelKeys.push(a)}),this.setLanguage(this._interfaceLanguage),this._opts.pseudo&&this._pseudoAllValues(this._props)}_pseudoAllValues(e){Object.keys(e).forEach(t=>{if(typeof e[t]=="object")this._pseudoAllValues(e[t]);else if(typeof e[t]=="string"){if(e[t].indexOf("[")===0&&e[t].lastIndexOf("]")===e[t].length-1)return;const a=e[t].split(" ");for(let n=0;n<a.length;n+=1){if(a[n].match(c)||a[n].match(h))continue;let i=a[n].length;this._opts.pseudoMultipleLanguages&&(i=Math.floor(i*1.4)),a[n]=m(i)}e[t]=`[${a.join(" ")}]`}})}setLanguage(e){const t=y(e,this._props),a=Object.keys(this._props)[0];if(this._language=t,this._props[t]){for(const i of this._defaultLanguageFirstLevelKeys)delete this[i];let n={...this._props[this._language]};Object.keys(n).forEach(i=>{this[i]=n[i]}),a!==this._language&&(n=this._props[a],this._fallbackValues(n,this))}}_fallbackValues(e,t){Object.keys(e).forEach(a=>{Object.prototype.hasOwnProperty.call(e,a)&&!t[a]&&t[a]!==""?(t[a]=e[a],this._opts.logsEnabled&&console.log(`🚧 👷 key '${a}' not found in localizedStrings for language ${this._language} 🚧`)):typeof t[a]!="string"&&this._fallbackValues(e[a],t[a])})}getLanguage(){return this._language}getInterfaceLanguage(){return this._interfaceLanguage}getAvailableLanguages(){return this._availableLanguages||(this._availableLanguages=Object.keys(this._props)),this._availableLanguages}formatString(e,...t){let a=e||"";return typeof a=="string"&&(a=this.getString(e,null,!0)||a),a.split(h).filter(Boolean).map(n=>{if(n.match(h)){const i=n.slice(5,-1);return this.getString(i)||(this._opts.logsEnabled&&console.log(`No Localization ref found for '${n}' in string '${e}'`),`$ref(id:${i})`)}return n}).join("").split(c).filter(Boolean).map(n=>{if(n.match(c)){const i=n.slice(1,-1);let r=t[i];return r===void 0&&t[0]&&(r=t[0][i]),r}return n}).join("")}getString(e,t,a=!1){try{let n=this._props[t||this._language];const i=e.split(".");for(const r of i){if(n[r]===void 0)throw new Error(r);n=n[r]}return n}catch(n){!a&&this._opts.logsEnabled&&console.log(`No localization found for key '${e}' and language '${t}', failed on ${n.message}`)}return null}getContent(){return this._props}}const d=/(\{[\d|\w]+\})/;return p.prototype.formatString=(s,...e)=>{let t=!1;const a=(s||"").split(d).filter(n=>!!n).map((n,i)=>{if(n.match(d)){const r=n.slice(1,-1);let l=e[r];if(l==null){const f=e[0]?e[0][r]:void 0;if(f!==void 0)l=f;else return l}return o.isValidElement(l)?(t=!0,o.Children.toArray(l).map(f=>({...f,key:i.toString()}))):l}return n});return t?a:a.join("")},p});
|
package/package.json
CHANGED
@@ -1,24 +1,26 @@
|
|
1
1
|
{
|
2
2
|
"name": "react-localization",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.4",
|
4
4
|
"description": "Simple module to localize the React interface using the same syntax used in the ReactNativeLocalization module",
|
5
5
|
"type": "module",
|
6
|
-
"main": "./
|
7
|
-
"module": "./
|
8
|
-
"types": "./
|
6
|
+
"main": "./lib/react-localization.umd.js",
|
7
|
+
"module": "./lib/react-localization.es.js",
|
8
|
+
"types": "./lib/LocalizedStrings.d.ts",
|
9
9
|
"exports": {
|
10
10
|
".": {
|
11
|
-
"import": "./
|
11
|
+
"import": "./lib/react-localization.es.js"
|
12
12
|
}
|
13
13
|
},
|
14
14
|
"files": [
|
15
|
-
"
|
15
|
+
"lib"
|
16
16
|
],
|
17
17
|
"scripts": {
|
18
18
|
"dev": "vite",
|
19
|
-
"build": "tsc && vite build",
|
20
19
|
"test": "vitest",
|
21
|
-
"prepare": "npm run build"
|
20
|
+
"prepare": "npm run build",
|
21
|
+
"build:vite": "vite build",
|
22
|
+
"build:types": "tsc --emitDeclarationOnly",
|
23
|
+
"build": "npm run build:vite && npm run build:types"
|
22
24
|
},
|
23
25
|
"repository": {
|
24
26
|
"type": "git",
|
@@ -56,7 +58,7 @@
|
|
56
58
|
"jsdom": "^26.0.0"
|
57
59
|
},
|
58
60
|
"dependencies": {
|
59
|
-
"localized-strings": "^
|
61
|
+
"localized-strings": "^2.0.3"
|
60
62
|
},
|
61
63
|
"peerDependenciesMeta": {
|
62
64
|
"react": {
|
@@ -1,216 +0,0 @@
|
|
1
|
-
import f from "react";
|
2
|
-
function h() {
|
3
|
-
const i = "en-US";
|
4
|
-
if (typeof navigator > "u")
|
5
|
-
return i;
|
6
|
-
const e = navigator;
|
7
|
-
if (e) {
|
8
|
-
if (e.language)
|
9
|
-
return e.language;
|
10
|
-
if (e.languages && e.languages[0])
|
11
|
-
return e.languages[0];
|
12
|
-
if (e.userLanguage)
|
13
|
-
return e.userLanguage;
|
14
|
-
if (e.browserLanguage)
|
15
|
-
return e.browserLanguage;
|
16
|
-
}
|
17
|
-
return i;
|
18
|
-
}
|
19
|
-
function d(i, e) {
|
20
|
-
if (e[i]) return i;
|
21
|
-
const t = i.indexOf("-"), a = t >= 0 ? i.substring(0, t) : i;
|
22
|
-
return e[a] ? a : Object.keys(e)[0];
|
23
|
-
}
|
24
|
-
function _(i) {
|
25
|
-
const e = ["_interfaceLanguage", "_language", "_defaultLanguage", "_defaultLanguageFirstLevelKeys", "_props"];
|
26
|
-
i.forEach((t) => {
|
27
|
-
if (e.indexOf(t) !== -1)
|
28
|
-
throw new Error(`${t} cannot be used as a key. It is a reserved word.`);
|
29
|
-
});
|
30
|
-
}
|
31
|
-
function p(i) {
|
32
|
-
let e = "";
|
33
|
-
const t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
34
|
-
for (let a = 0; a < i; a += 1) e += t.charAt(Math.floor(Math.random() * t.length));
|
35
|
-
return e;
|
36
|
-
}
|
37
|
-
const g = /(\{[\d|\w]+\})/, o = /(\$ref\{[\w|.]+\})/;
|
38
|
-
class L {
|
39
|
-
/**
|
40
|
-
* Constructor used to provide the strings objects in various language and the optional callback to get
|
41
|
-
* the interface language
|
42
|
-
* @param {*} props - the strings object
|
43
|
-
* @param {Function} options.customLanguageInterface - the optional method to use to get the InterfaceLanguage
|
44
|
-
* @param {Boolean} options.pseudo - convert all strings to pseudo, helpful when implementing
|
45
|
-
* @param {Boolean} options.pseudoMultipleLanguages - add 40% to pseudo, helps with translations in the future
|
46
|
-
* @param {Boolean} options.logsEnabled - Enable/Disable console.log outputs (default=true)
|
47
|
-
*/
|
48
|
-
constructor(e, t) {
|
49
|
-
typeof t == "function" && (t = {
|
50
|
-
customLanguageInterface: t
|
51
|
-
}), this._opts = Object.assign({}, {
|
52
|
-
customLanguageInterface: h,
|
53
|
-
pseudo: !1,
|
54
|
-
pseudoMultipleLanguages: !1,
|
55
|
-
logsEnabled: !0
|
56
|
-
}, t), this._interfaceLanguage = this._opts.customLanguageInterface(), this._language = this._interfaceLanguage, this.setContent(e);
|
57
|
-
}
|
58
|
-
/**
|
59
|
-
* Set the strings objects based on the parameter passed in the constructor
|
60
|
-
* @param {*} props
|
61
|
-
*/
|
62
|
-
setContent(e) {
|
63
|
-
const [t] = Object.keys(e);
|
64
|
-
this._defaultLanguage = t, this._defaultLanguageFirstLevelKeys = [], this._props = e, _(Object.keys(e[this._defaultLanguage])), Object.keys(this._props[this._defaultLanguage]).forEach((a) => {
|
65
|
-
typeof this._props[this._defaultLanguage][a] == "string" && this._defaultLanguageFirstLevelKeys.push(a);
|
66
|
-
}), this.setLanguage(this._interfaceLanguage), this._opts.pseudo && this._pseudoAllValues(this._props);
|
67
|
-
}
|
68
|
-
/**
|
69
|
-
* Replace all strings to pseudo value
|
70
|
-
* @param {Object} obj - Loopable object
|
71
|
-
*/
|
72
|
-
_pseudoAllValues(e) {
|
73
|
-
Object.keys(e).forEach((t) => {
|
74
|
-
if (typeof e[t] == "object")
|
75
|
-
this._pseudoAllValues(e[t]);
|
76
|
-
else if (typeof e[t] == "string") {
|
77
|
-
if (e[t].indexOf("[") === 0 && e[t].lastIndexOf("]") === e[t].length - 1)
|
78
|
-
return;
|
79
|
-
const a = e[t].split(" ");
|
80
|
-
for (let s = 0; s < a.length; s += 1)
|
81
|
-
if (!a[s].match(g)) {
|
82
|
-
if (!a[s].match(o)) {
|
83
|
-
let n = a[s].length;
|
84
|
-
this._opts.pseudoMultipleLanguages && (n = parseInt(n * 1.4, 10)), a[s] = p(n);
|
85
|
-
}
|
86
|
-
}
|
87
|
-
e[t] = `[${a.join(" ")}]`;
|
88
|
-
}
|
89
|
-
});
|
90
|
-
}
|
91
|
-
/**
|
92
|
-
* Can be used from ouside the class to force a particular language
|
93
|
-
* indipendently from the interface one
|
94
|
-
* @param {*} language
|
95
|
-
*/
|
96
|
-
setLanguage(e) {
|
97
|
-
const t = d(e, this._props), a = Object.keys(this._props)[0];
|
98
|
-
if (this._language = t, this._props[t]) {
|
99
|
-
for (let n = 0; n < this._defaultLanguageFirstLevelKeys.length; n += 1)
|
100
|
-
delete this[this._defaultLanguageFirstLevelKeys[n]];
|
101
|
-
let s = Object.assign({}, this._props[this._language]);
|
102
|
-
Object.keys(s).forEach((n) => {
|
103
|
-
this[n] = s[n];
|
104
|
-
}), a !== this._language && (s = this._props[a], this._fallbackValues(s, this));
|
105
|
-
}
|
106
|
-
}
|
107
|
-
/**
|
108
|
-
* Load fallback values for missing translations
|
109
|
-
* @param {*} defaultStrings
|
110
|
-
* @param {*} strings
|
111
|
-
*/
|
112
|
-
_fallbackValues(e, t) {
|
113
|
-
Object.keys(e).forEach((a) => {
|
114
|
-
Object.prototype.hasOwnProperty.call(e, a) && !t[a] && t[a] !== "" ? (t[a] = e[a], this._opts.logsEnabled && console.log(`🚧 👷 key '${a}' not found in localizedStrings for language ${this._language} 🚧`)) : typeof t[a] != "string" && this._fallbackValues(e[a], t[a]);
|
115
|
-
});
|
116
|
-
}
|
117
|
-
/**
|
118
|
-
* The current language displayed (could differ from the interface language
|
119
|
-
* if it has been forced manually and a matching translation has been found)
|
120
|
-
*/
|
121
|
-
getLanguage() {
|
122
|
-
return this._language;
|
123
|
-
}
|
124
|
-
/**
|
125
|
-
* The current interface language (could differ from the language displayed)
|
126
|
-
*/
|
127
|
-
getInterfaceLanguage() {
|
128
|
-
return this._interfaceLanguage;
|
129
|
-
}
|
130
|
-
/**
|
131
|
-
* Return an array containing the available languages passed as props in the constructor
|
132
|
-
*/
|
133
|
-
getAvailableLanguages() {
|
134
|
-
return this._availableLanguages || (this._availableLanguages = [], Object.keys(this._props).forEach((e) => {
|
135
|
-
this._availableLanguages.push(e);
|
136
|
-
})), this._availableLanguages;
|
137
|
-
}
|
138
|
-
// Format the passed string replacing the numbered or tokenized placeholders
|
139
|
-
// eg. 1: I'd like some {0} and {1}, or just {0}
|
140
|
-
// eg. 2: I'd like some {bread} and {butter}, or just {bread}
|
141
|
-
// eg. 3: I'd like some $ref{bread} and $ref{butter}, or just $ref{bread}
|
142
|
-
// Use example:
|
143
|
-
// eg. 1: strings.formatString(strings.question, strings.bread, strings.butter)
|
144
|
-
// eg. 2: strings.formatString(strings.question, { bread: strings.bread, butter: strings.butter })
|
145
|
-
// eg. 3: strings.formatString(strings.question)
|
146
|
-
formatString(e, ...t) {
|
147
|
-
let a = e || "";
|
148
|
-
return typeof a == "string" && (a = this.getString(e, null, !0) || a), a.split(o).filter((n) => !!n).map((n) => {
|
149
|
-
if (n.match(o)) {
|
150
|
-
const l = n.slice(5, -1), r = this.getString(l);
|
151
|
-
return r || (this._opts.logsEnabled && console.log(`No Localization ref found for '${n}' in string '${e}'`), `$ref(id:${l})`);
|
152
|
-
}
|
153
|
-
return n;
|
154
|
-
}).join("").split(g).filter((n) => !!n).map((n) => {
|
155
|
-
if (n.match(g)) {
|
156
|
-
const l = n.slice(1, -1);
|
157
|
-
let r = t[l];
|
158
|
-
if (r === void 0) {
|
159
|
-
const u = t[0][l];
|
160
|
-
if (u !== void 0)
|
161
|
-
r = u;
|
162
|
-
else
|
163
|
-
return r;
|
164
|
-
}
|
165
|
-
return r;
|
166
|
-
}
|
167
|
-
return n;
|
168
|
-
}).join("");
|
169
|
-
}
|
170
|
-
// Return a string with the passed key in a different language or defalt if not set
|
171
|
-
// We allow deep . notation for finding strings
|
172
|
-
getString(e, t, a = !1) {
|
173
|
-
try {
|
174
|
-
let s = this._props[t || this._language];
|
175
|
-
const n = e.split(".");
|
176
|
-
for (let l = 0; l < n.length; l += 1) {
|
177
|
-
if (s[n[l]] === void 0)
|
178
|
-
throw Error(n[l]);
|
179
|
-
s = s[n[l]];
|
180
|
-
}
|
181
|
-
return s;
|
182
|
-
} catch (s) {
|
183
|
-
!a && this._opts.logsEnabled && console.log(`No localization found for key '${e}' and language '${t}', failed on ${s.message}`);
|
184
|
-
}
|
185
|
-
return null;
|
186
|
-
}
|
187
|
-
/**
|
188
|
-
* The current props (locale object)
|
189
|
-
*/
|
190
|
-
getContent() {
|
191
|
-
return this._props;
|
192
|
-
}
|
193
|
-
}
|
194
|
-
const c = /(\{[\d|\w]+\})/;
|
195
|
-
L.prototype.formatString = (i, ...e) => {
|
196
|
-
let t = !1;
|
197
|
-
const a = (i || "").split(c).filter((s) => !!s).map((s, n) => {
|
198
|
-
if (s.match(c)) {
|
199
|
-
const l = s.slice(1, -1);
|
200
|
-
let r = e[l];
|
201
|
-
if (r == null) {
|
202
|
-
const u = e[0] ? e[0][l] : void 0;
|
203
|
-
if (u !== void 0)
|
204
|
-
r = u;
|
205
|
-
else
|
206
|
-
return r;
|
207
|
-
}
|
208
|
-
return f.isValidElement(r) ? (t = !0, f.Children.toArray(r).map((u) => ({ ...u, key: n.toString() }))) : r;
|
209
|
-
}
|
210
|
-
return s;
|
211
|
-
});
|
212
|
-
return t ? a : a.join("");
|
213
|
-
};
|
214
|
-
export {
|
215
|
-
L as default
|
216
|
-
};
|
@@ -1 +0,0 @@
|
|
1
|
-
(function(u,o){typeof exports=="object"&&typeof module<"u"?module.exports=o(require("react")):typeof define=="function"&&define.amd?define(["react"],o):(u=typeof globalThis<"u"?globalThis:u||self,u.ReactLocalization=o(u.React))})(this,function(u){"use strict";function o(){const i="en-US";if(typeof navigator>"u")return i;const e=navigator;if(e){if(e.language)return e.language;if(e.languages&&e.languages[0])return e.languages[0];if(e.userLanguage)return e.userLanguage;if(e.browserLanguage)return e.browserLanguage}return i}function p(i,e){if(e[i])return i;const t=i.indexOf("-"),n=t>=0?i.substring(0,t):i;return e[n]?n:Object.keys(e)[0]}function _(i){const e=["_interfaceLanguage","_language","_defaultLanguage","_defaultLanguageFirstLevelKeys","_props"];i.forEach(t=>{if(e.indexOf(t)!==-1)throw new Error(`${t} cannot be used as a key. It is a reserved word.`)})}function L(i){let e="";const t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(let n=0;n<i;n+=1)e+=t.charAt(Math.floor(Math.random()*t.length));return e}const f=/(\{[\d|\w]+\})/,c=/(\$ref\{[\w|.]+\})/;class h{constructor(e,t){typeof t=="function"&&(t={customLanguageInterface:t}),this._opts=Object.assign({},{customLanguageInterface:o,pseudo:!1,pseudoMultipleLanguages:!1,logsEnabled:!0},t),this._interfaceLanguage=this._opts.customLanguageInterface(),this._language=this._interfaceLanguage,this.setContent(e)}setContent(e){const[t]=Object.keys(e);this._defaultLanguage=t,this._defaultLanguageFirstLevelKeys=[],this._props=e,_(Object.keys(e[this._defaultLanguage])),Object.keys(this._props[this._defaultLanguage]).forEach(n=>{typeof this._props[this._defaultLanguage][n]=="string"&&this._defaultLanguageFirstLevelKeys.push(n)}),this.setLanguage(this._interfaceLanguage),this._opts.pseudo&&this._pseudoAllValues(this._props)}_pseudoAllValues(e){Object.keys(e).forEach(t=>{if(typeof e[t]=="object")this._pseudoAllValues(e[t]);else if(typeof e[t]=="string"){if(e[t].indexOf("[")===0&&e[t].lastIndexOf("]")===e[t].length-1)return;const n=e[t].split(" ");for(let s=0;s<n.length;s+=1)if(!n[s].match(f)){if(!n[s].match(c)){let a=n[s].length;this._opts.pseudoMultipleLanguages&&(a=parseInt(a*1.4,10)),n[s]=L(a)}}e[t]=`[${n.join(" ")}]`}})}setLanguage(e){const t=p(e,this._props),n=Object.keys(this._props)[0];if(this._language=t,this._props[t]){for(let a=0;a<this._defaultLanguageFirstLevelKeys.length;a+=1)delete this[this._defaultLanguageFirstLevelKeys[a]];let s=Object.assign({},this._props[this._language]);Object.keys(s).forEach(a=>{this[a]=s[a]}),n!==this._language&&(s=this._props[n],this._fallbackValues(s,this))}}_fallbackValues(e,t){Object.keys(e).forEach(n=>{Object.prototype.hasOwnProperty.call(e,n)&&!t[n]&&t[n]!==""?(t[n]=e[n],this._opts.logsEnabled&&console.log(`🚧 👷 key '${n}' not found in localizedStrings for language ${this._language} 🚧`)):typeof t[n]!="string"&&this._fallbackValues(e[n],t[n])})}getLanguage(){return this._language}getInterfaceLanguage(){return this._interfaceLanguage}getAvailableLanguages(){return this._availableLanguages||(this._availableLanguages=[],Object.keys(this._props).forEach(e=>{this._availableLanguages.push(e)})),this._availableLanguages}formatString(e,...t){let n=e||"";return typeof n=="string"&&(n=this.getString(e,null,!0)||n),n.split(c).filter(a=>!!a).map(a=>{if(a.match(c)){const r=a.slice(5,-1),l=this.getString(r);return l||(this._opts.logsEnabled&&console.log(`No Localization ref found for '${a}' in string '${e}'`),`$ref(id:${r})`)}return a}).join("").split(f).filter(a=>!!a).map(a=>{if(a.match(f)){const r=a.slice(1,-1);let l=t[r];if(l===void 0){const g=t[0][r];if(g!==void 0)l=g;else return l}return l}return a}).join("")}getString(e,t,n=!1){try{let s=this._props[t||this._language];const a=e.split(".");for(let r=0;r<a.length;r+=1){if(s[a[r]]===void 0)throw Error(a[r]);s=s[a[r]]}return s}catch(s){!n&&this._opts.logsEnabled&&console.log(`No localization found for key '${e}' and language '${t}', failed on ${s.message}`)}return null}getContent(){return this._props}}const d=/(\{[\d|\w]+\})/;return h.prototype.formatString=(i,...e)=>{let t=!1;const n=(i||"").split(d).filter(s=>!!s).map((s,a)=>{if(s.match(d)){const r=s.slice(1,-1);let l=e[r];if(l==null){const g=e[0]?e[0][r]:void 0;if(g!==void 0)l=g;else return l}return u.isValidElement(l)?(t=!0,u.Children.toArray(l).map(g=>({...g,key:a.toString()}))):l}return s});return t?n:n.join("")},h});
|