next-translate 2.0.6 → 2.3.0
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/I18nProvider.d.ts +1 -1
- package/README.md +30 -22
- package/appWithI18n.d.ts +1 -1
- package/index.d.ts +1 -0
- package/lib/cjs/withTranslationClientComponent.js +71 -0
- package/lib/esm/withTranslationClientComponent.js +45 -0
- package/package.json +18 -18
- package/withTranslationClientComponent/package.json +7 -0
- package/withTranslationClientComponent.d.ts +3 -0
package/I18nProvider.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export declare const InternalContext: React.Context<{
|
|
|
4
4
|
ns: {};
|
|
5
5
|
config: {};
|
|
6
6
|
}>;
|
|
7
|
-
export default function I18nProvider({ lang: lng, namespaces, children, config: newConfig, }: I18nProviderProps): JSX.Element;
|
|
7
|
+
export default function I18nProvider({ lang: lng, namespaces, children, config: newConfig, }: I18nProviderProps): React.JSX.Element;
|
package/README.md
CHANGED
|
@@ -957,38 +957,46 @@ Next.js 10 introduced [i18n routing](https://nextjs.org/docs/advanced-features/i
|
|
|
957
957
|
|
|
958
958
|
However, since the pages have been moved from the `pages` dir to the **app dir**, this i18n routing **no longer works correctly**.
|
|
959
959
|
|
|
960
|
-
At Next-translate, we have chosen not to re-implement this functionality, as we aim to be a library for translating pages, rather than routing them. We hope that in the future, this feature will be implemented in the `app` directory
|
|
960
|
+
At Next-translate, we have chosen not to re-implement this functionality, as we aim to be a library for translating pages, rather than routing them. We hope that in the future, this feature will be implemented in the `app` directory.
|
|
961
961
|
|
|
962
|
-
|
|
962
|
+
**We recommend the following:**
|
|
963
963
|
|
|
964
|
-
|
|
964
|
+
- Add the dynamic path `[lang]` to the first level. That is, all your pages will be inside `/app/[lang]`.
|
|
965
|
+
- If you need more control over which languages to support, or to detect the browser language, use the [middleware](https://nextjs.org/docs/app/building-your-application/routing/internationalization#routing-overview) that the Next.js team [recommends here](https://nextjs.org/docs/app/building-your-application/routing/internationalization#routing-overview).
|
|
966
|
+
- Update all the pages inside `i18n.(js|ts)` file to contain the `/[lang]` at the beginning.
|
|
965
967
|
|
|
966
|
-
```
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
return NextResponse.rewrite(request.nextUrl)
|
|
968
|
+
```diff
|
|
969
|
+
module.exports = {
|
|
970
|
+
locales: ['en', 'ca', 'es'],
|
|
971
|
+
defaultLocale: 'en',
|
|
972
|
+
pages: {
|
|
973
|
+
'*': ['common'],
|
|
974
|
+
- '/': ['home'],
|
|
975
|
+
+ '/[lang]': ['home'],
|
|
976
|
+
- '/second-page': ['home'],
|
|
977
|
+
+ '/[lang]/second-page': ['home'],
|
|
978
|
+
},
|
|
978
979
|
}
|
|
979
980
|
```
|
|
980
981
|
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
And to navigate:
|
|
982
|
+
At Next-translate level we **already detect the language automatically** according to `searchParams.get('lang')` and `params.lang`. So you **don't need to configure it for each page**, you can use `next-translate` as **normal** within the server/client pages/components:
|
|
984
983
|
|
|
985
984
|
```js
|
|
986
|
-
|
|
987
|
-
|
|
985
|
+
import useTranslation from 'next-translate/useTranslation'
|
|
986
|
+
import Trans from 'next-translate/Trans'
|
|
988
987
|
|
|
989
|
-
|
|
988
|
+
export default function Page() {
|
|
989
|
+
const { t, lang } = useTranslation('common')
|
|
990
|
+
|
|
991
|
+
return (
|
|
992
|
+
<>
|
|
993
|
+
<h1>{t`title`}</h1>
|
|
994
|
+
<Trans i18nKey="common:another-text" components={[<b />]} />
|
|
995
|
+
</>
|
|
996
|
+
)
|
|
997
|
+
}
|
|
998
|
+
```
|
|
990
999
|
|
|
991
|
-
- https://beta.nextjs.org/docs/guides/internationalization.
|
|
992
1000
|
|
|
993
1001
|
## 15. Demos
|
|
994
1002
|
|
package/appWithI18n.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ type NextComponentType<C extends PartialNextContext = PartialNextContext, IP = {
|
|
|
14
14
|
getInitialProps?(context: C): IP | Promise<IP>;
|
|
15
15
|
};
|
|
16
16
|
export default function appWithI18n(AppToTranslate: NextComponentType, config?: LoaderConfig): {
|
|
17
|
-
(props: Props): JSX.Element;
|
|
17
|
+
(props: Props): React.JSX.Element;
|
|
18
18
|
getInitialProps(appCtx: any): Promise<{
|
|
19
19
|
__lang: string;
|
|
20
20
|
__namespaces?: Record<string, object> | undefined;
|
package/index.d.ts
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
25
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
26
|
+
}) : function(o, v) {
|
|
27
|
+
o["default"] = v;
|
|
28
|
+
});
|
|
29
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
30
|
+
if (mod && mod.__esModule) return mod;
|
|
31
|
+
var result = {};
|
|
32
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
33
|
+
__setModuleDefault(result, mod);
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
var react_1 = __importStar(require("react"));
|
|
38
|
+
function withTranslationClientComponent(Component, i18nConfig) {
|
|
39
|
+
function WithTranslation(props) {
|
|
40
|
+
var forceUpdate = (0, react_1.useReducer)(function () { return []; })[1];
|
|
41
|
+
var isClient = typeof window !== 'undefined';
|
|
42
|
+
if (isClient && !window.__NEXT_TRANSLATE__) {
|
|
43
|
+
window.__NEXT_TRANSLATE__ = {
|
|
44
|
+
lang: i18nConfig.defaultLocale,
|
|
45
|
+
namespaces: {},
|
|
46
|
+
};
|
|
47
|
+
update(false);
|
|
48
|
+
}
|
|
49
|
+
if (isClient && !window.i18nConfig) {
|
|
50
|
+
window.i18nConfig = i18nConfig;
|
|
51
|
+
}
|
|
52
|
+
(0, react_1.useEffect)(update);
|
|
53
|
+
function update(rerender) {
|
|
54
|
+
if (rerender === void 0) { rerender = true; }
|
|
55
|
+
var el = document.getElementById('__NEXT_TRANSLATE_DATA__');
|
|
56
|
+
if (!el)
|
|
57
|
+
return;
|
|
58
|
+
var _a = el.dataset, lang = _a.lang, ns = _a.ns, pathname = _a.pathname;
|
|
59
|
+
var shouldRerender = lang !== window.__NEXT_TRANSLATE__.lang ||
|
|
60
|
+
pathname !== window.__NEXT_TRANSLATE__.pathname;
|
|
61
|
+
window.__NEXT_TRANSLATE__ = { lang: lang, namespaces: JSON.parse(ns), pathname: pathname };
|
|
62
|
+
if (shouldRerender && rerender)
|
|
63
|
+
forceUpdate();
|
|
64
|
+
}
|
|
65
|
+
return react_1.default.createElement(Component, __assign({}, props));
|
|
66
|
+
}
|
|
67
|
+
var displayName = Component.displayName || Component.name || 'Component';
|
|
68
|
+
WithTranslation.displayName = "withTranslationClientComponent(".concat(displayName, ")");
|
|
69
|
+
return WithTranslation;
|
|
70
|
+
}
|
|
71
|
+
exports.default = withTranslationClientComponent;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import React, { useEffect, useReducer } from 'react';
|
|
13
|
+
export default function withTranslationClientComponent(Component, i18nConfig) {
|
|
14
|
+
function WithTranslation(props) {
|
|
15
|
+
var forceUpdate = useReducer(function () { return []; })[1];
|
|
16
|
+
var isClient = typeof window !== 'undefined';
|
|
17
|
+
if (isClient && !window.__NEXT_TRANSLATE__) {
|
|
18
|
+
window.__NEXT_TRANSLATE__ = {
|
|
19
|
+
lang: i18nConfig.defaultLocale,
|
|
20
|
+
namespaces: {},
|
|
21
|
+
};
|
|
22
|
+
update(false);
|
|
23
|
+
}
|
|
24
|
+
if (isClient && !window.i18nConfig) {
|
|
25
|
+
window.i18nConfig = i18nConfig;
|
|
26
|
+
}
|
|
27
|
+
useEffect(update);
|
|
28
|
+
function update(rerender) {
|
|
29
|
+
if (rerender === void 0) { rerender = true; }
|
|
30
|
+
var el = document.getElementById('__NEXT_TRANSLATE_DATA__');
|
|
31
|
+
if (!el)
|
|
32
|
+
return;
|
|
33
|
+
var _a = el.dataset, lang = _a.lang, ns = _a.ns, pathname = _a.pathname;
|
|
34
|
+
var shouldRerender = lang !== window.__NEXT_TRANSLATE__.lang ||
|
|
35
|
+
pathname !== window.__NEXT_TRANSLATE__.pathname;
|
|
36
|
+
window.__NEXT_TRANSLATE__ = { lang: lang, namespaces: JSON.parse(ns), pathname: pathname };
|
|
37
|
+
if (shouldRerender && rerender)
|
|
38
|
+
forceUpdate();
|
|
39
|
+
}
|
|
40
|
+
return React.createElement(Component, __assign({}, props));
|
|
41
|
+
}
|
|
42
|
+
var displayName = Component.displayName || Component.name || 'Component';
|
|
43
|
+
WithTranslation.displayName = "withTranslationClientComponent(".concat(displayName, ")");
|
|
44
|
+
return WithTranslation;
|
|
45
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "next-translate",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Tiny and powerful i18n tools (Next plugin + API) to translate your Next.js pages.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -64,35 +64,35 @@
|
|
|
64
64
|
"tsc": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && node build-packages.js"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@babel/cli": "7.
|
|
68
|
-
"@babel/core": "7.
|
|
69
|
-
"@babel/preset-env": "7.
|
|
70
|
-
"@babel/preset-typescript": "7.
|
|
67
|
+
"@babel/cli": "7.22.5",
|
|
68
|
+
"@babel/core": "7.22.5",
|
|
69
|
+
"@babel/preset-env": "7.22.5",
|
|
70
|
+
"@babel/preset-typescript": "7.22.5",
|
|
71
71
|
"@testing-library/react": "13.4.0",
|
|
72
|
-
"@types/jest": "29.2
|
|
73
|
-
"@types/node": "
|
|
74
|
-
"@types/react": "18.
|
|
75
|
-
"@types/react-dom": "18.
|
|
76
|
-
"@types/webpack": "5.28.
|
|
77
|
-
"babel-jest": "29.
|
|
72
|
+
"@types/jest": "29.5.2",
|
|
73
|
+
"@types/node": "20.3.1",
|
|
74
|
+
"@types/react": "18.2.12",
|
|
75
|
+
"@types/react-dom": "18.2.5",
|
|
76
|
+
"@types/webpack": "5.28.1",
|
|
77
|
+
"babel-jest": "29.5.0",
|
|
78
78
|
"babel-plugin-transform-es2015-modules-commonjs": "6.26.2",
|
|
79
79
|
"babel-preset-minify": "0.5.2",
|
|
80
80
|
"cross-env": "7.0.3",
|
|
81
81
|
"express": "4.18.2",
|
|
82
82
|
"husky": "7.0.4",
|
|
83
83
|
"jest": "27.3.1",
|
|
84
|
-
"next": "13.
|
|
85
|
-
"next-router-mock": "0.
|
|
86
|
-
"prettier": "2.8.
|
|
84
|
+
"next": "13.4.6",
|
|
85
|
+
"next-router-mock": "0.9.6",
|
|
86
|
+
"prettier": "2.8.8",
|
|
87
87
|
"pretty-quick": "3.1.3",
|
|
88
88
|
"react": "18.2.0",
|
|
89
89
|
"react-dom": "18.2.0",
|
|
90
90
|
"supertest": "6.3.3",
|
|
91
|
-
"typescript": "
|
|
91
|
+
"typescript": "5.1.3"
|
|
92
92
|
},
|
|
93
93
|
"peerDependencies": {
|
|
94
|
-
"next": ">=
|
|
95
|
-
"react": "
|
|
94
|
+
"next": ">= 13.2.5",
|
|
95
|
+
"react": "18.0.0"
|
|
96
96
|
},
|
|
97
97
|
"prettier": {
|
|
98
98
|
"trailingComma": "es5",
|
|
@@ -101,7 +101,7 @@
|
|
|
101
101
|
"singleQuote": true
|
|
102
102
|
},
|
|
103
103
|
"engines": {
|
|
104
|
-
"node": ">=
|
|
104
|
+
"node": ">=16.10.0"
|
|
105
105
|
},
|
|
106
106
|
"jest": {
|
|
107
107
|
"roots": [
|