@nimpl/i18n 1.1.1 → 1.1.2-canary.1

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.
@@ -1,12 +1,12 @@
1
- import React from 'react';
2
- import { type I18nOptions } from './types';
1
+ import React from "react";
2
+ import { type I18nOptions } from "./types";
3
3
  type ClientTranslationProps = {
4
4
  term: string;
5
5
  components?: {
6
6
  [key: string]: JSX.Element;
7
7
  };
8
- query?: I18nOptions['query'];
9
- removeUnusedQueries?: I18nOptions['removeUnusedQueries'];
8
+ query?: I18nOptions["query"];
9
+ removeUnusedQueries?: I18nOptions["removeUnusedQueries"];
10
10
  };
11
11
  declare const ClientTranslation: React.FC<ClientTranslationProps>;
12
12
  export default ClientTranslation;
package/I18nProvider.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import React from 'react';
2
- import { type I18nTransmitterProps } from './I18nTransmitter';
1
+ import React from "react";
2
+ import { type I18nTransmitterProps } from "./I18nTransmitter";
3
3
  type I18nProviderProps = {
4
4
  lang: string;
5
5
  children: React.ReactNode;
6
- clientTerms?: I18nTransmitterProps['terms'];
6
+ clientTerms?: I18nTransmitterProps["terms"];
7
7
  };
8
8
  declare const I18nProvider: React.FC<I18nProviderProps>;
9
9
  export default I18nProvider;
@@ -1,5 +1,5 @@
1
- import React from 'react';
2
- import { type I18nOptions } from './types';
1
+ import React from "react";
2
+ import { type I18nOptions } from "./types";
3
3
  export type I18nTransmitterProps = {
4
4
  terms: (string | [string, I18nOptions])[];
5
5
  children: React.ReactNode;
@@ -1,12 +1,12 @@
1
- import React from 'react';
2
- import { type I18nOptions } from './types';
1
+ import React from "react";
2
+ import { type I18nOptions } from "./types";
3
3
  type ServerTranslationProps = {
4
4
  term: string;
5
5
  components?: {
6
6
  [key: string]: JSX.Element;
7
7
  };
8
- query?: I18nOptions['query'];
9
- removeUnusedQueries?: I18nOptions['removeUnusedQueries'];
8
+ query?: I18nOptions["query"];
9
+ removeUnusedQueries?: I18nOptions["removeUnusedQueries"];
10
10
  };
11
11
  declare const ServerTranslation: React.FC<ServerTranslationProps>;
12
12
  export default ServerTranslation;
@@ -1,4 +1,4 @@
1
- import { type I18nOptions } from './types';
1
+ import { type I18nOptions } from "./types";
2
2
  type CreateTranslationReturnType = {
3
3
  t: (term: string, opts?: I18nOptions) => string;
4
4
  lang: string;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- 'use client';
2
+ "use client";
3
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
@@ -10,6 +10,6 @@ const useTranslation_1 = __importDefault(require("./useTranslation"));
10
10
  const ClientTranslation = ({ term, components, query, removeUnusedQueries }) => {
11
11
  const { t } = (0, useTranslation_1.default)();
12
12
  const text = t(term, { query, removeUnusedQueries });
13
- return (react_1.default.createElement(Translation_1.default, { term: term, text: text, components: components }));
13
+ return react_1.default.createElement(Translation_1.default, { term: term, text: text, components: components });
14
14
  };
15
15
  exports.default = ClientTranslation;
@@ -12,7 +12,7 @@ const formatServerTranslate_1 = __importDefault(require("./lib/formatServerTrans
12
12
  const formatServerTranslates = (result, targetKey, translates, opts = {}) => {
13
13
  if (!translates)
14
14
  return;
15
- if (typeof translates === 'string') {
15
+ if (typeof translates === "string") {
16
16
  // eslint-disable-next-line no-param-reassign
17
17
  result[targetKey] = (0, formatServerTranslate_1.default)({ term: targetKey, text: translates, parseEntities: true, ...opts });
18
18
  }
@@ -25,7 +25,7 @@ const formatServerTranslates = (result, targetKey, translates, opts = {}) => {
25
25
  const I18nTransmitter = async ({ terms, children, cleanThread }) => {
26
26
  const context = (0, get_server_context_1.default)(I18nContext_1.I18nContext);
27
27
  if (!context) {
28
- throw new Error('Please, Init I18nProvider - https://github.com/vordgi/nimpl-i18n#server-components');
28
+ throw new Error("Please, Init I18nProvider - https://github.com/vordgi/nimpl-i18n#server-components");
29
29
  }
30
30
  const { dictionary, lang } = context;
31
31
  const result = {};
@@ -9,6 +9,6 @@ const getTranslation_1 = __importDefault(require("./getTranslation"));
9
9
  const ServerTranslation = ({ term, components, query, removeUnusedQueries }) => {
10
10
  const { t } = (0, getTranslation_1.default)();
11
11
  const text = t(term, { query, removeUnusedQueries });
12
- return (react_1.default.createElement(Translation_1.default, { term: term, text: text, components: components }));
12
+ return react_1.default.createElement(Translation_1.default, { term: term, text: text, components: components });
13
13
  };
14
14
  exports.default = ServerTranslation;
@@ -24,7 +24,10 @@ class DataLoader {
24
24
  }
25
25
  }
26
26
  async actualizeData(lang, lastLoadMeta) {
27
- const isOutdatedCache = !lastLoadMeta || (this.revalidate !== undefined && this.revalidate !== false && (lastLoadMeta.lastUpdated + (1000 * this.revalidate) < Date.now()));
27
+ const isOutdatedCache = !lastLoadMeta ||
28
+ (this.revalidate !== undefined &&
29
+ this.revalidate !== false &&
30
+ lastLoadMeta.lastUpdated + 1000 * this.revalidate < Date.now());
28
31
  if (isOutdatedCache) {
29
32
  this.cache[lang] = new Promise(async (resolve) => {
30
33
  if (this.checkIsActual) {
@@ -75,7 +78,7 @@ class DataLoader {
75
78
  if (isCacheExist) {
76
79
  return this.cacheHandler.get(lang);
77
80
  }
78
- throw new Error('Can\'t load data or read from cache');
81
+ throw new Error("Can't load data or read from cache");
79
82
  }
80
83
  async revalidateTag(lang) {
81
84
  const prevMeta = this.cache[lang];
@@ -93,9 +96,9 @@ class DataLoader {
93
96
  }
94
97
  catch {
95
98
  if (attempts === this.retryAttempts) {
96
- throw new Error('Can\'t load data');
99
+ throw new Error("Can't load data");
97
100
  }
98
- console.warn('Can\'t load data, trying again...');
101
+ console.warn("Can't load data, trying again...");
99
102
  call();
100
103
  }
101
104
  };
@@ -8,16 +8,16 @@ const getConfig_1 = __importDefault(require("./getConfig"));
8
8
  const http_1 = __importDefault(require("http"));
9
9
  const DataLoader_1 = __importDefault(require("./DataLoader"));
10
10
  async function createCacheServer(port) {
11
- const serverSecret = crypto_1.default.randomBytes(32).toString('hex');
11
+ const serverSecret = crypto_1.default.randomBytes(32).toString("hex");
12
12
  const config = await (0, getConfig_1.default)();
13
13
  const dataLoader = new DataLoader_1.default(config);
14
14
  const server = http_1.default.createServer(async (req, res) => {
15
15
  try {
16
- const url = new URL(req.url || '/', 'http://n');
17
- const secret = url.searchParams.get('secret');
18
- const lang = url.searchParams.get('lang');
19
- const type = url.searchParams.get('type');
20
- if (type === 'dev' && process.env.NODE_ENV === 'development') {
16
+ const url = new URL(req.url || "/", "http://n");
17
+ const secret = url.searchParams.get("secret");
18
+ const lang = url.searchParams.get("lang");
19
+ const type = url.searchParams.get("type");
20
+ if (type === "dev" && process.env.NODE_ENV === "development") {
21
21
  return res.end(JSON.stringify({ secret: serverSecret }));
22
22
  }
23
23
  if (secret !== serverSecret || !lang) {
@@ -27,21 +27,21 @@ async function createCacheServer(port) {
27
27
  res.end(JSON.stringify(data));
28
28
  }
29
29
  catch (e) {
30
- console.log('error on data loading', e);
30
+ console.log("error on data loading", e);
31
31
  }
32
32
  });
33
33
  await new Promise((resolve) => {
34
- server.listen(+port, 'localhost', () => {
34
+ server.listen(+port, "localhost", () => {
35
35
  resolve(1);
36
36
  });
37
37
  });
38
- process.on('SIGINT', () => {
38
+ process.on("SIGINT", () => {
39
39
  server.close();
40
40
  });
41
- process.on('SIGQUIT', () => {
41
+ process.on("SIGQUIT", () => {
42
42
  server.close();
43
43
  });
44
- process.on('SIGTERM', () => {
44
+ process.on("SIGTERM", () => {
45
45
  server.close();
46
46
  });
47
47
  return {
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const fs_1 = __importDefault(require("fs"));
7
7
  const path_1 = __importDefault(require("path"));
8
8
  const url_1 = require("url");
9
- const CONFIG_PATH = path_1.default.join(process.cwd(), 'nimpl-i18n.js');
9
+ const CONFIG_PATH = path_1.default.join(process.cwd(), "nimpl-i18n.js");
10
10
  // Crutch bypass of conversion by the assembler to require
11
- const dynamicImport = new Function('p', 'return import(p)');
11
+ const dynamicImport = new Function("p", "return import(p)");
12
12
  const getConfig = async () => {
13
13
  try {
14
14
  if (fs_1.default.existsSync(CONFIG_PATH)) {
@@ -26,6 +26,6 @@ const getConfig = async () => {
26
26
  catch {
27
27
  //
28
28
  }
29
- throw new Error('Can\'t load config - https://github.com/vordgi/nimpl-i18n#configuration');
29
+ throw new Error("Can't load config - https://github.com/vordgi/nimpl-i18n#configuration");
30
30
  };
31
31
  exports.default = getConfig;
@@ -13,13 +13,13 @@ const createTranslation = async (lang, namespace) => {
13
13
  let termDictionary = namespaceDictionary;
14
14
  let termNamespace = namespace;
15
15
  let termKey = term;
16
- if (term.includes(':')) {
17
- [termNamespace, termKey] = term.split(':');
16
+ if (term.includes(":")) {
17
+ [termNamespace, termKey] = term.split(":");
18
18
  termDictionary = object_path_1.default.get(dictionary, termNamespace);
19
19
  }
20
- const translation = object_path_1.default.get(namespaceDictionary, termKey);
21
- const fullTerm = `${termNamespace ? `${termNamespace}.` : ''}${termKey}`;
22
- if (typeof translation !== 'string' || !translation)
20
+ const translation = object_path_1.default.get(termDictionary, termKey);
21
+ const fullTerm = `${termNamespace ? `${termNamespace}.` : ""}${termKey}`;
22
+ if (typeof translation !== "string" || !translation)
23
23
  return fullTerm;
24
24
  return (0, formatServerTranslate_1.default)({ term: fullTerm, text: translation, parseEntities: true, ...opts });
25
25
  };
@@ -10,7 +10,7 @@ const formatServerTranslate_1 = __importDefault(require("./lib/formatServerTrans
10
10
  const getTranslation = (namespace) => {
11
11
  const context = (0, get_server_context_1.default)(I18nContext_1.I18nContext);
12
12
  if (!context) {
13
- throw new Error('Please, Init I18nProvider - https://nimpl.tech/i18n/usage#i18nprovider');
13
+ throw new Error("Please, Init I18nProvider - https://nimpl.tech/i18n/usage#i18nprovider");
14
14
  }
15
15
  const { dictionary, lang } = context;
16
16
  const namespaceDictionary = namespace ? object_path_1.default.get(dictionary, namespace) : dictionary;
@@ -18,13 +18,13 @@ const getTranslation = (namespace) => {
18
18
  let termDictionary = namespaceDictionary;
19
19
  let termNamespace = namespace;
20
20
  let termKey = term;
21
- if (term.includes(':')) {
22
- [termNamespace, termKey] = term.split(':');
21
+ if (term.includes(":")) {
22
+ [termNamespace, termKey] = term.split(":");
23
23
  termDictionary = object_path_1.default.get(dictionary, termNamespace);
24
24
  }
25
- const translation = object_path_1.default.get(namespaceDictionary, termKey);
26
- const fullTerm = `${termNamespace ? `${termNamespace}.` : ''}${termKey}`;
27
- if (typeof translation !== 'string' || !translation)
25
+ const translation = object_path_1.default.get(termDictionary, termKey);
26
+ const fullTerm = `${termNamespace ? `${termNamespace}.` : ""}${termKey}`;
27
+ if (typeof translation !== "string" || !translation)
28
28
  return fullTerm;
29
29
  return (0, formatServerTranslate_1.default)({ term: fullTerm, text: translation, parseEntities: true, ...opts });
30
30
  };
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isPromise = void 0;
4
4
  const isPromise = (el) => {
5
- if (el && typeof el === 'object' && 'then' in el) {
5
+ if (el && typeof el === "object" && "then" in el) {
6
6
  return true;
7
7
  }
8
8
  return false;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- 'use client';
2
+ "use client";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.ClientI18nContext = void 0;
5
5
  const react_1 = require("react");
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- 'use client';
2
+ "use client";
3
3
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
4
  if (k2 === undefined) k2 = k;
5
5
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -31,6 +31,6 @@ const ClientI18nProvider = ({ translates, children, lang, cleanThread }) => {
31
31
  if (cleanThread) {
32
32
  Object.assign(translates, prevTranslates?.translates);
33
33
  }
34
- return (react_1.default.createElement(ClientI18nContext_1.ClientI18nContext.Provider, { value: { lang, translates } }, children));
34
+ return react_1.default.createElement(ClientI18nContext_1.ClientI18nContext.Provider, { value: { lang, translates } }, children);
35
35
  };
36
36
  exports.default = ClientI18nProvider;
@@ -3,10 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- /* eslint-disable no-console, react/no-array-index-key */
7
6
  const react_1 = __importDefault(require("react"));
8
7
  const Translation = ({ term, text, components }) => {
9
- const parts = text.split(/<\/?[a-zA-Z0-9]+>|<[a-zA-Z0-9]+ ?\/>/gm).map((el, i) => react_1.default.createElement(react_1.default.Fragment, { key: `p-${i}` }, el));
8
+ const parts = text
9
+ .split(/<\/?[a-zA-Z0-9]+>|<[a-zA-Z0-9]+ ?\/>/gm)
10
+ .map((el, i) => react_1.default.createElement(react_1.default.Fragment, { key: `p-${i}` }, el));
10
11
  if (components) {
11
12
  const tags = text.match(/<\/?[a-zA-Z0-9]+>|<[a-zA-Z0-9]+ ?\/>/gm);
12
13
  const openedTags = [];
@@ -32,7 +33,8 @@ const Translation = ({ term, text, components }) => {
32
33
  const targetTag = openedTags[targetIndex];
33
34
  const component = components[targetTag.tag];
34
35
  if (component) {
35
- const children = parts.slice(targetTag.position + 1, tagIndex + 1)
36
+ const children = parts
37
+ .slice(targetTag.position + 1, tagIndex + 1)
36
38
  .filter((c) => Boolean(c.props.children));
37
39
  parts.splice(targetTag.position + 1, // parts на 1 больше
38
40
  tagIndex - targetTag.position, react_1.default.cloneElement(component, { key: `${tagIndex}-${targetIndex}` }, children.length ? children : component.props.children));
@@ -49,6 +51,6 @@ const Translation = ({ term, text, components }) => {
49
51
  }
50
52
  });
51
53
  }
52
- return (parts);
54
+ return parts;
53
55
  };
54
56
  exports.default = Translation;
@@ -11,7 +11,7 @@ const formatServerTranslate = ({ term, text, removeUnusedQueries, query, parseEn
11
11
  newTranslate = (0, injectQuery_1.default)({ term, text: newTranslate, query, removeUnusedQueries });
12
12
  }
13
13
  if (parseEntities === undefined || parseEntities === true) {
14
- newTranslate = (0, html_entities_1.decode)(newTranslate, { scope: 'strict' });
14
+ newTranslate = (0, html_entities_1.decode)(newTranslate, { scope: "strict" });
15
15
  }
16
16
  return newTranslate;
17
17
  };
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const getDictionary = async (lang) => {
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
5
  const origFetch = globalThis._nextOriginalFetch || fetch;
5
- const dataResp = await origFetch(`http://localhost:${process.env.I18N_CACHE_PORT}/?secret=${process.env.I18N_CACHE_SECRET}&lang=${lang}`, { cache: 'no-cache' });
6
+ const dataResp = await origFetch(`http://localhost:${process.env.I18N_CACHE_PORT}/?secret=${process.env.I18N_CACHE_SECRET}&lang=${lang}`, { cache: "no-cache" });
6
7
  const data = await dataResp.json();
7
8
  return data;
8
9
  };
@@ -6,7 +6,7 @@ const injectQuery = ({ text, query, removeUnusedQueries }) => {
6
6
  return query[g1].toString();
7
7
  }
8
8
  if (removeUnusedQueries) {
9
- return '';
9
+ return "";
10
10
  }
11
11
  else {
12
12
  return matched;
@@ -2,6 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const html_entities_1 = require("html-entities");
4
4
  const parseEntities = (translate) => {
5
- return (0, html_entities_1.decode)(translate, { scope: 'strict' });
5
+ return (0, html_entities_1.decode)(translate, { scope: "strict" });
6
6
  };
7
7
  exports.default = parseEntities;
@@ -9,22 +9,27 @@ const injectQuery_1 = __importDefault(require("./lib/injectQuery"));
9
9
  const useTranslation = (namespace) => {
10
10
  const context = (0, react_1.useContext)(ClientI18nContext_1.ClientI18nContext);
11
11
  if (!context) {
12
- throw new Error('Please, Init I18nTransmitter for client components - https://nimpl.tech/i18n/usage#client-components');
12
+ throw new Error("Please, Init I18nTransmitter for client components - https://nimpl.tech/i18n/usage#client-components");
13
13
  }
14
14
  const { lang, translates } = context;
15
15
  const t = (term, opts) => {
16
16
  let termKey;
17
- if (term.includes(':')) {
18
- termKey = term.replace(':', '.');
17
+ if (term.includes(":")) {
18
+ termKey = term.replace(":", ".");
19
19
  }
20
20
  else {
21
- termKey = `${namespace ? `${namespace}.` : ''}${term}`;
21
+ termKey = `${namespace ? `${namespace}.` : ""}${term}`;
22
22
  }
23
23
  const translation = translates[termKey];
24
24
  if (!translation)
25
25
  return termKey;
26
26
  if (opts?.query) {
27
- return (0, injectQuery_1.default)({ term, text: translation, query: opts.query, removeUnusedQueries: opts.removeUnusedQueries });
27
+ return (0, injectQuery_1.default)({
28
+ term,
29
+ text: translation,
30
+ query: opts.query,
31
+ removeUnusedQueries: opts.removeUnusedQueries,
32
+ });
28
33
  }
29
34
  return translation;
30
35
  };
package/dist/withI18n.js CHANGED
@@ -4,10 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const createCacheServer_1 = __importDefault(require("./configuration/createCacheServer"));
7
- const PORT = '24';
7
+ const PORT = "24";
8
8
  let cacheSecret = process.env.I18N_CACHE_SECRET;
9
9
  const withI18n = async (phase) => {
10
- if (!cacheSecret && process.env.NODE_ENV === 'development') {
10
+ if (!cacheSecret && process.env.NODE_ENV === "development") {
11
11
  try {
12
12
  const devResp = await fetch(`http://localhost:${PORT}/?type=dev`);
13
13
  const data = await devResp.json();
@@ -19,13 +19,14 @@ const withI18n = async (phase) => {
19
19
  //
20
20
  }
21
21
  }
22
- if ((phase === 'phase-development-server' || phase === 'phase-production-server') && !cacheSecret) {
22
+ if ((phase === "phase-development-server" || phase === "phase-production-server") && !cacheSecret) {
23
23
  const { secret } = await (0, createCacheServer_1.default)(PORT);
24
24
  cacheSecret = secret;
25
25
  }
26
26
  process.env.I18N_CACHE_PORT = PORT;
27
27
  if (cacheSecret)
28
28
  process.env.I18N_CACHE_SECRET = cacheSecret;
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
30
  return (nextConfig) => nextConfig;
30
31
  };
31
32
  exports.default = withI18n;
@@ -1,4 +1,4 @@
1
- import { type I18nOptions } from './types';
1
+ import { type I18nOptions } from "./types";
2
2
  type GetTranslationReturnType = {
3
3
  t: (term: string, opts?: I18nOptions) => string;
4
4
  lang: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nimpl/i18n",
3
- "version": "1.1.1",
3
+ "version": "1.1.2-canary.1",
4
4
  "description": "i18n library for working with translations in server and client components",
5
5
  "exports": {
6
6
  "./ClientTranslation": {
@@ -1,4 +1,4 @@
1
- import { type I18nOptions } from './types';
1
+ import { type I18nOptions } from "./types";
2
2
  type GetTranslationReturnType = {
3
3
  t: (term: string, opts?: I18nOptions) => string;
4
4
  lang: string;