@nimpl/i18n 0.0.0-experimental-f38acae → 0.0.0-experimental-480e42f
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 +295 -23
- package/{ClientTranslation.d.ts → client-translation.d.ts} +1 -1
- package/dist/{ClientTranslation.js → client-translation.js} +4 -4
- package/dist/initialize.js +88 -0
- package/dist/lib/{ClientI18nContext.js → client-context.js} +2 -2
- package/dist/lib/{ClientI18nProvider.js → client-provider.js} +6 -6
- package/dist/lib/{formatServerTranslate.js → format-translate.js} +5 -5
- package/dist/{getTranslation.js → lib/get-translation-core.js} +6 -12
- package/dist/lib/translation-core.js +15 -0
- package/dist/lib/transmitter-core.js +38 -0
- package/dist/{useTranslation.js → use-translation.js} +4 -4
- package/initialize.d.ts +17 -0
- package/package.json +7 -20
- package/types.d.ts +10 -0
- package/I18nTransmitter.d.ts +0 -9
- package/ServerTranslation.d.ts +0 -12
- package/clientConfig.d.ts +0 -0
- package/dist/I18nTransmitter.js +0 -44
- package/dist/ServerTranslation.js +0 -15
- package/dist/clientConfig.js +0 -2
- package/dist/configuration/getConfig.js +0 -69
- package/dist/configuration/types.js +0 -2
- package/dist/helpers/isPromise.js +0 -10
- package/dist/i18n.js +0 -30
- package/dist/lib/loadI18nData.js +0 -23
- package/getTranslation.d.ts +0 -10
- package/i18n.d.ts +0 -1
- /package/dist/lib/{injectQuery.js → inject-query.js} +0 -0
- /package/dist/lib/{parseEntities.js → parse-entities.js} +0 -0
- /package/dist/lib/{Translation.js → translation.js} +0 -0
- /package/{useTranslation.d.ts → use-translation.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,49 +1,321 @@
|
|
|
1
1
|
# @nimpl/i18n
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<PackageLinks npmName="@nimpl/i18n" githubName="nimpl-i18n" />
|
|
4
4
|
|
|
5
|
-
i18n library designed
|
|
5
|
+
i18n library designed for React Server Components with maximum optimization (due to the transfer of logic to the assembly stage and/or server side).
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
<!---robin-->
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Read the documentation in a convenient interface at [nimpl.dev/docs/i18n](https://nimpl.dev/docs/i18n)
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
<!---/robin-->
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm i @nimpl/i18n
|
|
17
|
+
```
|
|
14
18
|
|
|
15
|
-
##
|
|
19
|
+
## Why This Library?
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
Most i18n libraries either load the entire dictionary on the client or disable static optimization when working with Server Components.
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
This library resolves translations on the server. Client components only receive the specific precompiled keys they need through the `Transmitter` wrapper - not the whole dictionary. Pages remain statically generated with translations baked into the HTML.
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
## Quick Start
|
|
22
26
|
|
|
23
|
-
|
|
27
|
+
### 1. Create i18n Configuration
|
|
24
28
|
|
|
25
|
-
|
|
29
|
+
Create a configuration file (e.g., `src/i18n.ts`):
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
```ts
|
|
32
|
+
import { initialize } from "@nimpl/i18n/initialize";
|
|
33
|
+
import fs from "fs/promises";
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
export const { getTranslation, ServerTranslation, Transmitter, revalidate } = initialize({
|
|
36
|
+
load: async (language) => {
|
|
37
|
+
const data = await fs.readFile(`./translations/${language}.json`, "utf-8");
|
|
38
|
+
return JSON.parse(data);
|
|
39
|
+
},
|
|
40
|
+
getLanguage: async () => {
|
|
41
|
+
// Use next/root-params, @nimpl/getters, @nimpl/context or implement your own language detection
|
|
42
|
+
const { params } = await import("@nimpl/getters/get-params").then(m => m.getParams());
|
|
43
|
+
return params.lang as string;
|
|
44
|
+
},
|
|
45
|
+
languages: ["en", "de", "fr"],
|
|
46
|
+
cache: true,
|
|
47
|
+
});
|
|
48
|
+
```
|
|
30
49
|
|
|
31
|
-
|
|
50
|
+
### 2. Set Up Routes
|
|
32
51
|
|
|
33
|
-
```
|
|
34
|
-
|
|
52
|
+
```
|
|
53
|
+
app/
|
|
54
|
+
[lang]/
|
|
55
|
+
layout.tsx
|
|
56
|
+
page.tsx
|
|
35
57
|
```
|
|
36
58
|
|
|
37
|
-
|
|
59
|
+
```tsx
|
|
60
|
+
// app/[lang]/layout.tsx
|
|
61
|
+
export default function RootLayout({
|
|
62
|
+
children,
|
|
63
|
+
params,
|
|
64
|
+
}: {
|
|
65
|
+
children: React.ReactNode;
|
|
66
|
+
params: { lang: string };
|
|
67
|
+
}) {
|
|
68
|
+
return (
|
|
69
|
+
<html lang={params.lang}>
|
|
70
|
+
<body>{children}</body>
|
|
71
|
+
</html>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
```
|
|
38
75
|
|
|
39
|
-
|
|
40
|
-
|
|
76
|
+
### 3. Use Translations
|
|
77
|
+
|
|
78
|
+
```tsx
|
|
79
|
+
// app/[lang]/page.tsx
|
|
80
|
+
import { getTranslation } from "@/i18n";
|
|
81
|
+
|
|
82
|
+
export default async function Page() {
|
|
83
|
+
const { t } = await getTranslation();
|
|
84
|
+
|
|
85
|
+
return <h1>{t("home.title")}</h1>;
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## API
|
|
90
|
+
|
|
91
|
+
### initialize
|
|
92
|
+
|
|
93
|
+
Creates configured i18n functions. Call once and export the returned functions.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { initialize } from "@nimpl/i18n/initialize";
|
|
97
|
+
|
|
98
|
+
export const { getTranslation, ServerTranslation, Transmitter, revalidate } = initialize(config);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Config Options
|
|
102
|
+
|
|
103
|
+
| Option | Type | Required | Description |
|
|
104
|
+
|--------|------|----------|-------------|
|
|
105
|
+
| `load` | `(language: string) => Promise<object>` | Yes | Async function that returns translations for a language |
|
|
106
|
+
| `getLanguage` | `() => Promise<string>` | Yes | Function to determine current language |
|
|
107
|
+
| `languages` | `string[]` | Yes | Array of supported language codes |
|
|
108
|
+
| `cache` | `boolean` | No | Enable translation caching (recommended for production) |
|
|
109
|
+
|
|
110
|
+
### getTranslation
|
|
111
|
+
|
|
112
|
+
Server-side function for translations in React Server Components.
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { getTranslation } from "@/i18n";
|
|
116
|
+
|
|
117
|
+
export default async function Page() {
|
|
118
|
+
const { t, language } = await getTranslation();
|
|
119
|
+
|
|
120
|
+
return <h1>{t("home.title")}</h1>;
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
With explicit language (useful for `generateMetadata`):
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
export async function generateMetadata({ params }: { params: { lang: string } }) {
|
|
128
|
+
const { t } = await getTranslation({ language: params.lang });
|
|
129
|
+
|
|
130
|
+
return { title: t("home.meta.title") };
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
With namespace:
|
|
135
|
+
|
|
136
|
+
```tsx
|
|
137
|
+
const { t } = await getTranslation({ namespace: "home" });
|
|
138
|
+
t("title"); // Equivalent to t("home.title")
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Transmitter
|
|
142
|
+
|
|
143
|
+
Server component that passes translations to client components. Wrap client components that need translations.
|
|
144
|
+
|
|
145
|
+
```tsx
|
|
146
|
+
import { Transmitter } from "@/i18n";
|
|
147
|
+
import Counter from "./Counter";
|
|
148
|
+
|
|
149
|
+
export default async function Page() {
|
|
150
|
+
return (
|
|
151
|
+
<Transmitter terms={["counter", "shared.buttons"]}>
|
|
152
|
+
<Counter />
|
|
153
|
+
</Transmitter>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
With explicit language:
|
|
159
|
+
|
|
160
|
+
```tsx
|
|
161
|
+
<Transmitter terms={["counter"]} language="fr">
|
|
162
|
+
<Counter />
|
|
163
|
+
</Transmitter>
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### ServerTranslation
|
|
167
|
+
|
|
168
|
+
Server component for complex translations with embedded JSX.
|
|
169
|
+
|
|
170
|
+
```tsx
|
|
171
|
+
import { ServerTranslation } from "@/i18n";
|
|
172
|
+
|
|
173
|
+
// Translation: "Read our <link>documentation</link> for more info"
|
|
174
|
+
export default async function Page() {
|
|
175
|
+
return (
|
|
176
|
+
<ServerTranslation
|
|
177
|
+
term="home.description"
|
|
178
|
+
components={{ link: <a href="/docs" /> }}
|
|
179
|
+
/>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
With variables:
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
// Translation: "We have <b>{{count}}</b> products"
|
|
188
|
+
<ServerTranslation
|
|
189
|
+
term="products.count"
|
|
190
|
+
components={{ b: <strong /> }}
|
|
191
|
+
query={{ count: 42 }}
|
|
192
|
+
/>
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### useTranslation
|
|
196
|
+
|
|
197
|
+
Client-side hook. Requires `Transmitter` in a parent Server Component.
|
|
198
|
+
|
|
199
|
+
```tsx
|
|
200
|
+
"use client";
|
|
201
|
+
|
|
202
|
+
import { useTranslation } from "@nimpl/i18n/use-translation";
|
|
203
|
+
|
|
204
|
+
export default function Counter() {
|
|
205
|
+
const { t } = useTranslation();
|
|
206
|
+
|
|
207
|
+
return <button>{t("counter.increment")}</button>;
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
With namespace:
|
|
212
|
+
|
|
213
|
+
```tsx
|
|
214
|
+
const { t } = useTranslation({ namespace: "counter" });
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### ClientTranslation
|
|
218
|
+
|
|
219
|
+
Client component for complex translations with embedded JSX.
|
|
220
|
+
|
|
221
|
+
```tsx
|
|
222
|
+
"use client";
|
|
223
|
+
|
|
224
|
+
import { ClientTranslation } from "@nimpl/i18n/client-translation";
|
|
225
|
+
|
|
226
|
+
// Translation: "Read our <link>documentation</link>"
|
|
227
|
+
export default function Info() {
|
|
228
|
+
return (
|
|
229
|
+
<ClientTranslation
|
|
230
|
+
term="info.description"
|
|
231
|
+
components={{ link: <a href="/docs" /> }}
|
|
232
|
+
/>
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### revalidate
|
|
238
|
+
|
|
239
|
+
Refresh cached translations for a language.
|
|
240
|
+
|
|
241
|
+
```tsx
|
|
242
|
+
import { revalidate } from "@/i18n";
|
|
243
|
+
|
|
244
|
+
// Foreground revalidation (blocks until complete)
|
|
245
|
+
await revalidate("en");
|
|
246
|
+
|
|
247
|
+
// Background revalidation (non-blocking)
|
|
248
|
+
await revalidate("en", true);
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Variable Interpolation
|
|
252
|
+
|
|
253
|
+
Use `{{variable}}` syntax in translations:
|
|
254
|
+
|
|
255
|
+
```json
|
|
256
|
+
{
|
|
257
|
+
"greeting": "Hello, {{name}}!",
|
|
258
|
+
"items": "You have {{count}} items"
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
```tsx
|
|
263
|
+
t("greeting", { query: { name: "Alex" } });
|
|
264
|
+
// → "Hello, Alex!"
|
|
265
|
+
|
|
266
|
+
t("items", { query: { count: 5 } });
|
|
267
|
+
// → "You have 5 items"
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Use `removeUnusedQueries` to strip undefined variables:
|
|
271
|
+
|
|
272
|
+
```tsx
|
|
273
|
+
// Translation: "Hello, {{name}}! Role: {{role}}"
|
|
274
|
+
t("welcome", { query: { name: "Alex" }, removeUnusedQueries: true });
|
|
275
|
+
// → "Hello, Alex! Role: "
|
|
41
276
|
```
|
|
42
277
|
|
|
43
|
-
|
|
278
|
+
### Server-Side Query Injection
|
|
279
|
+
|
|
280
|
+
Pass dynamic values to client translations from the server to improve client performance:
|
|
281
|
+
|
|
282
|
+
```tsx
|
|
283
|
+
<Transmitter
|
|
284
|
+
terms={[
|
|
285
|
+
"pricing",
|
|
286
|
+
["welcome", { query: { stage: process.env.NODE_ENV } }],
|
|
287
|
+
]}
|
|
288
|
+
>
|
|
289
|
+
<ClientComponent />
|
|
290
|
+
</Transmitter>
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Namespaces
|
|
294
|
+
|
|
295
|
+
Access nested keys with dot notation or colon prefix:
|
|
296
|
+
|
|
297
|
+
```tsx
|
|
298
|
+
// Dot notation
|
|
299
|
+
t("header.nav.home");
|
|
300
|
+
|
|
301
|
+
// Namespace in options
|
|
302
|
+
const { t } = await getTranslation({ namespace: "header" });
|
|
303
|
+
t("nav.home");
|
|
304
|
+
|
|
305
|
+
// Colon prefix (overrides default namespace)
|
|
306
|
+
t("footer:copyright");
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Notes
|
|
310
|
+
|
|
311
|
+
- Client components (`useTranslation`, `ClientTranslation`) inherit language from server parents
|
|
312
|
+
- The `terms` array accepts namespace prefixes (e.g., `"nav"`) or specific keys (e.g., `"nav.home"`)
|
|
313
|
+
- Use `next/root-params`, `@nimpl/getters` or `@nimpl/context` for automatic route parameter detection
|
|
314
|
+
|
|
315
|
+
## Examples
|
|
44
316
|
|
|
45
|
-
|
|
317
|
+
- [Base example](https://github.com/alexdln/nimpl-i18n/tree/main/examples/base)
|
|
46
318
|
|
|
47
319
|
## License
|
|
48
320
|
|
|
49
|
-
|
|
321
|
+
MIT
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { type I18nOptions } from "./types";
|
|
3
|
-
import { type TranslationProps } from "./lib/
|
|
3
|
+
import { type TranslationProps } from "./lib/translation";
|
|
4
4
|
type ClientTranslationProps = {
|
|
5
5
|
term: string;
|
|
6
6
|
components?: TranslationProps["components"];
|
|
@@ -6,11 +6,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.ClientTranslation = void 0;
|
|
8
8
|
const react_1 = __importDefault(require("react"));
|
|
9
|
-
const
|
|
10
|
-
const
|
|
9
|
+
const translation_1 = require("./lib/translation");
|
|
10
|
+
const use_translation_1 = require("./use-translation");
|
|
11
11
|
const ClientTranslation = ({ term, components, query, removeUnusedQueries, }) => {
|
|
12
|
-
const { t } = (0,
|
|
12
|
+
const { t } = (0, use_translation_1.useTranslation)();
|
|
13
13
|
const text = t(term, { query, removeUnusedQueries });
|
|
14
|
-
return react_1.default.createElement(
|
|
14
|
+
return react_1.default.createElement(translation_1.Translation, { term: term, text: text, components: components });
|
|
15
15
|
};
|
|
16
16
|
exports.ClientTranslation = ClientTranslation;
|
|
@@ -0,0 +1,88 @@
|
|
|
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.initialize = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const get_translation_core_1 = require("./lib/get-translation-core");
|
|
9
|
+
const translation_core_1 = require("./lib/translation-core");
|
|
10
|
+
const transmitter_core_1 = require("./lib/transmitter-core");
|
|
11
|
+
const initialize = (config) => {
|
|
12
|
+
const cache = new Map();
|
|
13
|
+
const loadTranslates = async (language, revalidate) => {
|
|
14
|
+
const item = cache.get(language);
|
|
15
|
+
if (!revalidate && item)
|
|
16
|
+
return item;
|
|
17
|
+
if (item && "then" in item) {
|
|
18
|
+
return item;
|
|
19
|
+
}
|
|
20
|
+
const newData = await config.load(language);
|
|
21
|
+
cache.set(language, newData);
|
|
22
|
+
return newData;
|
|
23
|
+
};
|
|
24
|
+
const getTranslation = async (options) => {
|
|
25
|
+
const { language, ...rest } = options || {};
|
|
26
|
+
const targetLanguage = language || (await config.getLanguage());
|
|
27
|
+
let dictionary;
|
|
28
|
+
if (config.cache) {
|
|
29
|
+
dictionary = await loadTranslates(targetLanguage);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
dictionary = await revalidate(targetLanguage);
|
|
33
|
+
}
|
|
34
|
+
if (!language) {
|
|
35
|
+
throw new Error("Unable to get the language in getTranslation. Please check the getLanguage method in the configuration file or pass the language as an argument.");
|
|
36
|
+
}
|
|
37
|
+
return (0, get_translation_core_1.getTranslationCore)({ ...rest, language: targetLanguage, dictionary });
|
|
38
|
+
};
|
|
39
|
+
const ServerTranslation = async (options) => {
|
|
40
|
+
const { language, ...rest } = options || {};
|
|
41
|
+
const targetLanguage = language || (await config.getLanguage());
|
|
42
|
+
let dictionary;
|
|
43
|
+
if (config.cache) {
|
|
44
|
+
dictionary = await loadTranslates(targetLanguage);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
dictionary = await revalidate(targetLanguage);
|
|
48
|
+
}
|
|
49
|
+
if (!language) {
|
|
50
|
+
throw new Error("Unable to get the language in ServerTranslation. Please check the getLanguage method in the configuration file or pass the language as an argument.");
|
|
51
|
+
}
|
|
52
|
+
return react_1.default.createElement(translation_core_1.TranslationCore, { ...rest, language: targetLanguage, dictionary: dictionary });
|
|
53
|
+
};
|
|
54
|
+
const Transmitter = async (options) => {
|
|
55
|
+
const { language, ...rest } = options || {};
|
|
56
|
+
const targetLanguage = language || (await config.getLanguage());
|
|
57
|
+
let dictionary;
|
|
58
|
+
if (config.cache) {
|
|
59
|
+
dictionary = await loadTranslates(targetLanguage);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
dictionary = await revalidate(targetLanguage);
|
|
63
|
+
}
|
|
64
|
+
if (!language) {
|
|
65
|
+
throw new Error("Unable to get the language in Transmitter. Please check the getLanguage method in the configuration file or pass the language as an argument.");
|
|
66
|
+
}
|
|
67
|
+
return react_1.default.createElement(transmitter_core_1.TransmitterCore, { ...rest, language: targetLanguage, dictionary: dictionary });
|
|
68
|
+
};
|
|
69
|
+
const revalidate = async (language, background = false) => {
|
|
70
|
+
const item = cache.get(language);
|
|
71
|
+
if (item && "then" in item)
|
|
72
|
+
return item;
|
|
73
|
+
if (background) {
|
|
74
|
+
const newData = await loadTranslates(language, true);
|
|
75
|
+
return newData;
|
|
76
|
+
}
|
|
77
|
+
const newDataPromise = loadTranslates(language, true);
|
|
78
|
+
cache.set(language, newDataPromise);
|
|
79
|
+
return newDataPromise;
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
Transmitter,
|
|
83
|
+
ServerTranslation,
|
|
84
|
+
getTranslation,
|
|
85
|
+
revalidate,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
exports.initialize = initialize;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
"use client";
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
4
|
+
exports.ClientContext = void 0;
|
|
5
5
|
const react_1 = require("react");
|
|
6
|
-
exports.
|
|
6
|
+
exports.ClientContext = (0, react_1.createContext)(null);
|
|
@@ -34,14 +34,14 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
};
|
|
35
35
|
})();
|
|
36
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
-
exports.
|
|
37
|
+
exports.ClientProvider = void 0;
|
|
38
38
|
const react_1 = __importStar(require("react"));
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const prevTranslates = (0, react_1.useContext)(
|
|
39
|
+
const client_context_1 = require("./client-context");
|
|
40
|
+
const ClientProvider = ({ translates, children, language, cleanThread }) => {
|
|
41
|
+
const prevTranslates = (0, react_1.useContext)(client_context_1.ClientContext);
|
|
42
42
|
if (cleanThread) {
|
|
43
43
|
Object.assign(translates, prevTranslates?.translates);
|
|
44
44
|
}
|
|
45
|
-
return react_1.default.createElement(
|
|
45
|
+
return react_1.default.createElement(client_context_1.ClientContext.Provider, { value: { language, translates } }, children);
|
|
46
46
|
};
|
|
47
|
-
exports.
|
|
47
|
+
exports.ClientProvider = ClientProvider;
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.formatTranslate = void 0;
|
|
4
4
|
const html_entities_1 = require("html-entities");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const inject_query_1 = require("./inject-query");
|
|
6
|
+
const formatTranslate = ({ term, text, removeUnusedQueries, query, parseEntities }) => {
|
|
7
7
|
let newTranslate = text;
|
|
8
8
|
if (query) {
|
|
9
|
-
newTranslate = (0,
|
|
9
|
+
newTranslate = (0, inject_query_1.injectQuery)({ term, text: newTranslate, query, removeUnusedQueries });
|
|
10
10
|
}
|
|
11
11
|
if (parseEntities === undefined || parseEntities === true) {
|
|
12
12
|
newTranslate = (0, html_entities_1.decode)(newTranslate, { scope: "strict" });
|
|
13
13
|
}
|
|
14
14
|
return newTranslate;
|
|
15
15
|
};
|
|
16
|
-
exports.
|
|
16
|
+
exports.formatTranslate = formatTranslate;
|
|
@@ -3,17 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.getTranslationCore = void 0;
|
|
7
7
|
const object_path_1 = __importDefault(require("object-path"));
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const { language: argLanguage, namespace } = options || {};
|
|
12
|
-
const { dictionary, language: configLanguage } = await (0, loadI18nData_1.loadI18nData)();
|
|
13
|
-
const language = argLanguage || configLanguage;
|
|
14
|
-
if (!language) {
|
|
15
|
-
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.");
|
|
16
|
-
}
|
|
8
|
+
const format_translate_1 = require("./format-translate");
|
|
9
|
+
const getTranslationCore = (options) => {
|
|
10
|
+
const { language, namespace, dictionary } = options || {};
|
|
17
11
|
const namespaceDictionary = namespace ? object_path_1.default.get(dictionary, namespace) : dictionary;
|
|
18
12
|
const t = (term, opts) => {
|
|
19
13
|
let termDictionary = namespaceDictionary;
|
|
@@ -27,8 +21,8 @@ const getTranslation = async (options) => {
|
|
|
27
21
|
const fullTerm = `${termNamespace ? `${termNamespace}.` : ""}${termKey}`;
|
|
28
22
|
if (typeof translation !== "string" || !translation)
|
|
29
23
|
return fullTerm;
|
|
30
|
-
return (0,
|
|
24
|
+
return (0, format_translate_1.formatTranslate)({ term: fullTerm, text: translation, parseEntities: true, ...opts });
|
|
31
25
|
};
|
|
32
26
|
return { t, language };
|
|
33
27
|
};
|
|
34
|
-
exports.
|
|
28
|
+
exports.getTranslationCore = getTranslationCore;
|
|
@@ -0,0 +1,15 @@
|
|
|
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.TranslationCore = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const translation_1 = require("./translation");
|
|
9
|
+
const get_translation_core_1 = require("./get-translation-core");
|
|
10
|
+
const TranslationCore = ({ dictionary, namespace, term, components, query, removeUnusedQueries, language, }) => {
|
|
11
|
+
const { t } = (0, get_translation_core_1.getTranslationCore)({ language, namespace, dictionary });
|
|
12
|
+
const text = t(term, { query, removeUnusedQueries });
|
|
13
|
+
return react_1.default.createElement(translation_1.Translation, { term: term, text: text, components: components });
|
|
14
|
+
};
|
|
15
|
+
exports.TranslationCore = TranslationCore;
|
|
@@ -0,0 +1,38 @@
|
|
|
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.TransmitterCore = void 0;
|
|
7
|
+
const react_1 = __importDefault(require("react"));
|
|
8
|
+
const object_path_1 = __importDefault(require("object-path"));
|
|
9
|
+
const client_provider_1 = require("./client-provider");
|
|
10
|
+
const format_translate_1 = require("./format-translate");
|
|
11
|
+
const formatTranslates = (result, targetKey, translates, opts = {}) => {
|
|
12
|
+
if (!translates)
|
|
13
|
+
return;
|
|
14
|
+
if (typeof translates === "string") {
|
|
15
|
+
result[targetKey] = (0, format_translate_1.formatTranslate)({ term: targetKey, text: translates, parseEntities: true, ...opts });
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
Object.entries(translates).forEach(([subKey, translate]) => {
|
|
19
|
+
formatTranslates(result, `${targetKey}.${subKey}`, translate, opts);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const TransmitterCore = async ({ dictionary, language, terms, children, cleanThread, }) => {
|
|
24
|
+
const result = {};
|
|
25
|
+
terms.forEach((term) => {
|
|
26
|
+
if (Array.isArray(term)) {
|
|
27
|
+
const [termKey, opts] = term;
|
|
28
|
+
const translates = object_path_1.default.get(dictionary, termKey);
|
|
29
|
+
formatTranslates(result, termKey, translates, opts);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const translates = object_path_1.default.get(dictionary, term);
|
|
33
|
+
formatTranslates(result, term, translates);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return (react_1.default.createElement(client_provider_1.ClientProvider, { language: language, translates: result, cleanThread: cleanThread }, children));
|
|
37
|
+
};
|
|
38
|
+
exports.TransmitterCore = TransmitterCore;
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useTranslation = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const client_context_1 = require("./lib/client-context");
|
|
6
|
+
const inject_query_1 = require("./lib/inject-query");
|
|
7
7
|
const useTranslation = ({ namespace } = {}) => {
|
|
8
|
-
const context = (0, react_1.useContext)(
|
|
8
|
+
const context = (0, react_1.useContext)(client_context_1.ClientContext);
|
|
9
9
|
if (!context) {
|
|
10
10
|
throw new Error("Please, Init I18nTransmitter for client components - https://nimpl.dev/docs/i18n/usage#client-components");
|
|
11
11
|
}
|
|
@@ -22,7 +22,7 @@ const useTranslation = ({ namespace } = {}) => {
|
|
|
22
22
|
if (!translation)
|
|
23
23
|
return termKey;
|
|
24
24
|
if (opts?.query) {
|
|
25
|
-
return (0,
|
|
25
|
+
return (0, inject_query_1.injectQuery)({
|
|
26
26
|
term,
|
|
27
27
|
text: translation,
|
|
28
28
|
query: opts.query,
|
package/initialize.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { type Config, type Translates } from "./types";
|
|
3
|
+
import { type GetTranslationCoreOptions } from "./lib/get-translation-core";
|
|
4
|
+
import { type TranslationCoreProps } from "./lib/translation-core";
|
|
5
|
+
import { type TransmitterCoreProps } from "./lib/transmitter-core";
|
|
6
|
+
export declare const initialize: (config: Config) => {
|
|
7
|
+
Transmitter: (options: Omit<TransmitterCoreProps, "dictionary" | "language"> & {
|
|
8
|
+
language?: string;
|
|
9
|
+
}) => Promise<React.JSX.Element>;
|
|
10
|
+
ServerTranslation: (options: Omit<TranslationCoreProps, "dictionary" | "language"> & {
|
|
11
|
+
language?: string;
|
|
12
|
+
}) => Promise<React.JSX.Element>;
|
|
13
|
+
getTranslation: (options: Omit<GetTranslationCoreOptions, "dictionary" | "language"> & {
|
|
14
|
+
language?: string;
|
|
15
|
+
}) => Promise<import("./lib/get-translation-core").GetTranslationCoreReturnType>;
|
|
16
|
+
revalidate: (language: string, background?: boolean) => Promise<Translates>;
|
|
17
|
+
};
|
package/package.json
CHANGED
|
@@ -1,31 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nimpl/i18n",
|
|
3
|
-
"version": "0.0.0-experimental-
|
|
3
|
+
"version": "0.0.0-experimental-480e42f",
|
|
4
4
|
"description": "i18n library for working with translations in server and client components",
|
|
5
5
|
"exports": {
|
|
6
|
-
"./
|
|
7
|
-
"default": "./dist/
|
|
6
|
+
"./client-translation": {
|
|
7
|
+
"default": "./dist/client-translation.js"
|
|
8
8
|
},
|
|
9
|
-
"./
|
|
10
|
-
"default": "./dist/
|
|
11
|
-
},
|
|
12
|
-
"./i18n": {
|
|
13
|
-
"default": "./dist/i18n.js"
|
|
14
|
-
},
|
|
15
|
-
"./I18nTransmitter": {
|
|
16
|
-
"default": "./dist/I18nTransmitter.js"
|
|
17
|
-
},
|
|
18
|
-
"./ServerTranslation": {
|
|
19
|
-
"default": "./dist/ServerTranslation.js"
|
|
9
|
+
"./initialize": {
|
|
10
|
+
"default": "./dist/initialize.js"
|
|
20
11
|
},
|
|
21
12
|
"./types": {
|
|
22
13
|
"default": "./dist/types.js"
|
|
23
14
|
},
|
|
24
|
-
"./
|
|
25
|
-
"default": "./dist/
|
|
26
|
-
},
|
|
27
|
-
"./clientConfig": {
|
|
28
|
-
"default": "./dist/clientConfig.js"
|
|
15
|
+
"./use-translation": {
|
|
16
|
+
"default": "./dist/use-translation.js"
|
|
29
17
|
}
|
|
30
18
|
},
|
|
31
19
|
"files": [
|
|
@@ -68,7 +56,6 @@
|
|
|
68
56
|
"react": ">= 19.1.0"
|
|
69
57
|
},
|
|
70
58
|
"dependencies": {
|
|
71
|
-
"@nimpl/getters": "2.2.2",
|
|
72
59
|
"html-entities": "2.6.0",
|
|
73
60
|
"object-path": "0.11.8"
|
|
74
61
|
}
|
package/types.d.ts
CHANGED
|
@@ -20,3 +20,13 @@ export type I18nContextType = {
|
|
|
20
20
|
[key: string]: string;
|
|
21
21
|
};
|
|
22
22
|
} | null;
|
|
23
|
+
export type Meta = {
|
|
24
|
+
lastUpdated: number;
|
|
25
|
+
isRevalidated?: boolean;
|
|
26
|
+
} & Record<string, unknown>;
|
|
27
|
+
export type Config = {
|
|
28
|
+
load(key: string, meta?: Meta): Promise<Translates>;
|
|
29
|
+
getLanguage(): Promise<string>;
|
|
30
|
+
languages: string[];
|
|
31
|
+
cache?: boolean;
|
|
32
|
+
};
|
package/I18nTransmitter.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { type I18nOptions } from "./types";
|
|
3
|
-
export type I18nTransmitterProps = {
|
|
4
|
-
terms: (string | [string, I18nOptions])[];
|
|
5
|
-
children: React.ReactNode;
|
|
6
|
-
cleanThread?: boolean;
|
|
7
|
-
language?: string;
|
|
8
|
-
};
|
|
9
|
-
export declare const I18nTransmitter: React.FC<I18nTransmitterProps>;
|
package/ServerTranslation.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { type I18nOptions } from "./types";
|
|
3
|
-
import { type TranslationProps } from "./lib/Translation";
|
|
4
|
-
type ServerTranslationProps = {
|
|
5
|
-
term: string;
|
|
6
|
-
components?: TranslationProps["components"];
|
|
7
|
-
query?: I18nOptions["query"];
|
|
8
|
-
removeUnusedQueries?: I18nOptions["removeUnusedQueries"];
|
|
9
|
-
language?: string;
|
|
10
|
-
};
|
|
11
|
-
export declare const ServerTranslation: React.FC<ServerTranslationProps>;
|
|
12
|
-
export {};
|
package/clientConfig.d.ts
DELETED
|
File without changes
|
package/dist/I18nTransmitter.js
DELETED
|
@@ -1,44 +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.I18nTransmitter = void 0;
|
|
7
|
-
const react_1 = __importDefault(require("react"));
|
|
8
|
-
const object_path_1 = __importDefault(require("object-path"));
|
|
9
|
-
const ClientI18nProvider_1 = require("./lib/ClientI18nProvider");
|
|
10
|
-
const formatServerTranslate_1 = require("./lib/formatServerTranslate");
|
|
11
|
-
const loadI18nData_1 = require("./lib/loadI18nData");
|
|
12
|
-
const formatServerTranslates = (result, targetKey, translates, opts = {}) => {
|
|
13
|
-
if (!translates)
|
|
14
|
-
return;
|
|
15
|
-
if (typeof translates === "string") {
|
|
16
|
-
result[targetKey] = (0, formatServerTranslate_1.formatServerTranslate)({ term: targetKey, text: translates, parseEntities: true, ...opts });
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
Object.entries(translates).forEach(([subKey, translate]) => {
|
|
20
|
-
formatServerTranslates(result, `${targetKey}.${subKey}`, translate, opts);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
const I18nTransmitter = async ({ language: argLanguage, terms, children, cleanThread, }) => {
|
|
25
|
-
const { dictionary, language: configLanguage } = await (0, loadI18nData_1.loadI18nData)();
|
|
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.");
|
|
29
|
-
}
|
|
30
|
-
const result = {};
|
|
31
|
-
terms.forEach((term) => {
|
|
32
|
-
if (Array.isArray(term)) {
|
|
33
|
-
const [termKey, opts] = term;
|
|
34
|
-
const translates = object_path_1.default.get(dictionary, termKey);
|
|
35
|
-
formatServerTranslates(result, termKey, translates, opts);
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
const translates = object_path_1.default.get(dictionary, term);
|
|
39
|
-
formatServerTranslates(result, term, translates);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
return (react_1.default.createElement(ClientI18nProvider_1.ClientI18nProvider, { language: language, translates: result, cleanThread: cleanThread }, children));
|
|
43
|
-
};
|
|
44
|
-
exports.I18nTransmitter = I18nTransmitter;
|
|
@@ -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
|
-
exports.ServerTranslation = void 0;
|
|
7
|
-
const react_1 = __importDefault(require("react"));
|
|
8
|
-
const Translation_1 = require("./lib/Translation");
|
|
9
|
-
const getTranslation_1 = require("./getTranslation");
|
|
10
|
-
const ServerTranslation = async ({ term, components, query, removeUnusedQueries, language, }) => {
|
|
11
|
-
const { t } = await (0, getTranslation_1.getTranslation)({ language });
|
|
12
|
-
const text = t(term, { query, removeUnusedQueries });
|
|
13
|
-
return react_1.default.createElement(Translation_1.Translation, { term: term, text: text, components: components });
|
|
14
|
-
};
|
|
15
|
-
exports.ServerTranslation = ServerTranslation;
|
package/dist/clientConfig.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.getConfig = void 0;
|
|
40
|
-
const fs_1 = __importDefault(require("fs"));
|
|
41
|
-
const path_1 = __importDefault(require("path"));
|
|
42
|
-
const url_1 = require("url");
|
|
43
|
-
const CONFIG_PATH = path_1.default.join(process.cwd(), "nimpl-i18n.js");
|
|
44
|
-
// Crutch bypass of conversion by the assembler to require
|
|
45
|
-
const dynamicImport = new Function("p", "return import(p)");
|
|
46
|
-
const getConfig = async () => {
|
|
47
|
-
let clientConfig;
|
|
48
|
-
if (fs_1.default.existsSync(CONFIG_PATH)) {
|
|
49
|
-
const config = await dynamicImport((0, url_1.pathToFileURL)(CONFIG_PATH).href);
|
|
50
|
-
clientConfig = config.default;
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
// @ts-expect-error will be imported from the alias configuration in webpack
|
|
54
|
-
const config = await Promise.resolve().then(() => __importStar(require("@nimpl/i18n/clientConfig")));
|
|
55
|
-
clientConfig = config.default;
|
|
56
|
-
}
|
|
57
|
-
const { load, getLanguage, languages } = clientConfig;
|
|
58
|
-
if (!load) {
|
|
59
|
-
throw new Error(`Can't find "load" method in configuration file - https://github.com/alexdln/nimpl-i18n#configuration`);
|
|
60
|
-
}
|
|
61
|
-
if (!languages) {
|
|
62
|
-
throw new Error(`Can't find "languages" list in configuration file - https://github.com/alexdln/nimpl-i18n#configuration`);
|
|
63
|
-
}
|
|
64
|
-
if (!getLanguage) {
|
|
65
|
-
throw new Error(`Can't find "getLanguage" method in configuration file - https://github.com/alexdln/nimpl-i18n#configuration`);
|
|
66
|
-
}
|
|
67
|
-
return clientConfig;
|
|
68
|
-
};
|
|
69
|
-
exports.getConfig = getConfig;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isPromise = void 0;
|
|
4
|
-
const isPromise = (el) => {
|
|
5
|
-
if (el && typeof el === "object" && "then" in el) {
|
|
6
|
-
return true;
|
|
7
|
-
}
|
|
8
|
-
return false;
|
|
9
|
-
};
|
|
10
|
-
exports.isPromise = isPromise;
|
package/dist/i18n.js
DELETED
|
@@ -1,30 +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.i18n = void 0;
|
|
7
|
-
const fs_1 = require("fs");
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const CONFIG_PATH = path_1.default.join(process.cwd(), "nimpl-i18n.js");
|
|
10
|
-
const i18n = () => {
|
|
11
|
-
return (nextConfig = {}) => {
|
|
12
|
-
return {
|
|
13
|
-
...nextConfig,
|
|
14
|
-
webpack: (config, options) => {
|
|
15
|
-
if (!(0, fs_1.existsSync)(CONFIG_PATH))
|
|
16
|
-
throw new Error("Please create and configure nimpl-i18n.js");
|
|
17
|
-
config.resolve ||= {};
|
|
18
|
-
config.resolve.alias = {
|
|
19
|
-
...config.resolve.alias,
|
|
20
|
-
"@nimpl/i18n/clientConfig": CONFIG_PATH,
|
|
21
|
-
};
|
|
22
|
-
if (typeof nextConfig.webpack === "function") {
|
|
23
|
-
return nextConfig.webpack(config, options);
|
|
24
|
-
}
|
|
25
|
-
return config;
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
exports.i18n = i18n;
|
package/dist/lib/loadI18nData.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadI18nData = void 0;
|
|
4
|
-
const get_pathname_1 = require("@nimpl/getters/get-pathname");
|
|
5
|
-
const get_params_1 = require("@nimpl/getters/get-params");
|
|
6
|
-
const getConfig_1 = require("../configuration/getConfig");
|
|
7
|
-
const loadI18nData = async () => {
|
|
8
|
-
const config = await (0, getConfig_1.getConfig)();
|
|
9
|
-
const language = await config.getLanguage({
|
|
10
|
-
get pathname() {
|
|
11
|
-
return (0, get_pathname_1.getPathname)();
|
|
12
|
-
},
|
|
13
|
-
get params() {
|
|
14
|
-
return (0, get_params_1.getParams)();
|
|
15
|
-
},
|
|
16
|
-
});
|
|
17
|
-
if (!language || !config.languages.includes(language)) {
|
|
18
|
-
throw new Error(`Can\' load data for language "${language}", valid languages are: ${config.languages.join(", ")}`);
|
|
19
|
-
}
|
|
20
|
-
const dictionary = await config.load(language);
|
|
21
|
-
return { dictionary, language };
|
|
22
|
-
};
|
|
23
|
-
exports.loadI18nData = loadI18nData;
|
package/getTranslation.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { type I18nOptions } from "./types";
|
|
2
|
-
type GetTranslationReturnType = {
|
|
3
|
-
t: (term: string, opts?: I18nOptions) => string;
|
|
4
|
-
language: string;
|
|
5
|
-
};
|
|
6
|
-
export declare const getTranslation: (options?: {
|
|
7
|
-
language?: string;
|
|
8
|
-
namespace?: string;
|
|
9
|
-
}) => Promise<GetTranslationReturnType>;
|
|
10
|
-
export {};
|
package/i18n.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const i18n: () => (nextConfig?: any) => any;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|