@wix/essentials 1.0.0 → 1.0.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.
@@ -2,5 +2,5 @@ import { Host } from '@wix/sdk-types';
2
2
  interface Messages {
3
3
  [key: string]: string;
4
4
  }
5
- export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next", { with: { "resolution-mode": "require" } }).i18n;
5
+ export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next").i18n;
6
6
  export {};
@@ -1,7 +1,10 @@
1
+ import { TFunction } from 'i18next';
1
2
  export declare const i18n: {
2
3
  getLanguage: () => string;
3
4
  getLocale: () => string;
5
+ useTranslation: () => TFunction;
4
6
  } & import("@wix/sdk-types").HostModule<{
5
7
  getLanguage: () => string;
6
8
  getLocale: () => string;
9
+ useTranslation: () => TFunction;
7
10
  }, import("@wix/sdk-types").Host>;
@@ -1,4 +1,11 @@
1
1
  import { createHostModule } from '@wix/sdk-runtime/host-modules';
2
+ import i18next from 'i18next';
3
+ import { initReactI18next } from 'react-i18next';
4
+ import { multilingual } from '@wix/site';
5
+ function getPrimaryLanguage(fallback) {
6
+ return (multilingual.listSupportedLanguages().find((language) => language.primary)
7
+ ?.id ?? fallback);
8
+ }
2
9
  export const i18n = createHostModule({
3
10
  getLanguage: (host) => () => {
4
11
  const language = host.essentials?.language;
@@ -14,4 +21,25 @@ export const i18n = createHostModule({
14
21
  }
15
22
  return locale;
16
23
  },
24
+ useTranslation: (host) => () => {
25
+ const resources = host.translations?.();
26
+ if (!resources) {
27
+ throw new Error('Host translation resources are not available.');
28
+ }
29
+ const instance = i18next.createInstance();
30
+ const mainLanguage = getPrimaryLanguage(host.essentials?.language) || 'en';
31
+ const initOptions = {
32
+ lng: host.essentials?.language || mainLanguage,
33
+ fallbackLng: mainLanguage,
34
+ resources,
35
+ };
36
+ // Init is asynchronous, but when provided with a synchronous resource,
37
+ // it inits immediately and the callback ensures synchronous completion.
38
+ void instance.use(initReactI18next).init(initOptions, (err) => {
39
+ if (err) {
40
+ throw err;
41
+ }
42
+ });
43
+ return instance.t.bind(instance);
44
+ },
17
45
  });
@@ -1,7 +1,10 @@
1
+ import { TFunction } from 'i18next';
1
2
  export declare const i18n: {
2
3
  getLanguage: () => string;
3
4
  getLocale: () => string;
5
+ useTranslation: () => TFunction;
4
6
  } & import("@wix/sdk-types").HostModule<{
5
7
  getLanguage: () => string;
6
8
  getLocale: () => string;
9
+ useTranslation: () => TFunction;
7
10
  }, import("@wix/sdk-types").Host>;
@@ -1,7 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.i18n = void 0;
4
7
  const host_modules_1 = require("@wix/sdk-runtime/host-modules");
8
+ const i18next_1 = __importDefault(require("i18next"));
9
+ const react_i18next_1 = require("react-i18next");
10
+ const site_1 = require("@wix/site");
11
+ function getPrimaryLanguage(fallback) {
12
+ return (site_1.multilingual.listSupportedLanguages().find((language) => language.primary)
13
+ ?.id ?? fallback);
14
+ }
5
15
  exports.i18n = (0, host_modules_1.createHostModule)({
6
16
  getLanguage: (host) => () => {
7
17
  const language = host.essentials?.language;
@@ -17,4 +27,25 @@ exports.i18n = (0, host_modules_1.createHostModule)({
17
27
  }
18
28
  return locale;
19
29
  },
30
+ useTranslation: (host) => () => {
31
+ const resources = host.translations?.();
32
+ if (!resources) {
33
+ throw new Error('Host translation resources are not available.');
34
+ }
35
+ const instance = i18next_1.default.createInstance();
36
+ const mainLanguage = getPrimaryLanguage(host.essentials?.language) || 'en';
37
+ const initOptions = {
38
+ lng: host.essentials?.language || mainLanguage,
39
+ fallbackLng: mainLanguage,
40
+ resources,
41
+ };
42
+ // Init is asynchronous, but when provided with a synchronous resource,
43
+ // it inits immediately and the callback ensures synchronous completion.
44
+ void instance.use(react_i18next_1.initReactI18next).init(initOptions, (err) => {
45
+ if (err) {
46
+ throw err;
47
+ }
48
+ });
49
+ return instance.t.bind(instance);
50
+ },
20
51
  });
@@ -2,5 +2,5 @@ import { Host } from '@wix/sdk-types';
2
2
  interface Messages {
3
3
  [key: string]: string;
4
4
  }
5
- export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next", { with: { "resolution-mode": "require" } }).i18n;
5
+ export declare function createI18n(host: Host, asyncMessagesLoader?: (language: string) => Promise<Messages>): import("i18next").i18n;
6
6
  export {};
@@ -1,7 +1,10 @@
1
+ import { TFunction } from 'i18next';
1
2
  export declare const i18n: {
2
3
  getLanguage: () => string;
3
4
  getLocale: () => string;
5
+ useTranslation: () => TFunction;
4
6
  } & import("@wix/sdk-types").HostModule<{
5
7
  getLanguage: () => string;
6
8
  getLocale: () => string;
9
+ useTranslation: () => TFunction;
7
10
  }, import("@wix/sdk-types").Host>;
@@ -1,4 +1,11 @@
1
1
  import { createHostModule } from '@wix/sdk-runtime/host-modules';
2
+ import i18next from 'i18next';
3
+ import { initReactI18next } from 'react-i18next';
4
+ import { multilingual } from '@wix/site';
5
+ function getPrimaryLanguage(fallback) {
6
+ return (multilingual.listSupportedLanguages().find((language) => language.primary)
7
+ ?.id ?? fallback);
8
+ }
2
9
  export const i18n = createHostModule({
3
10
  getLanguage: (host) => () => {
4
11
  const language = host.essentials?.language;
@@ -14,4 +21,25 @@ export const i18n = createHostModule({
14
21
  }
15
22
  return locale;
16
23
  },
24
+ useTranslation: (host) => () => {
25
+ const resources = host.translations?.();
26
+ if (!resources) {
27
+ throw new Error('Host translation resources are not available.');
28
+ }
29
+ const instance = i18next.createInstance();
30
+ const mainLanguage = getPrimaryLanguage(host.essentials?.language) || 'en';
31
+ const initOptions = {
32
+ lng: host.essentials?.language || mainLanguage,
33
+ fallbackLng: mainLanguage,
34
+ resources,
35
+ };
36
+ // Init is asynchronous, but when provided with a synchronous resource,
37
+ // it inits immediately and the callback ensures synchronous completion.
38
+ void instance.use(initReactI18next).init(initOptions, (err) => {
39
+ if (err) {
40
+ throw err;
41
+ }
42
+ });
43
+ return instance.t.bind(instance);
44
+ },
17
45
  });
@@ -1,7 +1,10 @@
1
+ import { TFunction } from 'i18next';
1
2
  export declare const i18n: {
2
3
  getLanguage: () => string;
3
4
  getLocale: () => string;
5
+ useTranslation: () => TFunction;
4
6
  } & import("@wix/sdk-types").HostModule<{
5
7
  getLanguage: () => string;
6
8
  getLocale: () => string;
9
+ useTranslation: () => TFunction;
7
10
  }, import("@wix/sdk-types").Host>;
@@ -1,7 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.i18n = void 0;
4
7
  const host_modules_1 = require("@wix/sdk-runtime/host-modules");
8
+ const i18next_1 = __importDefault(require("i18next"));
9
+ const react_i18next_1 = require("react-i18next");
10
+ const site_1 = require("@wix/site");
11
+ function getPrimaryLanguage(fallback) {
12
+ return (site_1.multilingual.listSupportedLanguages().find((language) => language.primary)
13
+ ?.id ?? fallback);
14
+ }
5
15
  exports.i18n = (0, host_modules_1.createHostModule)({
6
16
  getLanguage: (host) => () => {
7
17
  const language = host.essentials?.language;
@@ -17,4 +27,25 @@ exports.i18n = (0, host_modules_1.createHostModule)({
17
27
  }
18
28
  return locale;
19
29
  },
30
+ useTranslation: (host) => () => {
31
+ const resources = host.translations?.();
32
+ if (!resources) {
33
+ throw new Error('Host translation resources are not available.');
34
+ }
35
+ const instance = i18next_1.default.createInstance();
36
+ const mainLanguage = getPrimaryLanguage(host.essentials?.language) || 'en';
37
+ const initOptions = {
38
+ lng: host.essentials?.language || mainLanguage,
39
+ fallbackLng: mainLanguage,
40
+ resources,
41
+ };
42
+ // Init is asynchronous, but when provided with a synchronous resource,
43
+ // it inits immediately and the callback ensures synchronous completion.
44
+ void instance.use(react_i18next_1.initReactI18next).init(initOptions, (err) => {
45
+ if (err) {
46
+ throw err;
47
+ }
48
+ });
49
+ return instance.t.bind(instance);
50
+ },
20
51
  });
package/package.json CHANGED
@@ -1,10 +1,35 @@
1
1
  {
2
2
  "name": "@wix/essentials",
3
- "version": "1.0.0",
4
- "license": "MIT",
5
- "main": "cjs/build/index.js",
6
- "module": "build/index.mjs",
7
- "type": "module",
3
+ "version": "1.0.1",
4
+ "dependencies": {
5
+ "@wix/error-handler": "^1.67.0",
6
+ "@wix/monitoring": "^0.21.0",
7
+ "@wix/sdk-runtime": "1.0.3",
8
+ "@wix/sdk-types": "1.17.1",
9
+ "@wix/site": "^1.37.0",
10
+ "i18next": "^25.6.3",
11
+ "i18next-icu": "^2.4.1",
12
+ "intl-messageformat": "^10.7.18",
13
+ "react-i18next": "^16.1.2"
14
+ },
15
+ "devDependencies": {
16
+ "@types/is-ci": "^3.0.4",
17
+ "@types/node": "^20.19.25",
18
+ "@vitest/ui": "^1.6.1",
19
+ "@wix/sdk": "1.21.1",
20
+ "eslint": "^8.57.1",
21
+ "eslint-config-sdk": "1.0.0",
22
+ "graphql": "^16.8.0",
23
+ "is-ci": "^3.0.1",
24
+ "jsdom": "^22.1.0",
25
+ "msw": "^2.12.2",
26
+ "typescript": "^5.9.3",
27
+ "vitest": "^1.6.1",
28
+ "vitest-teamcity-reporter": "^0.3.1"
29
+ },
30
+ "eslintConfig": {
31
+ "extends": "sdk"
32
+ },
8
33
  "exports": {
9
34
  ".": {
10
35
  "import": "./build/index.js",
@@ -16,59 +41,36 @@
16
41
  },
17
42
  "./package.json": "./package.json"
18
43
  },
19
- "sideEffects": false,
20
- "keywords": [
21
- "wix-sdk-module=backend,page,public"
22
- ],
23
44
  "files": [
24
45
  "build",
25
46
  "cjs",
26
47
  "internal"
27
48
  ],
49
+ "keywords": [
50
+ "wix-sdk-module=backend,page,public"
51
+ ],
52
+ "license": "MIT",
53
+ "lint-staged": {
54
+ "*.{js,ts}": "yarn lint"
55
+ },
56
+ "main": "cjs/build/index.js",
57
+ "module": "build/index.mjs",
58
+ "optionalDependencies": {
59
+ "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
60
+ },
28
61
  "publishConfig": {
29
62
  "registry": "https://registry.npmjs.org/",
30
63
  "access": "public"
31
64
  },
32
65
  "scripts": {
33
66
  "build": "tsc -b tsconfig.json tsconfig.cjs.json tsconfig.internal.json tsconfig.internal.cjs.json",
34
- "test": "vitest",
35
67
  "lint": "eslint --max-warnings=0 .",
36
68
  "lint:fix": "eslint --max-warnings=0 . --fix",
69
+ "test": "vitest",
37
70
  "typecheck": "tsc --noEmit"
38
71
  },
39
- "lint-staged": {
40
- "*.{js,ts}": "yarn lint"
41
- },
42
- "dependencies": {
43
- "@wix/error-handler": "^1.67.0",
44
- "@wix/monitoring": "^0.21.0",
45
- "@wix/sdk-runtime": "1.0.0",
46
- "@wix/sdk-types": "1.14.0",
47
- "i18next": "^25.6.1",
48
- "i18next-icu": "^2.4.1",
49
- "intl-messageformat": "^10.7.18"
50
- },
51
- "optionalDependencies": {
52
- "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
53
- },
54
- "devDependencies": {
55
- "@types/is-ci": "^3.0.4",
56
- "@types/node": "^20.19.24",
57
- "@vitest/ui": "^1.6.1",
58
- "@wix/sdk": "1.17.3",
59
- "eslint": "^8.57.1",
60
- "eslint-config-sdk": "1.0.0",
61
- "graphql": "^16.8.0",
62
- "is-ci": "^3.0.1",
63
- "jsdom": "^22.1.0",
64
- "msw": "^2.12.0",
65
- "typescript": "^5.9.3",
66
- "vitest": "^1.6.1",
67
- "vitest-teamcity-reporter": "^0.3.1"
68
- },
69
- "eslintConfig": {
70
- "extends": "sdk"
71
- },
72
+ "sideEffects": false,
73
+ "type": "module",
72
74
  "wix": {
73
75
  "artifact": {
74
76
  "groupId": "com.wixpress",
@@ -83,5 +85,5 @@
83
85
  ]
84
86
  }
85
87
  },
86
- "falconPackageHash": "ceaddd2df4618ad4d9ffefde1b9f902264ed7753d2b7b2c62c5e9924"
88
+ "falconPackageHash": "7b256d0376ce1c1f5e87495b65f5d237327350612d600b2d03120ff5"
87
89
  }