next-translate 2.5.3 → 2.6.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.
package/README.md CHANGED
@@ -34,6 +34,7 @@
34
34
  - [3. Configuration](#3-configuration)
35
35
  - [4. API](#4-api)
36
36
  - [useTranslation](#usetranslation)
37
+ - [createTranslation](#createtranslation)
37
38
  - [withTranslation](#withtranslation)
38
39
  - [Trans Component](#trans-component)
39
40
  - [DynamicNamespaces](#dynamicnamespaces)
@@ -299,6 +300,15 @@ The `t` function:
299
300
  - **ns**: string - Namespace to use when none is embded in the `i18nKey`.
300
301
  - **Output**: string
301
302
 
303
+ ### createTranslation
304
+
305
+ Similar than `useTranslation` but without being a hook. This helper **only works** in **app dir**.
306
+
307
+ ```ts
308
+ const { t, lang } = createTranslation('ns1') // default namespace (optional)
309
+ const title = t('title')
310
+ ```
311
+
302
312
  ### withTranslation
303
313
 
304
314
  **Size**: ~560b 📦
@@ -1139,6 +1149,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
1139
1149
  <td align="center" valign="top" width="14.28%"><a href="https://thematrixan.github.io/"><img src="https://avatars.githubusercontent.com/u/28862367?v=4?s=100" width="100px;" alt="Mateusz Lesiak"/><br /><sub><b>Mateusz Lesiak</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=TheMatrixan" title="Code">💻</a></td>
1140
1150
  <td align="center" valign="top" width="14.28%"><a href="https://github.com/Curetix"><img src="https://avatars.githubusercontent.com/u/15160542?v=4?s=100" width="100px;" alt="Curetix"/><br /><sub><b>Curetix</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=Curetix" title="Documentation">📖</a></td>
1141
1151
  </tr>
1152
+ <tr>
1153
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/crs1138"><img src="https://avatars.githubusercontent.com/u/1313681?v=4?s=100" width="100px;" alt="Honza"/><br /><sub><b>Honza</b></sub></a><br /><a href="#maintenance-crs1138" title="Maintenance">🚧</a></td>
1154
+ </tr>
1142
1155
  </tbody>
1143
1156
  </table>
1144
1157
 
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "createtranslation",
3
+ "private": true,
4
+ "main": "../lib/cjs/createTranslation.js",
5
+ "module": "../lib/esm/createTranslation.js",
6
+ "types": "../createTranslation.d.ts"
7
+ }
@@ -0,0 +1,4 @@
1
+ export default function createTranslation(defaultNS?: string): {
2
+ t: import(".").Translate;
3
+ lang: string;
4
+ };
@@ -0,0 +1,21 @@
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
+ var transCore_1 = __importDefault(require("./transCore"));
7
+ var wrapTWithDefaultNs_1 = __importDefault(require("./wrapTWithDefaultNs"));
8
+ function createTranslation(defaultNS) {
9
+ var _a;
10
+ var _b = (_a = globalThis.__NEXT_TRANSLATE__) !== null && _a !== void 0 ? _a : {}, lang = _b.lang, namespaces = _b.namespaces, config = _b.config;
11
+ var localesToIgnore = config.localesToIgnore || ['default'];
12
+ var ignoreLang = localesToIgnore.includes(lang);
13
+ var t = (0, transCore_1.default)({
14
+ config: config,
15
+ allNamespaces: namespaces,
16
+ pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
17
+ lang: lang,
18
+ });
19
+ return { t: (0, wrapTWithDefaultNs_1.default)(t, defaultNS), lang: lang };
20
+ }
21
+ exports.default = createTranslation;
@@ -17,27 +17,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  var react_1 = require("react");
18
18
  var wrapTWithDefaultNs_1 = __importDefault(require("./wrapTWithDefaultNs"));
19
19
  var context_1 = __importDefault(require("./context"));
20
- var transCore_1 = __importDefault(require("./transCore"));
20
+ var createTranslation_1 = __importDefault(require("./createTranslation"));
21
21
  function useTranslationInPages(defaultNS) {
22
22
  var ctx = (0, react_1.useContext)(context_1.default);
23
23
  return (0, react_1.useMemo)(function () { return (__assign(__assign({}, ctx), { t: (0, wrapTWithDefaultNs_1.default)(ctx.t, defaultNS) })); }, [ctx, defaultNS]);
24
24
  }
25
- function useTranslationAppDir(defaultNS) {
26
- var _a;
27
- var _b = (_a = globalThis.__NEXT_TRANSLATE__) !== null && _a !== void 0 ? _a : {}, lang = _b.lang, namespaces = _b.namespaces, config = _b.config;
28
- var localesToIgnore = config.localesToIgnore || ['default'];
29
- var ignoreLang = localesToIgnore.includes(lang);
30
- var t = (0, transCore_1.default)({
31
- config: config,
32
- allNamespaces: namespaces,
33
- pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
34
- lang: lang,
35
- });
36
- return { t: (0, wrapTWithDefaultNs_1.default)(t, defaultNS), lang: lang };
37
- }
38
25
  function useTranslation(defaultNS) {
39
26
  var appDir = globalThis.__NEXT_TRANSLATE__;
40
- var useT = (appDir === null || appDir === void 0 ? void 0 : appDir.config) ? useTranslationAppDir : useTranslationInPages;
27
+ var useT = (appDir === null || appDir === void 0 ? void 0 : appDir.config) ? createTranslation_1.default : useTranslationInPages;
41
28
  return useT(defaultNS);
42
29
  }
43
30
  exports.default = useTranslation;
@@ -0,0 +1,15 @@
1
+ import transCore from './transCore';
2
+ import wrapTWithDefaultNs from './wrapTWithDefaultNs';
3
+ export default function createTranslation(defaultNS) {
4
+ var _a;
5
+ var _b = (_a = globalThis.__NEXT_TRANSLATE__) !== null && _a !== void 0 ? _a : {}, lang = _b.lang, namespaces = _b.namespaces, config = _b.config;
6
+ var localesToIgnore = config.localesToIgnore || ['default'];
7
+ var ignoreLang = localesToIgnore.includes(lang);
8
+ var t = transCore({
9
+ config: config,
10
+ allNamespaces: namespaces,
11
+ pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
12
+ lang: lang,
13
+ });
14
+ return { t: wrapTWithDefaultNs(t, defaultNS), lang: lang };
15
+ }
@@ -12,26 +12,13 @@ var __assign = (this && this.__assign) || function () {
12
12
  import { useContext, useMemo } from 'react';
13
13
  import wrapTWithDefaultNs from './wrapTWithDefaultNs';
14
14
  import I18nContext from './context';
15
- import transCore from './transCore';
15
+ import createTranslation from './createTranslation';
16
16
  function useTranslationInPages(defaultNS) {
17
17
  var ctx = useContext(I18nContext);
18
18
  return useMemo(function () { return (__assign(__assign({}, ctx), { t: wrapTWithDefaultNs(ctx.t, defaultNS) })); }, [ctx, defaultNS]);
19
19
  }
20
- function useTranslationAppDir(defaultNS) {
21
- var _a;
22
- var _b = (_a = globalThis.__NEXT_TRANSLATE__) !== null && _a !== void 0 ? _a : {}, lang = _b.lang, namespaces = _b.namespaces, config = _b.config;
23
- var localesToIgnore = config.localesToIgnore || ['default'];
24
- var ignoreLang = localesToIgnore.includes(lang);
25
- var t = transCore({
26
- config: config,
27
- allNamespaces: namespaces,
28
- pluralRules: new Intl.PluralRules(ignoreLang ? undefined : lang),
29
- lang: lang,
30
- });
31
- return { t: wrapTWithDefaultNs(t, defaultNS), lang: lang };
32
- }
33
20
  export default function useTranslation(defaultNS) {
34
21
  var appDir = globalThis.__NEXT_TRANSLATE__;
35
- var useT = (appDir === null || appDir === void 0 ? void 0 : appDir.config) ? useTranslationAppDir : useTranslationInPages;
22
+ var useT = (appDir === null || appDir === void 0 ? void 0 : appDir.config) ? createTranslation : useTranslationInPages;
36
23
  return useT(defaultNS);
37
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-translate",
3
- "version": "2.5.3",
3
+ "version": "2.6.1",
4
4
  "description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -42,13 +42,14 @@
42
42
  "useTranslation*",
43
43
  "setLanguage*",
44
44
  "index*",
45
- "AppDirI18nProvider*"
45
+ "AppDirI18nProvider*",
46
+ "createTranslation*"
46
47
  ],
47
48
  "scripts": {
48
49
  "build": "yarn clean && cross-env NODE_ENV=production && yarn tsc",
49
50
  "clean": "yarn clean:build && yarn clean:examples",
50
- "clean:build": "rm -rf lib appWith* Dynamic* I18n* index context loadNa* setLang* Trans* useT* withT* getP* getC* *.d.ts getT transC* wrapT* types formatElements AppDirI18nProvider*",
51
- "clean:examples": "rm -rf examples/**/.next && rm -rf examples/**/node_modules && rm -rf examples/**/yarn.lock",
51
+ "clean:build": "del lib appWith* Dynamic* I18n* index context loadNa* setLang* Trans* useT* withT* getP* getC* *.d.ts getT transC* wrapT* types formatElements AppDirI18nProvider* createTrans*",
52
+ "clean:examples": "del examples/**/.next examples/**/node_modules examples/**/yarn.lock",
52
53
  "example": "yarn example:complex",
53
54
  "example:basic": "yarn build && yarn --cwd examples/basic && yarn --cwd examples/basic dev",
54
55
  "example:complex": "yarn build && yarn --cwd examples/complex && yarn --cwd examples/complex dev",
@@ -78,6 +79,7 @@
78
79
  "babel-plugin-transform-es2015-modules-commonjs": "6.26.2",
79
80
  "babel-preset-minify": "0.5.2",
80
81
  "cross-env": "7.0.3",
82
+ "del-cli": "^5.0.0",
81
83
  "express": "4.18.2",
82
84
  "husky": "7.0.4",
83
85
  "jest": "27.3.1",