@nimpl/i18n 2.0.0-canary.0 → 2.0.0-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.
@@ -4,6 +4,7 @@ export type I18nTransmitterProps = {
4
4
  terms: (string | [string, I18nOptions])[];
5
5
  children: React.ReactNode;
6
6
  cleanThread?: boolean;
7
+ language?: string;
7
8
  };
8
9
  declare const I18nTransmitter: React.FC<I18nTransmitterProps>;
9
10
  export default I18nTransmitter;
@@ -7,6 +7,7 @@ type ServerTranslationProps = {
7
7
  };
8
8
  query?: I18nOptions["query"];
9
9
  removeUnusedQueries?: I18nOptions["removeUnusedQueries"];
10
+ language?: string;
10
11
  };
11
12
  declare const ServerTranslation: React.FC<ServerTranslationProps>;
12
13
  export default ServerTranslation;
@@ -5,11 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const react_1 = __importDefault(require("react"));
7
7
  const object_path_1 = __importDefault(require("object-path"));
8
- const get_server_context_1 = __importDefault(require("@nimpl/getters/get-server-context"));
9
- const I18nContext_1 = require("./lib/I18nContext");
10
8
  const ClientI18nProvider_1 = __importDefault(require("./lib/ClientI18nProvider"));
11
9
  const formatServerTranslate_1 = __importDefault(require("./lib/formatServerTranslate"));
12
- const getDictionary_1 = __importDefault(require("./lib/getDictionary"));
10
+ const loadI18nData_1 = __importDefault(require("./lib/loadI18nData"));
13
11
  const formatServerTranslates = (result, targetKey, translates, opts = {}) => {
14
12
  if (!translates)
15
13
  return;
@@ -23,13 +21,12 @@ const formatServerTranslates = (result, targetKey, translates, opts = {}) => {
23
21
  });
24
22
  }
25
23
  };
26
- const I18nTransmitter = async ({ terms, children, cleanThread }) => {
27
- const context = (0, get_server_context_1.default)(I18nContext_1.I18nContext);
28
- if (!context) {
29
- throw new Error("Please, Init I18nProvider - https://github.com/vordgi/nimpl-i18n#server-components");
24
+ const I18nTransmitter = async ({ language: argLanguage, terms, children, cleanThread, }) => {
25
+ const { dictionary, language: configLanguage } = await (0, loadI18nData_1.default)();
26
+ const language = argLanguage || configLanguage;
27
+ if (!language) {
28
+ throw new Error("Unable to get the language in the createTranslation function. Please check the getLanguage method in the configuration file or pass the language as an argument.");
30
29
  }
31
- const { lang } = context;
32
- const dictionary = await (0, getDictionary_1.default)(lang);
33
30
  const result = {};
34
31
  terms.forEach((term) => {
35
32
  if (Array.isArray(term)) {
@@ -42,6 +39,6 @@ const I18nTransmitter = async ({ terms, children, cleanThread }) => {
42
39
  formatServerTranslates(result, term, translates);
43
40
  }
44
41
  });
45
- return (react_1.default.createElement(ClientI18nProvider_1.default, { lang: lang, translates: result, cleanThread: cleanThread }, children));
42
+ return (react_1.default.createElement(ClientI18nProvider_1.default, { language: language, translates: result, cleanThread: cleanThread }, children));
46
43
  };
47
44
  exports.default = I18nTransmitter;
@@ -6,8 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const react_1 = __importDefault(require("react"));
7
7
  const Translation_1 = __importDefault(require("./lib/Translation"));
8
8
  const getTranslation_1 = __importDefault(require("./getTranslation"));
9
- const ServerTranslation = async ({ term, components, query, removeUnusedQueries, }) => {
10
- const { t } = await (0, getTranslation_1.default)();
9
+ const ServerTranslation = async ({ term, components, query, removeUnusedQueries, language, }) => {
10
+ const { t } = await (0, getTranslation_1.default)({ language });
11
11
  const text = t(term, { query, removeUnusedQueries });
12
12
  return react_1.default.createElement(Translation_1.default, { term: term, text: text, components: components });
13
13
  };
@@ -13,18 +13,21 @@ const getConfig = async () => {
13
13
  try {
14
14
  if (fs_1.default.existsSync(CONFIG_PATH)) {
15
15
  const config = await dynamicImport((0, url_1.pathToFileURL)(CONFIG_PATH).href);
16
- const { load, languages } = config.default;
16
+ const { load, getLanguage, languages } = config.default;
17
17
  if (!load) {
18
18
  throw new Error(`Can't find load method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`);
19
19
  }
20
20
  if (!languages) {
21
21
  throw new Error(`Can't find languages list in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`);
22
22
  }
23
+ if (!getLanguage) {
24
+ throw new Error(`Can't find getLanguage method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`);
25
+ }
23
26
  return config.default;
24
27
  }
25
28
  }
26
- catch {
27
- //
29
+ catch (err) {
30
+ console.error(err);
28
31
  }
29
32
  throw new Error("Can't load config - https://github.com/vordgi/nimpl-i18n#configuration");
30
33
  };
@@ -4,17 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const object_path_1 = __importDefault(require("object-path"));
7
- const get_server_context_1 = __importDefault(require("@nimpl/getters/get-server-context"));
8
- const I18nContext_1 = require("./lib/I18nContext");
9
7
  const formatServerTranslate_1 = __importDefault(require("./lib/formatServerTranslate"));
10
- const getDictionary_1 = __importDefault(require("./lib/getDictionary"));
11
- const getTranslation = async (namespace) => {
12
- const context = (0, get_server_context_1.default)(I18nContext_1.I18nContext);
13
- if (!context) {
14
- throw new Error("Please, Init I18nProvider - https://nimpl.tech/i18n/usage#i18nprovider");
8
+ const loadI18nData_1 = __importDefault(require("./lib/loadI18nData"));
9
+ const getTranslation = async (options) => {
10
+ const { language: argLanguage, namespace } = options || {};
11
+ const { dictionary, language: configLanguage } = await (0, loadI18nData_1.default)();
12
+ const language = argLanguage || configLanguage;
13
+ if (!language) {
14
+ throw new Error("Unable to get the language in the createTranslation function. Please check the getLanguage method in the configuration file or pass the language as an argument.");
15
15
  }
16
- const dictionary = await (0, getDictionary_1.default)(context.lang);
17
- const { lang } = context;
18
16
  const namespaceDictionary = namespace ? object_path_1.default.get(dictionary, namespace) : dictionary;
19
17
  const t = (term, opts) => {
20
18
  let termDictionary = namespaceDictionary;
@@ -30,6 +28,6 @@ const getTranslation = async (namespace) => {
30
28
  return fullTerm;
31
29
  return (0, formatServerTranslate_1.default)({ term: fullTerm, text: translation, parseEntities: true, ...opts });
32
30
  };
33
- return { t, lang };
31
+ return { t, language };
34
32
  };
35
33
  exports.default = getTranslation;
@@ -26,11 +26,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
27
  const react_1 = __importStar(require("react"));
28
28
  const ClientI18nContext_1 = require("./ClientI18nContext");
29
- const ClientI18nProvider = ({ translates, children, lang, cleanThread }) => {
29
+ const ClientI18nProvider = ({ translates, children, language, cleanThread }) => {
30
30
  const prevTranslates = (0, react_1.useContext)(ClientI18nContext_1.ClientI18nContext);
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: { language, translates } }, children);
35
35
  };
36
36
  exports.default = ClientI18nProvider;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getConfig_1 = __importDefault(require("../configuration/getConfig"));
7
+ const get_pathname_1 = require("@nimpl/getters/get-pathname");
8
+ const get_params_1 = require("@nimpl/getters/get-params");
9
+ const loadI18nData = async () => {
10
+ const config = await (0, getConfig_1.default)();
11
+ const language = await config.getLanguage({ pathname: (0, get_pathname_1.getPathname)(), params: (0, get_params_1.getParams)() });
12
+ if (!language || !config.languages.includes(language)) {
13
+ throw new Error(`Can\' load data for language "${language}", valid languages are: ${config.languages.join(", ")}`);
14
+ }
15
+ const { data } = await config.load(language);
16
+ return { dictionary: data, language };
17
+ };
18
+ exports.default = loadI18nData;
@@ -6,12 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const react_1 = require("react");
7
7
  const ClientI18nContext_1 = require("./lib/ClientI18nContext");
8
8
  const injectQuery_1 = __importDefault(require("./lib/injectQuery"));
9
- const useTranslation = (namespace) => {
9
+ const useTranslation = ({ namespace } = {}) => {
10
10
  const context = (0, react_1.useContext)(ClientI18nContext_1.ClientI18nContext);
11
11
  if (!context) {
12
12
  throw new Error("Please, Init I18nTransmitter for client components - https://nimpl.tech/i18n/usage#client-components");
13
13
  }
14
- const { lang, translates } = context;
14
+ const { language, translates } = context;
15
15
  const t = (term, opts) => {
16
16
  let termKey;
17
17
  if (term.includes(":")) {
@@ -33,6 +33,6 @@ const useTranslation = (namespace) => {
33
33
  }
34
34
  return translation;
35
35
  };
36
- return { t, lang };
36
+ return { t, language };
37
37
  };
38
38
  exports.default = useTranslation;
@@ -1,7 +1,10 @@
1
1
  import { type I18nOptions } from "./types";
2
2
  type GetTranslationReturnType = {
3
3
  t: (term: string, opts?: I18nOptions) => string;
4
- lang: string;
4
+ language: string;
5
5
  };
6
- declare const getTranslation: (namespace?: string) => Promise<GetTranslationReturnType>;
6
+ declare const getTranslation: (options?: {
7
+ language?: string;
8
+ namespace?: string;
9
+ }) => Promise<GetTranslationReturnType>;
7
10
  export default getTranslation;
package/package.json CHANGED
@@ -1,20 +1,14 @@
1
1
  {
2
2
  "name": "@nimpl/i18n",
3
- "version": "2.0.0-canary.0",
3
+ "version": "2.0.0-canary.1",
4
4
  "description": "i18n library for working with translations in server and client components",
5
5
  "exports": {
6
6
  "./ClientTranslation": {
7
7
  "default": "./dist/ClientTranslation.js"
8
8
  },
9
- "./createTranslation": {
10
- "default": "./dist/createTranslation.js"
11
- },
12
9
  "./getTranslation": {
13
10
  "default": "./dist/getTranslation.js"
14
11
  },
15
- "./I18nProvider": {
16
- "default": "./dist/I18nProvider.js"
17
- },
18
12
  "./I18nTransmitter": {
19
13
  "default": "./dist/I18nTransmitter.js"
20
14
  },
@@ -26,9 +20,6 @@
26
20
  },
27
21
  "./useTranslation": {
28
22
  "default": "./dist/useTranslation.js"
29
- },
30
- "./withI18n": {
31
- "default": "./dist/withI18n.js"
32
23
  }
33
24
  },
34
25
  "files": [
@@ -70,8 +61,8 @@
70
61
  "react": ">= 18.2.0"
71
62
  },
72
63
  "dependencies": {
73
- "html-entities": "2.4.0",
74
64
  "@nimpl/getters": "1.3.4",
65
+ "html-entities": "2.4.0",
75
66
  "object-path": "0.11.8"
76
67
  }
77
68
  }
@@ -1,7 +1,9 @@
1
1
  import { type I18nOptions } from "./types";
2
2
  type GetTranslationReturnType = {
3
3
  t: (term: string, opts?: I18nOptions) => string;
4
- lang: string;
4
+ language: string;
5
5
  };
6
- declare const useTranslation: (namespace?: string) => GetTranslationReturnType;
6
+ declare const useTranslation: ({ namespace }?: {
7
+ namespace?: string | undefined;
8
+ }) => GetTranslationReturnType;
7
9
  export default useTranslation;
package/I18nProvider.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import React from "react";
2
- import { type I18nTransmitterProps } from "./I18nTransmitter";
3
- type I18nProviderProps = {
4
- lang: string;
5
- children: React.ReactNode;
6
- clientTerms?: I18nTransmitterProps["terms"];
7
- };
8
- declare const I18nProvider: React.FC<I18nProviderProps>;
9
- export default I18nProvider;
@@ -1,7 +0,0 @@
1
- import { type I18nOptions } from "./types";
2
- type CreateTranslationReturnType = {
3
- t: (term: string, opts?: I18nOptions) => string;
4
- lang: string;
5
- };
6
- declare const createTranslation: (lang: string, namespace?: string) => Promise<CreateTranslationReturnType>;
7
- export default createTranslation;
@@ -1,13 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const react_1 = __importDefault(require("react"));
7
- const I18nContext_1 = require("./lib/I18nContext");
8
- const I18nTransmitter_1 = __importDefault(require("./I18nTransmitter"));
9
- const I18nProvider = ({ children, lang, clientTerms = [] }) => {
10
- return (react_1.default.createElement(I18nContext_1.I18nContext.Provider, { value: { lang } },
11
- react_1.default.createElement(I18nTransmitter_1.default, { terms: clientTerms, cleanThread: true }, children)));
12
- };
13
- exports.default = I18nProvider;
@@ -1,28 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const object_path_1 = __importDefault(require("object-path"));
7
- const formatServerTranslate_1 = __importDefault(require("./lib/formatServerTranslate"));
8
- const getDictionary_1 = __importDefault(require("./lib/getDictionary"));
9
- const createTranslation = async (lang, namespace) => {
10
- const dictionary = await (0, getDictionary_1.default)(lang);
11
- const namespaceDictionary = namespace ? object_path_1.default.get(dictionary, namespace) : dictionary;
12
- const t = (term, opts) => {
13
- let termDictionary = namespaceDictionary;
14
- let termNamespace = namespace;
15
- let termKey = term;
16
- if (term.includes(":")) {
17
- [termNamespace, termKey] = term.split(":");
18
- termDictionary = object_path_1.default.get(dictionary, termNamespace);
19
- }
20
- const translation = object_path_1.default.get(termDictionary, termKey);
21
- const fullTerm = `${termNamespace ? `${termNamespace}.` : ""}${termKey}`;
22
- if (typeof translation !== "string" || !translation)
23
- return fullTerm;
24
- return (0, formatServerTranslate_1.default)({ term: fullTerm, text: translation, parseEntities: true, ...opts });
25
- };
26
- return { t, lang };
27
- };
28
- exports.default = createTranslation;
@@ -1,8 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.I18nContext = void 0;
7
- const create_server_context_1 = __importDefault(require("@nimpl/getters/create-server-context"));
8
- exports.I18nContext = (0, create_server_context_1.default)(null);
@@ -1,15 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const getConfig_1 = __importDefault(require("../configuration/getConfig"));
7
- const getDictionary = async (lang) => {
8
- const config = await (0, getConfig_1.default)();
9
- if (!lang || !config.languages.includes(lang)) {
10
- throw new Error(`Can\' load data for language "${lang}", valid languages are: ${config.languages.join(", ")}`);
11
- }
12
- const { data } = await config.load(lang);
13
- return data;
14
- };
15
- exports.default = getDictionary;