@next-core/i18n 1.0.2
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/dist/cjs/index.js +38 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/init.js +45 -0
- package/dist/cjs/init.js.map +1 -0
- package/dist/cjs/react.js +23 -0
- package/dist/cjs/react.js.map +1 -0
- package/dist/cjs/text.js +36 -0
- package/dist/cjs/text.js.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/init.js +38 -0
- package/dist/esm/init.js.map +1 -0
- package/dist/esm/react.js +12 -0
- package/dist/esm/react.js.map +1 -0
- package/dist/esm/text.js +30 -0
- package/dist/esm/text.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/init.d.ts +3 -0
- package/dist/types/react.d.ts +5 -0
- package/dist/types/text.d.ts +2 -0
- package/package.json +53 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
var _exportNames = {
|
|
8
|
+
i18n: true
|
|
9
|
+
};
|
|
10
|
+
exports.i18n = void 0;
|
|
11
|
+
var _i18next = _interopRequireDefault(require("i18next"));
|
|
12
|
+
var _init = require("./init.js");
|
|
13
|
+
Object.keys(_init).forEach(function (key) {
|
|
14
|
+
if (key === "default" || key === "__esModule") return;
|
|
15
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
16
|
+
if (key in exports && exports[key] === _init[key]) return;
|
|
17
|
+
Object.defineProperty(exports, key, {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function () {
|
|
20
|
+
return _init[key];
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
var _text = require("./text.js");
|
|
25
|
+
Object.keys(_text).forEach(function (key) {
|
|
26
|
+
if (key === "default" || key === "__esModule") return;
|
|
27
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
28
|
+
if (key in exports && exports[key] === _text[key]) return;
|
|
29
|
+
Object.defineProperty(exports, key, {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: function () {
|
|
32
|
+
return _text[key];
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
const i18n = process.env.NODE_ENV === "test" ? _i18next.default : /* istanbul ignore next */_i18next.default.default;
|
|
37
|
+
exports.i18n = i18n;
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["_i18next","_interopRequireDefault","require","_init","Object","keys","forEach","key","prototype","hasOwnProperty","call","_exportNames","exports","defineProperty","enumerable","get","_text","i18n","process","env","NODE_ENV","i18next","default"],"sources":["../../src/index.ts"],"sourcesContent":["import i18next from \"i18next\";\n\nexport const i18n =\n process.env.NODE_ENV === \"test\"\n ? (i18next as unknown as typeof i18next.default)\n : /* istanbul ignore next */ i18next.default;\n\nexport * from \"./init.js\";\nexport * from \"./text.js\";\n"],"mappings":";;;;;;;;;;AAAA,IAAAA,QAAA,GAAAC,sBAAA,CAAAC,OAAA;AAOA,IAAAC,KAAA,GAAAD,OAAA;AAAAE,MAAA,CAAAC,IAAA,CAAAF,KAAA,EAAAG,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAJ,KAAA,CAAAI,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAZ,KAAA,CAAAI,GAAA;IAAA;EAAA;AAAA;AACA,IAAAS,KAAA,GAAAd,OAAA;AAAAE,MAAA,CAAAC,IAAA,CAAAW,KAAA,EAAAV,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAC,YAAA,EAAAJ,GAAA;EAAA,IAAAA,GAAA,IAAAK,OAAA,IAAAA,OAAA,CAAAL,GAAA,MAAAS,KAAA,CAAAT,GAAA;EAAAH,MAAA,CAAAS,cAAA,CAAAD,OAAA,EAAAL,GAAA;IAAAO,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,KAAA,CAAAT,GAAA;IAAA;EAAA;AAAA;AANO,MAAMU,IAAI,GACfC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,MAAM,GAC1BC,gBAAO,GACR,0BAA2BA,gBAAO,CAACC,OAAO;AAACV,OAAA,CAAAK,IAAA,GAAAA,IAAA"}
|
package/dist/cjs/init.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.initializeI18n = initializeI18n;
|
|
8
|
+
var _i18n = require("@next-core/i18n");
|
|
9
|
+
var _i18nextBrowserLanguagedetector = _interopRequireDefault(require("i18next-browser-languagedetector"));
|
|
10
|
+
let initialized = false;
|
|
11
|
+
const initializedNamespaces = new Set();
|
|
12
|
+
function initializeI18n(NS, locales) {
|
|
13
|
+
if (!initialized) {
|
|
14
|
+
initialized = true;
|
|
15
|
+
_i18n.i18n
|
|
16
|
+
// learn more: https://github.com/i18next/i18next-xhr-backend
|
|
17
|
+
// .use(Backend)
|
|
18
|
+
// learn more: https://github.com/i18next/i18next-browser-languageDetector
|
|
19
|
+
.use(process.env.NODE_ENV === "test" ? _i18nextBrowserLanguagedetector.default : /* istanbul ignore next */_i18nextBrowserLanguagedetector.default.default)
|
|
20
|
+
// for all options read: https://www.i18next.com/overview/configuration-options
|
|
21
|
+
.init({
|
|
22
|
+
fallbackLng: "zh",
|
|
23
|
+
debug: process.env.NODE_ENV === "development",
|
|
24
|
+
supportedLngs: ["zh", "en"],
|
|
25
|
+
nonExplicitSupportedLngs: true,
|
|
26
|
+
interpolation: {
|
|
27
|
+
escapeValue: false // not needed for react as it escapes by default
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
react: {
|
|
31
|
+
useSuspense: false
|
|
32
|
+
},
|
|
33
|
+
compatibilityJSON: "v3",
|
|
34
|
+
resources: {}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (!NS || !locales || initializedNamespaces.has(NS)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
initializedNamespaces.add(NS);
|
|
41
|
+
for (const [lang, resources] of Object.entries(locales)) {
|
|
42
|
+
_i18n.i18n.addResourceBundle(lang, NS, resources);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","names":["_i18n","require","_i18nextBrowserLanguagedetector","_interopRequireDefault","initialized","initializedNamespaces","Set","initializeI18n","NS","locales","i18n","use","process","env","NODE_ENV","LanguageDetector","default","init","fallbackLng","debug","supportedLngs","nonExplicitSupportedLngs","interpolation","escapeValue","react","useSuspense","compatibilityJSON","resources","has","add","lang","Object","entries","addResourceBundle"],"sources":["../../src/init.ts"],"sourcesContent":["import { i18n } from \"@next-core/i18n\";\nimport LanguageDetector from \"i18next-browser-languagedetector\";\n\nlet initialized = false;\nconst initializedNamespaces = new Set<string>();\n\nexport type Locales = Record<string, Record<string, string>>;\n\nexport function initializeI18n(): void;\nexport function initializeI18n(NS: string, locales: Locales): void;\nexport function initializeI18n(NS?: string, locales?: Locales) {\n if (!initialized) {\n initialized = true;\n i18n\n // learn more: https://github.com/i18next/i18next-xhr-backend\n // .use(Backend)\n // learn more: https://github.com/i18next/i18next-browser-languageDetector\n .use(\n process.env.NODE_ENV === \"test\"\n ? (LanguageDetector as unknown as LanguageDetector.default)\n : /* istanbul ignore next */ LanguageDetector.default\n )\n // for all options read: https://www.i18next.com/overview/configuration-options\n .init({\n fallbackLng: \"zh\",\n debug: process.env.NODE_ENV === \"development\",\n supportedLngs: [\"zh\", \"en\"],\n nonExplicitSupportedLngs: true,\n interpolation: {\n escapeValue: false, // not needed for react as it escapes by default\n },\n react: {\n useSuspense: false,\n },\n compatibilityJSON: \"v3\",\n resources: {},\n });\n }\n\n if (!NS || !locales || initializedNamespaces.has(NS)) {\n return;\n }\n initializedNamespaces.add(NS);\n for (const [lang, resources] of Object.entries(locales)) {\n i18n.addResourceBundle(lang, NS, resources);\n }\n}\n"],"mappings":";;;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,+BAAA,GAAAC,sBAAA,CAAAF,OAAA;AAEA,IAAIG,WAAW,GAAG,KAAK;AACvB,MAAMC,qBAAqB,GAAG,IAAIC,GAAG,EAAU;AAMxC,SAASC,cAAcA,CAACC,EAAW,EAAEC,OAAiB,EAAE;EAC7D,IAAI,CAACL,WAAW,EAAE;IAChBA,WAAW,GAAG,IAAI;IAClBM;IACE;IACA;IACA;IAAA,CACCC,GAAG,CACFC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,MAAM,GAC1BC,uCAAgB,GACjB,0BAA2BA,uCAAgB,CAACC,OAAO;IAEzD;IAAA,CACCC,IAAI,CAAC;MACJC,WAAW,EAAE,IAAI;MACjBC,KAAK,EAAEP,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,aAAa;MAC7CM,aAAa,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;MAC3BC,wBAAwB,EAAE,IAAI;MAC9BC,aAAa,EAAE;QACbC,WAAW,EAAE,KAAK,CAAE;MACtB,CAAC;;MACDC,KAAK,EAAE;QACLC,WAAW,EAAE;MACf,CAAC;MACDC,iBAAiB,EAAE,IAAI;MACvBC,SAAS,EAAE,CAAC;IACd,CAAC,CAAC;EACN;EAEA,IAAI,CAACnB,EAAE,IAAI,CAACC,OAAO,IAAIJ,qBAAqB,CAACuB,GAAG,CAACpB,EAAE,CAAC,EAAE;IACpD;EACF;EACAH,qBAAqB,CAACwB,GAAG,CAACrB,EAAE,CAAC;EAC7B,KAAK,MAAM,CAACsB,IAAI,EAAEH,SAAS,CAAC,IAAII,MAAM,CAACC,OAAO,CAACvB,OAAO,CAAC,EAAE;IACvDC,UAAI,CAACuB,iBAAiB,CAACH,IAAI,EAAEtB,EAAE,EAAEmB,SAAS,CAAC;EAC7C;AACF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.initializeReactI18n = initializeReactI18n;
|
|
7
|
+
Object.defineProperty(exports, "useTranslation", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _reactI18next.useTranslation;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
var _i18n = require("@next-core/i18n");
|
|
14
|
+
var _reactI18next = require("react-i18next");
|
|
15
|
+
let initialized = false;
|
|
16
|
+
function initializeReactI18n(NS, locales) {
|
|
17
|
+
(0, _i18n.initializeI18n)(NS, locales);
|
|
18
|
+
if (!initialized) {
|
|
19
|
+
initialized = true;
|
|
20
|
+
_reactI18next.initReactI18next.init(_i18n.i18n);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","names":["_i18n","require","_reactI18next","initialized","initializeReactI18n","NS","locales","initializeI18n","initReactI18next","init","i18n"],"sources":["../../src/react.ts"],"sourcesContent":["import { i18n, initializeI18n, Locales } from \"@next-core/i18n\";\nimport { initReactI18next, useTranslation } from \"react-i18next\";\n\nlet initialized = false;\n\nexport function initializeReactI18n(): void;\nexport function initializeReactI18n(NS: string, locales: Locales): void;\nexport function initializeReactI18n(NS?: string, locales?: Locales) {\n initializeI18n(NS as string, locales as Locales);\n if (!initialized) {\n initialized = true;\n initReactI18next.init(i18n);\n }\n}\n\nexport { useTranslation };\n"],"mappings":";;;;;;;;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,aAAA,GAAAD,OAAA;AAEA,IAAIE,WAAW,GAAG,KAAK;AAIhB,SAASC,mBAAmBA,CAACC,EAAW,EAAEC,OAAiB,EAAE;EAClE,IAAAC,oBAAc,EAACF,EAAE,EAAYC,OAAO,CAAY;EAChD,IAAI,CAACH,WAAW,EAAE;IAChBA,WAAW,GAAG,IAAI;IAClBK,8BAAgB,CAACC,IAAI,CAACC,UAAI,CAAC;EAC7B;AACF"}
|
package/dist/cjs/text.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.i18nText = i18nText;
|
|
7
|
+
var _i18n = require("@next-core/i18n");
|
|
8
|
+
var _general = require("@next-core/utils/general");
|
|
9
|
+
function i18nText(data) {
|
|
10
|
+
var _i18n$language;
|
|
11
|
+
if (!data) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const language = (_i18n$language = _i18n.i18n.language) !== null && _i18n$language !== void 0 ? _i18n$language : "zh-CN";
|
|
15
|
+
// First, make a perfect match.
|
|
16
|
+
if ((0, _general.hasOwnProperty)(data, language)) {
|
|
17
|
+
return data[language];
|
|
18
|
+
}
|
|
19
|
+
// https://en.wikipedia.org/wiki/IETF_language_tag
|
|
20
|
+
const primaryLanguage = language.split("-")[0];
|
|
21
|
+
if (primaryLanguage !== language) {
|
|
22
|
+
// Then, match the primary language (which omits other subtags).
|
|
23
|
+
// E.g., match `zh` if the current language is `zh-CN`.
|
|
24
|
+
return (0, _general.hasOwnProperty)(data, primaryLanguage) ? data[primaryLanguage] : undefined;
|
|
25
|
+
}
|
|
26
|
+
// At last, match the first key which starts with the primary language,
|
|
27
|
+
// if the current language contains primary subtag only.
|
|
28
|
+
// E.g., match `zh-CN` if the current language is `zh`.
|
|
29
|
+
const prefix = `${primaryLanguage}-`;
|
|
30
|
+
for (const key of Object.keys(data)) {
|
|
31
|
+
if (key.startsWith(prefix)) {
|
|
32
|
+
return data[key];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","names":["_i18n","require","_general","i18nText","data","_i18n$language","language","i18n","hasOwnProperty","primaryLanguage","split","undefined","prefix","key","Object","keys","startsWith"],"sources":["../../src/text.ts"],"sourcesContent":["import { i18n } from \"@next-core/i18n\";\nimport { hasOwnProperty } from \"@next-core/utils/general\";\nimport { I18nData } from \"@next-core/types\";\n\nexport function i18nText(\n data: I18nData | null | undefined\n): string | undefined {\n if (!data) {\n return;\n }\n const language = i18n.language ?? \"zh-CN\";\n // First, make a perfect match.\n if (hasOwnProperty(data, language)) {\n return data[language];\n }\n // https://en.wikipedia.org/wiki/IETF_language_tag\n const primaryLanguage = language.split(\"-\")[0];\n if (primaryLanguage !== language) {\n // Then, match the primary language (which omits other subtags).\n // E.g., match `zh` if the current language is `zh-CN`.\n return hasOwnProperty(data, primaryLanguage)\n ? data[primaryLanguage]\n : undefined;\n }\n // At last, match the first key which starts with the primary language,\n // if the current language contains primary subtag only.\n // E.g., match `zh-CN` if the current language is `zh`.\n const prefix = `${primaryLanguage}-`;\n for (const key of Object.keys(data)) {\n if (key.startsWith(prefix)) {\n return data[key];\n }\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAGO,SAASE,QAAQA,CACtBC,IAAiC,EACb;EAAA,IAAAC,cAAA;EACpB,IAAI,CAACD,IAAI,EAAE;IACT;EACF;EACA,MAAME,QAAQ,IAAAD,cAAA,GAAGE,UAAI,CAACD,QAAQ,cAAAD,cAAA,cAAAA,cAAA,GAAI,OAAO;EACzC;EACA,IAAI,IAAAG,uBAAc,EAACJ,IAAI,EAAEE,QAAQ,CAAC,EAAE;IAClC,OAAOF,IAAI,CAACE,QAAQ,CAAC;EACvB;EACA;EACA,MAAMG,eAAe,GAAGH,QAAQ,CAACI,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;EAC9C,IAAID,eAAe,KAAKH,QAAQ,EAAE;IAChC;IACA;IACA,OAAO,IAAAE,uBAAc,EAACJ,IAAI,EAAEK,eAAe,CAAC,GACxCL,IAAI,CAACK,eAAe,CAAC,GACrBE,SAAS;EACf;EACA;EACA;EACA;EACA,MAAMC,MAAM,GAAI,GAAEH,eAAgB,GAAE;EACpC,KAAK,MAAMI,GAAG,IAAIC,MAAM,CAACC,IAAI,CAACX,IAAI,CAAC,EAAE;IACnC,IAAIS,GAAG,CAACG,UAAU,CAACJ,MAAM,CAAC,EAAE;MAC1B,OAAOR,IAAI,CAACS,GAAG,CAAC;IAClB;EACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["i18next","i18n","process","env","NODE_ENV","default"],"sources":["../../src/index.ts"],"sourcesContent":["import i18next from \"i18next\";\n\nexport const i18n =\n process.env.NODE_ENV === \"test\"\n ? (i18next as unknown as typeof i18next.default)\n : /* istanbul ignore next */ i18next.default;\n\nexport * from \"./init.js\";\nexport * from \"./text.js\";\n"],"mappings":"AAAA,OAAOA,OAAO,MAAM,SAAS;AAE7B,OAAO,IAAMC,IAAI,GACfC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,MAAM,GAC1BJ,OAAO,GACR,0BAA2BA,OAAO,CAACK,OAAO;AAEhD,cAAc,WAAW;AACzB,cAAc,WAAW"}
|
package/dist/esm/init.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { i18n } from "@next-core/i18n";
|
|
2
|
+
import LanguageDetector from "i18next-browser-languagedetector";
|
|
3
|
+
var initialized = false;
|
|
4
|
+
var initializedNamespaces = new Set();
|
|
5
|
+
export function initializeI18n(NS, locales) {
|
|
6
|
+
if (!initialized) {
|
|
7
|
+
initialized = true;
|
|
8
|
+
i18n
|
|
9
|
+
// learn more: https://github.com/i18next/i18next-xhr-backend
|
|
10
|
+
// .use(Backend)
|
|
11
|
+
// learn more: https://github.com/i18next/i18next-browser-languageDetector
|
|
12
|
+
.use(process.env.NODE_ENV === "test" ? LanguageDetector : /* istanbul ignore next */LanguageDetector.default)
|
|
13
|
+
// for all options read: https://www.i18next.com/overview/configuration-options
|
|
14
|
+
.init({
|
|
15
|
+
fallbackLng: "zh",
|
|
16
|
+
debug: process.env.NODE_ENV === "development",
|
|
17
|
+
supportedLngs: ["zh", "en"],
|
|
18
|
+
nonExplicitSupportedLngs: true,
|
|
19
|
+
interpolation: {
|
|
20
|
+
escapeValue: false // not needed for react as it escapes by default
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
react: {
|
|
24
|
+
useSuspense: false
|
|
25
|
+
},
|
|
26
|
+
compatibilityJSON: "v3",
|
|
27
|
+
resources: {}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (!NS || !locales || initializedNamespaces.has(NS)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
initializedNamespaces.add(NS);
|
|
34
|
+
for (var [lang, resources] of Object.entries(locales)) {
|
|
35
|
+
i18n.addResourceBundle(lang, NS, resources);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","names":["i18n","LanguageDetector","initialized","initializedNamespaces","Set","initializeI18n","NS","locales","use","process","env","NODE_ENV","default","init","fallbackLng","debug","supportedLngs","nonExplicitSupportedLngs","interpolation","escapeValue","react","useSuspense","compatibilityJSON","resources","has","add","lang","Object","entries","addResourceBundle"],"sources":["../../src/init.ts"],"sourcesContent":["import { i18n } from \"@next-core/i18n\";\nimport LanguageDetector from \"i18next-browser-languagedetector\";\n\nlet initialized = false;\nconst initializedNamespaces = new Set<string>();\n\nexport type Locales = Record<string, Record<string, string>>;\n\nexport function initializeI18n(): void;\nexport function initializeI18n(NS: string, locales: Locales): void;\nexport function initializeI18n(NS?: string, locales?: Locales) {\n if (!initialized) {\n initialized = true;\n i18n\n // learn more: https://github.com/i18next/i18next-xhr-backend\n // .use(Backend)\n // learn more: https://github.com/i18next/i18next-browser-languageDetector\n .use(\n process.env.NODE_ENV === \"test\"\n ? (LanguageDetector as unknown as LanguageDetector.default)\n : /* istanbul ignore next */ LanguageDetector.default\n )\n // for all options read: https://www.i18next.com/overview/configuration-options\n .init({\n fallbackLng: \"zh\",\n debug: process.env.NODE_ENV === \"development\",\n supportedLngs: [\"zh\", \"en\"],\n nonExplicitSupportedLngs: true,\n interpolation: {\n escapeValue: false, // not needed for react as it escapes by default\n },\n react: {\n useSuspense: false,\n },\n compatibilityJSON: \"v3\",\n resources: {},\n });\n }\n\n if (!NS || !locales || initializedNamespaces.has(NS)) {\n return;\n }\n initializedNamespaces.add(NS);\n for (const [lang, resources] of Object.entries(locales)) {\n i18n.addResourceBundle(lang, NS, resources);\n }\n}\n"],"mappings":"AAAA,SAASA,IAAI,QAAQ,iBAAiB;AACtC,OAAOC,gBAAgB,MAAM,kCAAkC;AAE/D,IAAIC,WAAW,GAAG,KAAK;AACvB,IAAMC,qBAAqB,GAAG,IAAIC,GAAG,EAAU;AAM/C,OAAO,SAASC,cAAcA,CAACC,EAAW,EAAEC,OAAiB,EAAE;EAC7D,IAAI,CAACL,WAAW,EAAE;IAChBA,WAAW,GAAG,IAAI;IAClBF;IACE;IACA;IACA;IAAA,CACCQ,GAAG,CACFC,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,MAAM,GAC1BV,gBAAgB,GACjB,0BAA2BA,gBAAgB,CAACW,OAAO;IAEzD;IAAA,CACCC,IAAI,CAAC;MACJC,WAAW,EAAE,IAAI;MACjBC,KAAK,EAAEN,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,aAAa;MAC7CK,aAAa,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;MAC3BC,wBAAwB,EAAE,IAAI;MAC9BC,aAAa,EAAE;QACbC,WAAW,EAAE,KAAK,CAAE;MACtB,CAAC;;MACDC,KAAK,EAAE;QACLC,WAAW,EAAE;MACf,CAAC;MACDC,iBAAiB,EAAE,IAAI;MACvBC,SAAS,EAAE,CAAC;IACd,CAAC,CAAC;EACN;EAEA,IAAI,CAACjB,EAAE,IAAI,CAACC,OAAO,IAAIJ,qBAAqB,CAACqB,GAAG,CAAClB,EAAE,CAAC,EAAE;IACpD;EACF;EACAH,qBAAqB,CAACsB,GAAG,CAACnB,EAAE,CAAC;EAC7B,KAAK,IAAM,CAACoB,IAAI,EAAEH,SAAS,CAAC,IAAII,MAAM,CAACC,OAAO,CAACrB,OAAO,CAAC,EAAE;IACvDP,IAAI,CAAC6B,iBAAiB,CAACH,IAAI,EAAEpB,EAAE,EAAEiB,SAAS,CAAC;EAC7C;AACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { i18n, initializeI18n } from "@next-core/i18n";
|
|
2
|
+
import { initReactI18next, useTranslation } from "react-i18next";
|
|
3
|
+
var initialized = false;
|
|
4
|
+
export function initializeReactI18n(NS, locales) {
|
|
5
|
+
initializeI18n(NS, locales);
|
|
6
|
+
if (!initialized) {
|
|
7
|
+
initialized = true;
|
|
8
|
+
initReactI18next.init(i18n);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export { useTranslation };
|
|
12
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.js","names":["i18n","initializeI18n","initReactI18next","useTranslation","initialized","initializeReactI18n","NS","locales","init"],"sources":["../../src/react.ts"],"sourcesContent":["import { i18n, initializeI18n, Locales } from \"@next-core/i18n\";\nimport { initReactI18next, useTranslation } from \"react-i18next\";\n\nlet initialized = false;\n\nexport function initializeReactI18n(): void;\nexport function initializeReactI18n(NS: string, locales: Locales): void;\nexport function initializeReactI18n(NS?: string, locales?: Locales) {\n initializeI18n(NS as string, locales as Locales);\n if (!initialized) {\n initialized = true;\n initReactI18next.init(i18n);\n }\n}\n\nexport { useTranslation };\n"],"mappings":"AAAA,SAASA,IAAI,EAAEC,cAAc,QAAiB,iBAAiB;AAC/D,SAASC,gBAAgB,EAAEC,cAAc,QAAQ,eAAe;AAEhE,IAAIC,WAAW,GAAG,KAAK;AAIvB,OAAO,SAASC,mBAAmBA,CAACC,EAAW,EAAEC,OAAiB,EAAE;EAClEN,cAAc,CAACK,EAAE,EAAYC,OAAO,CAAY;EAChD,IAAI,CAACH,WAAW,EAAE;IAChBA,WAAW,GAAG,IAAI;IAClBF,gBAAgB,CAACM,IAAI,CAACR,IAAI,CAAC;EAC7B;AACF;AAEA,SAASG,cAAc"}
|
package/dist/esm/text.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { i18n } from "@next-core/i18n";
|
|
2
|
+
import { hasOwnProperty } from "@next-core/utils/general";
|
|
3
|
+
export function i18nText(data) {
|
|
4
|
+
var _i18n$language;
|
|
5
|
+
if (!data) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
var language = (_i18n$language = i18n.language) !== null && _i18n$language !== void 0 ? _i18n$language : "zh-CN";
|
|
9
|
+
// First, make a perfect match.
|
|
10
|
+
if (hasOwnProperty(data, language)) {
|
|
11
|
+
return data[language];
|
|
12
|
+
}
|
|
13
|
+
// https://en.wikipedia.org/wiki/IETF_language_tag
|
|
14
|
+
var primaryLanguage = language.split("-")[0];
|
|
15
|
+
if (primaryLanguage !== language) {
|
|
16
|
+
// Then, match the primary language (which omits other subtags).
|
|
17
|
+
// E.g., match `zh` if the current language is `zh-CN`.
|
|
18
|
+
return hasOwnProperty(data, primaryLanguage) ? data[primaryLanguage] : undefined;
|
|
19
|
+
}
|
|
20
|
+
// At last, match the first key which starts with the primary language,
|
|
21
|
+
// if the current language contains primary subtag only.
|
|
22
|
+
// E.g., match `zh-CN` if the current language is `zh`.
|
|
23
|
+
var prefix = "".concat(primaryLanguage, "-");
|
|
24
|
+
for (var key of Object.keys(data)) {
|
|
25
|
+
if (key.startsWith(prefix)) {
|
|
26
|
+
return data[key];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","names":["i18n","hasOwnProperty","i18nText","data","_i18n$language","language","primaryLanguage","split","undefined","prefix","concat","key","Object","keys","startsWith"],"sources":["../../src/text.ts"],"sourcesContent":["import { i18n } from \"@next-core/i18n\";\nimport { hasOwnProperty } from \"@next-core/utils/general\";\nimport { I18nData } from \"@next-core/types\";\n\nexport function i18nText(\n data: I18nData | null | undefined\n): string | undefined {\n if (!data) {\n return;\n }\n const language = i18n.language ?? \"zh-CN\";\n // First, make a perfect match.\n if (hasOwnProperty(data, language)) {\n return data[language];\n }\n // https://en.wikipedia.org/wiki/IETF_language_tag\n const primaryLanguage = language.split(\"-\")[0];\n if (primaryLanguage !== language) {\n // Then, match the primary language (which omits other subtags).\n // E.g., match `zh` if the current language is `zh-CN`.\n return hasOwnProperty(data, primaryLanguage)\n ? data[primaryLanguage]\n : undefined;\n }\n // At last, match the first key which starts with the primary language,\n // if the current language contains primary subtag only.\n // E.g., match `zh-CN` if the current language is `zh`.\n const prefix = `${primaryLanguage}-`;\n for (const key of Object.keys(data)) {\n if (key.startsWith(prefix)) {\n return data[key];\n }\n }\n}\n"],"mappings":"AAAA,SAASA,IAAI,QAAQ,iBAAiB;AACtC,SAASC,cAAc,QAAQ,0BAA0B;AAGzD,OAAO,SAASC,QAAQA,CACtBC,IAAiC,EACb;EAAA,IAAAC,cAAA;EACpB,IAAI,CAACD,IAAI,EAAE;IACT;EACF;EACA,IAAME,QAAQ,IAAAD,cAAA,GAAGJ,IAAI,CAACK,QAAQ,cAAAD,cAAA,cAAAA,cAAA,GAAI,OAAO;EACzC;EACA,IAAIH,cAAc,CAACE,IAAI,EAAEE,QAAQ,CAAC,EAAE;IAClC,OAAOF,IAAI,CAACE,QAAQ,CAAC;EACvB;EACA;EACA,IAAMC,eAAe,GAAGD,QAAQ,CAACE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;EAC9C,IAAID,eAAe,KAAKD,QAAQ,EAAE;IAChC;IACA;IACA,OAAOJ,cAAc,CAACE,IAAI,EAAEG,eAAe,CAAC,GACxCH,IAAI,CAACG,eAAe,CAAC,GACrBE,SAAS;EACf;EACA;EACA;EACA;EACA,IAAMC,MAAM,MAAAC,MAAA,CAAMJ,eAAe,MAAG;EACpC,KAAK,IAAMK,GAAG,IAAIC,MAAM,CAACC,IAAI,CAACV,IAAI,CAAC,EAAE;IACnC,IAAIQ,GAAG,CAACG,UAAU,CAACL,MAAM,CAAC,EAAE;MAC1B,OAAON,IAAI,CAACQ,GAAG,CAAC;IAClB;EACF;AACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@next-core/i18n",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"homepage": "https://github.com/easyops-cn/next-core/tree/master/packages/i18n",
|
|
5
|
+
"license": "GPL-3.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git@github.com:easyops-cn/next-core.git"
|
|
9
|
+
},
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"type": "module",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/types/index.d.ts",
|
|
18
|
+
"import": "./dist/esm/index.js",
|
|
19
|
+
"require": "./dist/cjs/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./react": {
|
|
22
|
+
"types": "./dist/types/react.d.ts",
|
|
23
|
+
"import": "./dist/esm/react.js",
|
|
24
|
+
"require": "./dist/cjs/react.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"clean": "rimraf dist",
|
|
30
|
+
"prestart": "npm run clean",
|
|
31
|
+
"start": "concurrently -k -n types,main \"npm run start:types\" \"npm run start:main\"",
|
|
32
|
+
"start:main": "cross-env NODE_ENV=development build-next-libs --watch",
|
|
33
|
+
"start:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types --project tsconfig.build.json --watch",
|
|
34
|
+
"prebuild": "npm run clean",
|
|
35
|
+
"build": "npm run build:types && npm run build:main",
|
|
36
|
+
"build:main": "cross-env NODE_ENV=production build-next-libs",
|
|
37
|
+
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types --project tsconfig.build.json",
|
|
38
|
+
"test": "cross-env NODE_ENV='test' test-next",
|
|
39
|
+
"test:ci": "cross-env NODE_ENV='test' CI=true test-next"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@next-core/types": "^1.0.2",
|
|
43
|
+
"@next-core/utils": "^1.0.2",
|
|
44
|
+
"i18next": "^22.4.12",
|
|
45
|
+
"i18next-browser-languagedetector": "^7.0.1",
|
|
46
|
+
"react-i18next": "^12.2.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@next-core/build-next-libs": "^1.0.2",
|
|
50
|
+
"@next-core/test-next": "^1.0.2"
|
|
51
|
+
},
|
|
52
|
+
"gitHead": "ffd8b31a5d99db5af3b4ec7675ff2d6541eb1338"
|
|
53
|
+
}
|