@ohos-ports/ni18n 0.0.0-stage → 1.1.0-beta.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/LICENSE +21 -0
- package/README.md +62 -2
- package/dist/cjs/app-with-i18next/app-with-i18next.js +111 -0
- package/dist/cjs/app-with-i18next/index.js +5 -0
- package/dist/cjs/client-namespaces/client-namespaces.js +32 -0
- package/dist/cjs/client-namespaces/index.js +5 -0
- package/dist/cjs/common/get-namespaces.js +43 -0
- package/dist/cjs/common/index.js +7 -0
- package/dist/cjs/common/namespaces-needed.js +2 -0
- package/dist/cjs/common/ni18n-client-state.js +2 -0
- package/dist/cjs/common/ni18n-options.js +2 -0
- package/dist/cjs/common/ni18n-server-state.js +2 -0
- package/dist/cjs/common/unique-array.js +7 -0
- package/dist/cjs/create-i18n-instance/create-i18n-instance.js +37 -0
- package/dist/cjs/create-i18n-instance/get-backend-config.js +29 -0
- package/dist/cjs/create-i18n-instance/index.js +5 -0
- package/dist/cjs/create-i18n-instance/is-browser.js +5 -0
- package/dist/cjs/index.js +11 -0
- package/dist/cjs/load-translations/get-fallback-locales.js +54 -0
- package/dist/cjs/load-translations/index.js +5 -0
- package/dist/cjs/load-translations/load-translations.js +145 -0
- package/dist/cjs/use-backend/browser.js +17 -0
- package/dist/cjs/use-backend/package.json +5 -0
- package/dist/cjs/use-backend/server.js +17 -0
- package/dist/cjs/use-sync-language/index.js +5 -0
- package/dist/cjs/use-sync-language/use-sync-language.js +26 -0
- package/dist/esm/app-with-i18next/app-with-i18next.d.ts +18 -0
- package/dist/esm/app-with-i18next/app-with-i18next.js +84 -0
- package/dist/esm/app-with-i18next/index.d.ts +1 -0
- package/dist/esm/app-with-i18next/index.js +1 -0
- package/dist/esm/client-namespaces/client-namespaces.d.ts +25 -0
- package/dist/esm/client-namespaces/client-namespaces.js +28 -0
- package/dist/esm/client-namespaces/index.d.ts +1 -0
- package/dist/esm/client-namespaces/index.js +1 -0
- package/dist/esm/common/get-namespaces.d.ts +2 -0
- package/dist/esm/common/get-namespaces.js +39 -0
- package/dist/esm/common/index.d.ts +6 -0
- package/dist/esm/common/index.js +2 -0
- package/dist/esm/common/namespaces-needed.d.ts +2 -0
- package/dist/esm/common/namespaces-needed.js +1 -0
- package/dist/esm/common/ni18n-client-state.d.ts +6 -0
- package/dist/esm/common/ni18n-client-state.js +1 -0
- package/dist/esm/common/ni18n-options.d.ts +8 -0
- package/dist/esm/common/ni18n-options.js +1 -0
- package/dist/esm/common/ni18n-server-state.d.ts +9 -0
- package/dist/esm/common/ni18n-server-state.js +1 -0
- package/dist/esm/common/unique-array.d.ts +1 -0
- package/dist/esm/common/unique-array.js +3 -0
- package/dist/esm/create-i18n-instance/create-i18n-instance.d.ts +5 -0
- package/dist/esm/create-i18n-instance/create-i18n-instance.js +30 -0
- package/dist/esm/create-i18n-instance/get-backend-config.d.ts +2 -0
- package/dist/esm/create-i18n-instance/get-backend-config.js +25 -0
- package/dist/esm/create-i18n-instance/index.d.ts +1 -0
- package/dist/esm/create-i18n-instance/index.js +1 -0
- package/dist/esm/create-i18n-instance/is-browser.d.ts +1 -0
- package/dist/esm/create-i18n-instance/is-browser.js +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/load-translations/get-fallback-locales.d.ts +2 -0
- package/dist/esm/load-translations/get-fallback-locales.js +50 -0
- package/dist/esm/load-translations/index.d.ts +1 -0
- package/dist/esm/load-translations/index.js +1 -0
- package/dist/esm/load-translations/load-translations.d.ts +24 -0
- package/dist/esm/load-translations/load-translations.js +141 -0
- package/dist/esm/use-backend/browser.d.ts +3 -0
- package/dist/esm/use-backend/browser.js +12 -0
- package/dist/esm/use-backend/package.json +5 -0
- package/dist/esm/use-backend/server.d.ts +3 -0
- package/dist/esm/use-backend/server.js +12 -0
- package/dist/esm/use-sync-language/index.d.ts +1 -0
- package/dist/esm/use-sync-language/index.js +1 -0
- package/dist/esm/use-sync-language/use-sync-language.d.ts +16 -0
- package/dist/esm/use-sync-language/use-sync-language.js +22 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Jose C Quintas Jr
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Ni18n
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Is an easy to use integration for [Next.js](https://nextjs.org/) to enable [i18next](https://www.i18next.com/) translations on your application with support for SSR, SSG and Client translation loading.
|
|
4
|
+
|
|
5
|
+
<img height="150px" width="300px" src="./.github/assets/logo.svg" />
|
|
6
|
+
|
|
7
|
+
It gives you freedom to use `i18next` with a lot of different customizations, while still being compatible with `next.js`.
|
|
8
|
+
|
|
9
|
+
`Ni18n` also does not directly depend on `i18next` nor `react-i18next`, allowing you to update these dependencies without having to wait for any action on our part.
|
|
10
|
+
|
|
11
|
+
- [Installing](#installing)
|
|
12
|
+
- [Getting Started](./docs/usage/getting-started.md)
|
|
13
|
+
- [Examples](./docs/examples.md)
|
|
14
|
+
- [F.A.Q.](./docs/faq.md)
|
|
15
|
+
|
|
16
|
+
## Installing
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
yarn add i18next react-i18next ni18n
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install i18next react-i18next ni18n
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import {
|
|
28
|
+
appWithI18Next,
|
|
29
|
+
loadTranslations,
|
|
30
|
+
clientNamespaces,
|
|
31
|
+
useSyncLanguage,
|
|
32
|
+
} from 'ni18n'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Getting Started
|
|
36
|
+
|
|
37
|
+
Read the docs at [Getting Started](./docs/usage/getting-started.md) or on the website [https://jcquintas.gitbook.io/ni18n/usage/getting-started](https://jcquintas.gitbook.io/ni18n/usage/getting-started)
|
|
38
|
+
|
|
39
|
+
## Known Issues
|
|
40
|
+
|
|
41
|
+
When running on Vercel we are unable to load the translation files automatically because the [vercel/nft](https://github.com/vercel/nft) package doesn't bundle files requested by a third party lib.
|
|
42
|
+
|
|
43
|
+
A work around is to create a wrapper around `loadTranslations` that calls `path.resolve('./', './public/locales')`, and then use this function instead of the exported by the `ni18n` package.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { loadTranslations as ni18nLoadTranslations } from 'ni18n'
|
|
47
|
+
import { ni18nConfig } from '../ni18n.config'
|
|
48
|
+
|
|
49
|
+
export const loadTranslations = async (
|
|
50
|
+
initialLocale?: string | undefined,
|
|
51
|
+
namespacesNeeded?: NamespacesNeeded | undefined,
|
|
52
|
+
) => {
|
|
53
|
+
const locales = path.resolve('./', './public/locales')
|
|
54
|
+
|
|
55
|
+
return await ni18nLoadTranslations(
|
|
56
|
+
ni18nConfig,
|
|
57
|
+
initialLocale,
|
|
58
|
+
namespacesNeeded,
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
You can read more on [#49](https://github.com/JCQuintas/ni18n/issues/49)
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
37
|
+
var t = {};
|
|
38
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
39
|
+
t[p] = s[p];
|
|
40
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
41
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
42
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
43
|
+
t[p[i]] = s[p[i]];
|
|
44
|
+
}
|
|
45
|
+
return t;
|
|
46
|
+
};
|
|
47
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
48
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
49
|
+
if (!m) return o;
|
|
50
|
+
var i = m.call(o), r, ar = [], e;
|
|
51
|
+
try {
|
|
52
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
53
|
+
}
|
|
54
|
+
catch (error) { e = { error: error }; }
|
|
55
|
+
finally {
|
|
56
|
+
try {
|
|
57
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
58
|
+
}
|
|
59
|
+
finally { if (e) throw e.error; }
|
|
60
|
+
}
|
|
61
|
+
return ar;
|
|
62
|
+
};
|
|
63
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
64
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
65
|
+
if (ar || !(i in from)) {
|
|
66
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
67
|
+
ar[i] = from[i];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
71
|
+
};
|
|
72
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
73
|
+
exports.appWithI18Next = void 0;
|
|
74
|
+
var react_1 = __importStar(require("react"));
|
|
75
|
+
var react_i18next_1 = require("react-i18next");
|
|
76
|
+
var create_i18n_instance_1 = require("../create-i18n-instance");
|
|
77
|
+
var common_1 = require("../common");
|
|
78
|
+
/**
|
|
79
|
+
* Use `appWithI18Next` inside your `_app.jsx` file to initialize the `I18nextProvider`.
|
|
80
|
+
*
|
|
81
|
+
* ```jsx
|
|
82
|
+
* const MyApp = ({ Component, pageProps }) => <Component {...pageProps} />
|
|
83
|
+
*
|
|
84
|
+
* export default appWithI18Next(MyApp, ni18nConfig)
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @param WrappedComponent The MyApp component
|
|
88
|
+
* @param options The options allowed by [i18next options](https://www.i18next.com/overview/configuration-options) plus the `use` property for plugins
|
|
89
|
+
*/
|
|
90
|
+
var appWithI18Next = function (WrappedComponent, options) {
|
|
91
|
+
if (!options) {
|
|
92
|
+
throw new Error('No `options` passed to appWithI18Next');
|
|
93
|
+
}
|
|
94
|
+
var WithI18Next = function (props) {
|
|
95
|
+
var _a = (props.pageProps ||
|
|
96
|
+
{}), __ni18n_server__ = _a.__ni18n_server__, __ni18n_client__ = _a.__ni18n_client__;
|
|
97
|
+
var locale = props.router.locale;
|
|
98
|
+
var i18nInstance = (0, react_1.useMemo)(function () {
|
|
99
|
+
var plugins = options.use, i18nextOptions = __rest(options, ["use"]);
|
|
100
|
+
var ns = __ni18n_server__ || __ni18n_client__
|
|
101
|
+
? (0, common_1.uniqueArray)(__spreadArray(__spreadArray([], __read(((__ni18n_server__ === null || __ni18n_server__ === void 0 ? void 0 : __ni18n_server__.ns) || [])), false), __read(((__ni18n_client__ === null || __ni18n_client__ === void 0 ? void 0 : __ni18n_client__.ns) || [])), false))
|
|
102
|
+
: i18nextOptions.ns;
|
|
103
|
+
var instance = (0, create_i18n_instance_1.createI18nInstance)(__assign(__assign(__assign(__assign({}, i18nextOptions), { lng: locale }), __ni18n_server__), { ns: ns }), plugins).instance;
|
|
104
|
+
return instance;
|
|
105
|
+
}, [options, __ni18n_server__, locale]);
|
|
106
|
+
return (react_1.default.createElement(react_i18next_1.I18nextProvider, { i18n: i18nInstance },
|
|
107
|
+
react_1.default.createElement(WrappedComponent, __assign({}, props))));
|
|
108
|
+
};
|
|
109
|
+
return WithI18Next;
|
|
110
|
+
};
|
|
111
|
+
exports.appWithI18Next = appWithI18Next;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.appWithI18Next = void 0;
|
|
4
|
+
var app_with_i18next_1 = require("./app-with-i18next");
|
|
5
|
+
Object.defineProperty(exports, "appWithI18Next", { enumerable: true, get: function () { return app_with_i18next_1.appWithI18Next; } });
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clientNamespaces = void 0;
|
|
4
|
+
var common_1 = require("../common");
|
|
5
|
+
/**
|
|
6
|
+
* Use `clientNamespaces` inside `getStaticProps` or `getServerSideProps` to
|
|
7
|
+
* flag which namespaces are allowed to be loaded on the client.
|
|
8
|
+
*
|
|
9
|
+
* If you don't use this function, all the namespaces will be loaded as
|
|
10
|
+
* soon as the page loads, which might cause unwanted traffic.
|
|
11
|
+
*
|
|
12
|
+
* This function is safe to be used with `loadTranslations` but any overlapping
|
|
13
|
+
* namespaces will be loaded on the server instead.
|
|
14
|
+
*
|
|
15
|
+
* ```js
|
|
16
|
+
* export const getStaticProps = async (props) => ({
|
|
17
|
+
* props: {
|
|
18
|
+
* ...clientNamespaces(ni18nConfig, 'my-namespace'),
|
|
19
|
+
* },
|
|
20
|
+
* })
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param options The options allowed by [i18next options](https://www.i18next.com/overview/configuration-options) plus the `use` property for plugins
|
|
24
|
+
* @param namespacesNeeded The namespaces that are needed for all the elements in this page
|
|
25
|
+
* @returns an object with a `__ni18n_client__` property to be used internally
|
|
26
|
+
*/
|
|
27
|
+
var clientNamespaces = function (options, namespacesNeeded) { return ({
|
|
28
|
+
__ni18n_client__: {
|
|
29
|
+
ns: (0, common_1.getNamespaces)(options, namespacesNeeded),
|
|
30
|
+
},
|
|
31
|
+
}); };
|
|
32
|
+
exports.clientNamespaces = clientNamespaces;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clientNamespaces = void 0;
|
|
4
|
+
var client_namespaces_1 = require("./client-namespaces");
|
|
5
|
+
Object.defineProperty(exports, "clientNamespaces", { enumerable: true, get: function () { return client_namespaces_1.clientNamespaces; } });
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
+
if (ar || !(i in from)) {
|
|
21
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
+
ar[i] = from[i];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.getNamespaces = void 0;
|
|
29
|
+
var unique_array_1 = require("./unique-array");
|
|
30
|
+
var getNamespaces = function (options, namespacesNeeded) {
|
|
31
|
+
var inputNamespace = options.defaultNS || 'translation';
|
|
32
|
+
var defaultNS = Array.isArray(inputNamespace)
|
|
33
|
+
? inputNamespace
|
|
34
|
+
: [inputNamespace];
|
|
35
|
+
if (Array.isArray(namespacesNeeded) && namespacesNeeded.length === 0)
|
|
36
|
+
return [];
|
|
37
|
+
if (Array.isArray(namespacesNeeded))
|
|
38
|
+
return (0, unique_array_1.uniqueArray)(__spreadArray(__spreadArray([], __read(namespacesNeeded), false), __read(defaultNS), false));
|
|
39
|
+
if (typeof namespacesNeeded === 'string')
|
|
40
|
+
return (0, unique_array_1.uniqueArray)(__spreadArray([namespacesNeeded], __read(defaultNS), false));
|
|
41
|
+
return defaultNS;
|
|
42
|
+
};
|
|
43
|
+
exports.getNamespaces = getNamespaces;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uniqueArray = exports.getNamespaces = void 0;
|
|
4
|
+
var get_namespaces_1 = require("./get-namespaces");
|
|
5
|
+
Object.defineProperty(exports, "getNamespaces", { enumerable: true, get: function () { return get_namespaces_1.getNamespaces; } });
|
|
6
|
+
var unique_array_1 = require("./unique-array");
|
|
7
|
+
Object.defineProperty(exports, "uniqueArray", { enumerable: true, get: function () { return unique_array_1.uniqueArray; } });
|
|
@@ -0,0 +1,37 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.createI18nInstance = void 0;
|
|
18
|
+
var react_i18next_1 = require("react-i18next");
|
|
19
|
+
var i18next_1 = __importDefault(require("i18next"));
|
|
20
|
+
var use_backend_1 = __importDefault(require("../use-backend"));
|
|
21
|
+
var get_backend_config_1 = require("./get-backend-config");
|
|
22
|
+
var createI18nInstance = function (options, plugins) {
|
|
23
|
+
var _a, _b, _c;
|
|
24
|
+
var config = __assign(__assign(__assign({}, options), (0, get_backend_config_1.getBackendConfig)(options)), { partialBundledLanguages: (_a = options.partialBundledLanguages) !== null && _a !== void 0 ? _a : true, react: __assign(__assign({}, options.react), { useSuspense: (_c = (_b = options.react) === null || _b === void 0 ? void 0 : _b.useSuspense) !== null && _c !== void 0 ? _c : false }) });
|
|
25
|
+
var instance = i18next_1.default.createInstance(config);
|
|
26
|
+
instance.use(react_i18next_1.initReactI18next);
|
|
27
|
+
(0, use_backend_1.default)(instance);
|
|
28
|
+
if (Array.isArray(plugins)) {
|
|
29
|
+
plugins.flat().forEach(function (plugin) { return instance.use(plugin); });
|
|
30
|
+
}
|
|
31
|
+
var init = instance.init(config);
|
|
32
|
+
return {
|
|
33
|
+
instance: instance,
|
|
34
|
+
init: init,
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
exports.createI18nInstance = createI18nInstance;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBackendConfig = void 0;
|
|
4
|
+
var is_browser_1 = require("./is-browser");
|
|
5
|
+
var publicPath = './public';
|
|
6
|
+
var localesPath = '/locales';
|
|
7
|
+
var filePath = '/{{lng}}/{{ns}}.json';
|
|
8
|
+
var getBackendConfig = function (options) {
|
|
9
|
+
var hasCustomBackend = options.backend;
|
|
10
|
+
// Server side backend config
|
|
11
|
+
if (!(0, is_browser_1.isBrowser)() && !hasCustomBackend) {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
13
|
+
var path = require('path');
|
|
14
|
+
return {
|
|
15
|
+
backend: {
|
|
16
|
+
loadPath: path.join(process.cwd(), publicPath, localesPath, filePath),
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
if ((0, is_browser_1.isBrowser)() && !hasCustomBackend) {
|
|
21
|
+
return {
|
|
22
|
+
backend: {
|
|
23
|
+
loadPath: "".concat(localesPath).concat(filePath),
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
return {};
|
|
28
|
+
};
|
|
29
|
+
exports.getBackendConfig = getBackendConfig;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createI18nInstance = void 0;
|
|
4
|
+
var create_i18n_instance_1 = require("./create-i18n-instance");
|
|
5
|
+
Object.defineProperty(exports, "createI18nInstance", { enumerable: true, get: function () { return create_i18n_instance_1.createI18nInstance; } });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clientNamespaces = exports.useSyncLanguage = exports.loadTranslations = exports.appWithI18Next = void 0;
|
|
4
|
+
var app_with_i18next_1 = require("./app-with-i18next");
|
|
5
|
+
Object.defineProperty(exports, "appWithI18Next", { enumerable: true, get: function () { return app_with_i18next_1.appWithI18Next; } });
|
|
6
|
+
var load_translations_1 = require("./load-translations");
|
|
7
|
+
Object.defineProperty(exports, "loadTranslations", { enumerable: true, get: function () { return load_translations_1.loadTranslations; } });
|
|
8
|
+
var use_sync_language_1 = require("./use-sync-language");
|
|
9
|
+
Object.defineProperty(exports, "useSyncLanguage", { enumerable: true, get: function () { return use_sync_language_1.useSyncLanguage; } });
|
|
10
|
+
var client_namespaces_1 = require("./client-namespaces");
|
|
11
|
+
Object.defineProperty(exports, "clientNamespaces", { enumerable: true, get: function () { return client_namespaces_1.clientNamespaces; } });
|
|
@@ -0,0 +1,54 @@
|
|
|
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 __read = (this && this.__read) || function (o, n) {
|
|
14
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
+
if (!m) return o;
|
|
16
|
+
var i = m.call(o), r, ar = [], e;
|
|
17
|
+
try {
|
|
18
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
+
}
|
|
20
|
+
catch (error) { e = { error: error }; }
|
|
21
|
+
finally {
|
|
22
|
+
try {
|
|
23
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
+
}
|
|
25
|
+
finally { if (e) throw e.error; }
|
|
26
|
+
}
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
+
if (ar || !(i in from)) {
|
|
32
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
+
ar[i] = from[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.getFallbackLocales = void 0;
|
|
40
|
+
var getFallbackLocales = function (options, initialLocale) {
|
|
41
|
+
var fallbackLng = options.fallbackLng;
|
|
42
|
+
if (typeof fallbackLng === 'string')
|
|
43
|
+
return [fallbackLng];
|
|
44
|
+
if (Array.isArray(fallbackLng))
|
|
45
|
+
return __spreadArray([], __read(fallbackLng), false);
|
|
46
|
+
if (typeof fallbackLng === 'function') {
|
|
47
|
+
var fallbackResult = fallbackLng(initialLocale || '');
|
|
48
|
+
return (0, exports.getFallbackLocales)(__assign(__assign({}, options), { fallbackLng: fallbackResult }));
|
|
49
|
+
}
|
|
50
|
+
if (typeof fallbackLng === 'object' && fallbackLng !== null)
|
|
51
|
+
return Object.values(fallbackLng).flat();
|
|
52
|
+
return [];
|
|
53
|
+
};
|
|
54
|
+
exports.getFallbackLocales = getFallbackLocales;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadTranslations = void 0;
|
|
4
|
+
var load_translations_1 = require("./load-translations");
|
|
5
|
+
Object.defineProperty(exports, "loadTranslations", { enumerable: true, get: function () { return load_translations_1.loadTranslations; } });
|
|
@@ -0,0 +1,145 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
50
|
+
var t = {};
|
|
51
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
+
t[p] = s[p];
|
|
53
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
+
t[p[i]] = s[p[i]];
|
|
57
|
+
}
|
|
58
|
+
return t;
|
|
59
|
+
};
|
|
60
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
61
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
62
|
+
if (!m) return o;
|
|
63
|
+
var i = m.call(o), r, ar = [], e;
|
|
64
|
+
try {
|
|
65
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
66
|
+
}
|
|
67
|
+
catch (error) { e = { error: error }; }
|
|
68
|
+
finally {
|
|
69
|
+
try {
|
|
70
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
71
|
+
}
|
|
72
|
+
finally { if (e) throw e.error; }
|
|
73
|
+
}
|
|
74
|
+
return ar;
|
|
75
|
+
};
|
|
76
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
77
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
78
|
+
if (ar || !(i in from)) {
|
|
79
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
80
|
+
ar[i] = from[i];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
84
|
+
};
|
|
85
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
86
|
+
exports.loadTranslations = void 0;
|
|
87
|
+
var create_i18n_instance_1 = require("../create-i18n-instance");
|
|
88
|
+
var get_fallback_locales_1 = require("./get-fallback-locales");
|
|
89
|
+
var common_1 = require("../common");
|
|
90
|
+
/**
|
|
91
|
+
* Use `loadTranslations` inside `getStaticProps` or `getServerSideProps`
|
|
92
|
+
* to make the translations available for the client through the `pageProps`.
|
|
93
|
+
*
|
|
94
|
+
* Any namespace not loaded by this function will not be available to be
|
|
95
|
+
* loaded on the client side, you can use `clientNamespaces` in conjunction
|
|
96
|
+
* with this one to make them available. Any overlapping namespaces will be
|
|
97
|
+
* loaded on the server instead.
|
|
98
|
+
*
|
|
99
|
+
* ```js
|
|
100
|
+
* export const getStaticProps = async (props) => ({
|
|
101
|
+
* props: {
|
|
102
|
+
* ...(await loadTranslations(ni18nConfig, props.locale, 'my-namespace')),
|
|
103
|
+
* },
|
|
104
|
+
* })
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @param options The options allowed by [i18next options](https://www.i18next.com/overview/configuration-options) plus the `use` property for plugins
|
|
108
|
+
* @param initialLocale The initial locale for this page
|
|
109
|
+
* @param namespacesNeeded The namespaces that are needed for all the elements in this page
|
|
110
|
+
* @returns an object with a `__ni18n_server__` property to be used internally
|
|
111
|
+
*/
|
|
112
|
+
var loadTranslations = function (options, initialLocale, namespacesNeeded) { return __awaiter(void 0, void 0, void 0, function () {
|
|
113
|
+
var plugins, i18nextOptions, _a, instance, init, locales, namespaces, store;
|
|
114
|
+
return __generator(this, function (_b) {
|
|
115
|
+
switch (_b.label) {
|
|
116
|
+
case 0:
|
|
117
|
+
if (!options) {
|
|
118
|
+
throw new Error('No `options` passed to loadTranslations');
|
|
119
|
+
}
|
|
120
|
+
plugins = options.use, i18nextOptions = __rest(options, ["use"]);
|
|
121
|
+
_a = (0, create_i18n_instance_1.createI18nInstance)(__assign(__assign({}, i18nextOptions), { lng: initialLocale }), plugins), instance = _a.instance, init = _a.init;
|
|
122
|
+
return [4 /*yield*/, init];
|
|
123
|
+
case 1:
|
|
124
|
+
_b.sent();
|
|
125
|
+
locales = Array.from(new Set(__spreadArray([
|
|
126
|
+
initialLocale
|
|
127
|
+
], __read((0, get_fallback_locales_1.getFallbackLocales)(i18nextOptions, initialLocale)), false).filter(Boolean)));
|
|
128
|
+
namespaces = (0, common_1.getNamespaces)(i18nextOptions, namespacesNeeded);
|
|
129
|
+
store = Object.fromEntries(locales.map(function (locale) { return [
|
|
130
|
+
locale,
|
|
131
|
+
Object.fromEntries(namespaces.map(function (namespace) {
|
|
132
|
+
var _a, _b;
|
|
133
|
+
return [
|
|
134
|
+
namespace,
|
|
135
|
+
((_b = (_a = instance.services.resourceStore.data) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b[namespace]) || {},
|
|
136
|
+
];
|
|
137
|
+
})),
|
|
138
|
+
]; }));
|
|
139
|
+
return [2 /*return*/, {
|
|
140
|
+
__ni18n_server__: __assign({ resources: store, ns: namespaces }, (initialLocale && { lng: initialLocale })),
|
|
141
|
+
}];
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}); };
|
|
145
|
+
exports.loadTranslations = loadTranslations;
|
|
@@ -0,0 +1,17 @@
|
|
|
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 i18next_http_backend_1 = __importDefault(require("i18next-http-backend"));
|
|
7
|
+
exports.default = (function (i18n, plugins) {
|
|
8
|
+
var hasCustomBackend = plugins === null || plugins === void 0 ? void 0 : plugins.some(function (plugin) {
|
|
9
|
+
return Array.isArray(plugin)
|
|
10
|
+
? plugin.some(function (p) { return p.type === 'backend'; })
|
|
11
|
+
: plugin.type === 'backend';
|
|
12
|
+
});
|
|
13
|
+
if (!hasCustomBackend) {
|
|
14
|
+
i18n.use(i18next_http_backend_1.default);
|
|
15
|
+
}
|
|
16
|
+
return i18n;
|
|
17
|
+
});
|