nuxt-i18n-micro 1.1.12 → 1.1.14
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 +173 -0
- package/dist/client/200.html +1 -1
- package/dist/client/404.html +1 -1
- package/dist/client/_nuxt/builds/latest.json +1 -1
- package/dist/client/_nuxt/builds/meta/978c1c94-d4c2-447f-bdbf-d058cee4ff80.json +1 -0
- package/dist/client/index.html +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +15 -8
- package/dist/runtime/components/i18n-t.vue +61 -0
- package/dist/runtime/plugins/01.plugin.d.ts +0 -13
- package/dist/runtime/plugins/01.plugin.js +1 -1
- package/dist/runtime/plugins/03.define.d.ts +2 -2
- package/dist/runtime/plugins/03.define.js +18 -3
- package/dist/runtime/translationHelper.js +1 -1
- package/package.json +1 -1
- package/dist/client/_nuxt/builds/meta/e3ab2284-9b25-4b7a-b8d6-8e1f2eb8cf35.json +0 -1
package/README.md
CHANGED
|
@@ -176,6 +176,46 @@ $mergeTranslations({
|
|
|
176
176
|
})
|
|
177
177
|
```
|
|
178
178
|
|
|
179
|
+
## `$defineI18nRoute(routeDefinition: { locales?: string[] | Record<string, Record<string, TranslationObject>> })`
|
|
180
|
+
Defines route behavior based on the current locale. This method can be used to control access to specific routes based on available locales or to provide translations for specific locales.
|
|
181
|
+
|
|
182
|
+
- `locales`: This property determines which locales are available for the route. It can be either:
|
|
183
|
+
- An array of strings, where each string represents an available locale (e.g., `['en', 'fr', 'de']`).
|
|
184
|
+
- An object where each key is a locale code, and the value is either an object containing translations or an empty object if you do not wish to provide translations for that locale.
|
|
185
|
+
|
|
186
|
+
### Example 1: Controlling Access Based on Locales
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { useNuxtApp } from '#imports'
|
|
190
|
+
|
|
191
|
+
const { $defineI18nRoute } = useNuxtApp()
|
|
192
|
+
|
|
193
|
+
$defineI18nRoute({
|
|
194
|
+
locales: ['en', 'fr', 'de'] // Only these locales are allowed for this route
|
|
195
|
+
})
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Example 2: Providing Translations for Locales
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { useNuxtApp } from '#imports'
|
|
202
|
+
|
|
203
|
+
const { $defineI18nRoute } = useNuxtApp()
|
|
204
|
+
|
|
205
|
+
$defineI18nRoute({
|
|
206
|
+
locales: {
|
|
207
|
+
en: { greeting: 'Hello', farewell: 'Goodbye' },
|
|
208
|
+
fr: { greeting: 'Bonjour', farewell: 'Au revoir' },
|
|
209
|
+
de: { greeting: 'Hallo', farewell: { aaa: { bbb: "Auf Wiedersehen" } } },
|
|
210
|
+
ru: {} // Russian locale is allowed but no translations are provided
|
|
211
|
+
}
|
|
212
|
+
})
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Explanation:
|
|
216
|
+
- **Locales Array**: If you only want to specify which locales are allowed for a route, pass an array of locale codes. The user will only be able to access this route if the current locale is in this list.
|
|
217
|
+
- **Locales Object**: If you want to provide specific translations for each locale, pass an object where each key is a locale code. The value should be an object with key-value pairs for translations. If you do not wish to provide translations for a locale but still want to allow access, pass an empty object (`{}`) for that locale.
|
|
218
|
+
|
|
179
219
|
## useNuxtApp
|
|
180
220
|
|
|
181
221
|
```ts
|
|
@@ -301,6 +341,139 @@ To further optimize performance, `Nuxt I18n Micro` supports caching and pre-rend
|
|
|
301
341
|
- **Pre-rendering**: During the build process, the module can pre-render translation files for all configured locales and routes, which are then served directly from the server without the need for runtime requests.
|
|
302
342
|
|
|
303
343
|
|
|
344
|
+
Certainly! Here's an updated section for your README that includes the description of the `<i18n-t>` component, along with explanations for each prop and examples of usage.
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
## `<i18n-t>` Component
|
|
349
|
+
|
|
350
|
+
The `<i18n-t>` component in `Nuxt I18n Micro` is a flexible and powerful tool designed to handle translations within your Nuxt application. It supports various features such as interpolation, pluralization, and dynamic HTML rendering.
|
|
351
|
+
|
|
352
|
+
### Component Props
|
|
353
|
+
|
|
354
|
+
- **`keypath`** (`string`, required): The translation key used to look up the corresponding translation string from the locale files.
|
|
355
|
+
|
|
356
|
+
- **`plural`** (`number | null`, optional): The count used for pluralization. If provided, the component will use the `$tc` method for translating based on the pluralization rules defined for the locale.
|
|
357
|
+
|
|
358
|
+
- **`tag`** (`string`, optional, default: `'span'`): The HTML tag used to wrap the translated content. You can specify any valid HTML tag, such as `div`, `p`, `h1`, etc.
|
|
359
|
+
|
|
360
|
+
- **`scope`** (`string`, optional, default: `'global'`): Specifies the scope of the translation. This prop is useful if you have different translation scopes in your application, although the default is set to `'global'`.
|
|
361
|
+
|
|
362
|
+
- **`params`** (`Record<string, string | number | boolean>`, optional, default: `() => ({})`): An object containing key-value pairs to be interpolated into the translation string. This is useful for dynamic translations where certain values need to be inserted into the text.
|
|
363
|
+
|
|
364
|
+
- **`defaultValue`** (`string`, optional, default: `''`): A fallback translation that will be displayed if the `keypath` does not match any key in the translation files.
|
|
365
|
+
|
|
366
|
+
- **`html`** (`boolean`, optional, default: `false`): A flag indicating whether the translation contains HTML. When set to `true`, the translated string will be rendered as raw HTML.
|
|
367
|
+
|
|
368
|
+
- **`locale`** (`string`, optional): The locale to use for this translation. If not provided, the component will use the current locale set by the application.
|
|
369
|
+
|
|
370
|
+
- **`wrap`** (`boolean`, optional, default: `true`): Determines if the translated content should be wrapped in the specified `tag`. If set to `false`, the component will not wrap the translation in an HTML element.
|
|
371
|
+
|
|
372
|
+
- **`customPluralRule`** (`(value: string, count: number, locale: string) => string`, optional): A custom function for handling pluralization. This allows you to override the default pluralization behavior with your custom logic.
|
|
373
|
+
|
|
374
|
+
- **`hideIfEmpty`** (`boolean`, optional, default: `false`): If `true`, the component will not render anything if the translation string is empty. This is useful for conditional rendering based on the presence of a translation.
|
|
375
|
+
|
|
376
|
+
### Examples of Usage
|
|
377
|
+
|
|
378
|
+
#### Basic Usage
|
|
379
|
+
|
|
380
|
+
```vue
|
|
381
|
+
<i18n-t keypath="welcomeMessage"></i18n-t>
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
This will render the translation associated with the key `welcomeMessage` using the current locale.
|
|
385
|
+
|
|
386
|
+
#### Pluralization
|
|
387
|
+
|
|
388
|
+
```vue
|
|
389
|
+
<i18n-t keypath="apples" :plural="10" tag="div"></i18n-t>
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
This will handle the pluralization for the translation key `apples` and render it inside a `div` element.
|
|
393
|
+
|
|
394
|
+
#### Interpolation with Parameters
|
|
395
|
+
|
|
396
|
+
```vue
|
|
397
|
+
<i18n-t keypath="greeting" :params="{ name: 'John' }"></i18n-t>
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
Assuming the translation string is `"Hello, {name}!"`, this will render `"Hello, John!"`.
|
|
401
|
+
|
|
402
|
+
#### Rendering with HTML Content
|
|
403
|
+
|
|
404
|
+
```vue
|
|
405
|
+
<i18n-t keypath="richText" :html="true"></i18n-t>
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
If `richText` contains HTML content like `"<strong>Welcome</strong> to our site!"`, setting `html` to `true` will render the HTML directly.
|
|
409
|
+
|
|
410
|
+
#### Using a Custom Locale
|
|
411
|
+
|
|
412
|
+
```vue
|
|
413
|
+
<i18n-t keypath="welcomeMessage" locale="fr"></i18n-t>
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
This will render the translation in French, overriding the application's current locale.
|
|
417
|
+
|
|
418
|
+
#### Conditional Rendering
|
|
419
|
+
|
|
420
|
+
```vue
|
|
421
|
+
<i18n-t keypath="optionalMessage" :hideIfEmpty="true"></i18n-t>
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
This will render nothing if the translation for `optionalMessage` is empty.
|
|
425
|
+
|
|
426
|
+
#### Custom Pluralization Rule
|
|
427
|
+
|
|
428
|
+
```vue
|
|
429
|
+
<i18n-t
|
|
430
|
+
keypath="items"
|
|
431
|
+
:plural="itemCount"
|
|
432
|
+
:customPluralRule="(value, count, locale) => {
|
|
433
|
+
return count === 1 ? 'One item' : value.replace('{count}', `${count}`)
|
|
434
|
+
}"></i18n-t>
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
This uses a custom function to handle the pluralization, bypassing the default logic.
|
|
438
|
+
|
|
439
|
+
### Advanced Example
|
|
440
|
+
|
|
441
|
+
```vue
|
|
442
|
+
<i18n-t
|
|
443
|
+
keypath="notifications"
|
|
444
|
+
:plural="unreadCount"
|
|
445
|
+
tag="p"
|
|
446
|
+
:params="{ username: 'Alice' }"
|
|
447
|
+
defaultValue="You have no notifications"
|
|
448
|
+
:html="false"
|
|
449
|
+
locale="en"
|
|
450
|
+
:wrap="true"
|
|
451
|
+
:hideIfEmpty="false">
|
|
452
|
+
</i18n-t>
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
### Slot Usage Example
|
|
456
|
+
|
|
457
|
+
Here's how you can use the slot and access the `translation` within it:
|
|
458
|
+
|
|
459
|
+
```vue
|
|
460
|
+
<i18n-t keypath="welcomeMessage">
|
|
461
|
+
<template #default="{ translation }">
|
|
462
|
+
<strong>{{ translation }}</strong>
|
|
463
|
+
</template>
|
|
464
|
+
</i18n-t>
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
### Explanation:
|
|
468
|
+
|
|
469
|
+
- **Slot Prop `translation`**:
|
|
470
|
+
- The component now provides `translation` as a slot prop, which can be accessed in the parent component’s slot content.
|
|
471
|
+
- In this example, the translation is wrapped in a `<strong>` tag, allowing you to apply custom styling or further modify how the translation is displayed.
|
|
472
|
+
|
|
473
|
+
---
|
|
474
|
+
|
|
475
|
+
The `<i18n-t>` component provides a powerful and flexible way to handle translations in your Nuxt application, accommodating a wide range of use cases from simple text translations to complex pluralization and HTML rendering scenarios.
|
|
476
|
+
|
|
304
477
|
## Conclusion
|
|
305
478
|
|
|
306
479
|
`Nuxt I18n Micro` offers a highly efficient, minimalist approach to internationalization in Nuxt applications. By focusing on performance and simplicity, it provides a powerful alternative to heavier, more complex i18n solutions. Whether you're building a small website or a large-scale application, `Nuxt I18n Micro` helps you manage multilingual content with ease.
|
package/dist/client/200.html
CHANGED
|
@@ -8,4 +8,4 @@
|
|
|
8
8
|
<link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-500.B4KzowuE.css">
|
|
9
9
|
<link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/HZLiFEh-.js">
|
|
10
10
|
<script type="module" src="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" id="__NUXT_DATA__" data-ssr="false">[{"serverRendered":1},false]</script>
|
|
11
|
-
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"
|
|
11
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"978c1c94-d4c2-447f-bdbf-d058cee4ff80",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
package/dist/client/404.html
CHANGED
|
@@ -8,4 +8,4 @@
|
|
|
8
8
|
<link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-500.B4KzowuE.css">
|
|
9
9
|
<link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/HZLiFEh-.js">
|
|
10
10
|
<script type="module" src="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" id="__NUXT_DATA__" data-ssr="false">[{"serverRendered":1},false]</script>
|
|
11
|
-
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"
|
|
11
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"978c1c94-d4c2-447f-bdbf-d058cee4ff80",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"id":"
|
|
1
|
+
{"id":"978c1c94-d4c2-447f-bdbf-d058cee4ff80","timestamp":1724092454055}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"id":"978c1c94-d4c2-447f-bdbf-d058cee4ff80","timestamp":1724092454055,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
|
package/dist/client/index.html
CHANGED
|
@@ -8,4 +8,4 @@
|
|
|
8
8
|
<link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-500.B4KzowuE.css">
|
|
9
9
|
<link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/HZLiFEh-.js">
|
|
10
10
|
<script type="module" src="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" id="__NUXT_DATA__" data-ssr="false">[{"serverRendered":1},false]</script>
|
|
11
|
-
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"
|
|
11
|
+
<script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"978c1c94-d4c2-447f-bdbf-d058cee4ff80",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import * as fs from 'node:fs';
|
|
3
3
|
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
4
|
-
import { useNuxt, defineNuxtModule, createResolver, addPlugin, addImportsDir, addServerHandler, extendPages, addPrerenderRoutes } from '@nuxt/kit';
|
|
4
|
+
import { useNuxt, defineNuxtModule, createResolver, addPlugin, addImportsDir, addServerHandler, addComponentsDir, extendPages, addPrerenderRoutes } from '@nuxt/kit';
|
|
5
5
|
import { onDevToolsInitialized, extendServerRpc } from '@nuxt/devtools-kit';
|
|
6
6
|
|
|
7
7
|
const DEVTOOLS_UI_PORT = 3030;
|
|
@@ -33,7 +33,7 @@ function setupDevToolsUI(options, resolve) {
|
|
|
33
33
|
const localesDir = path.join(nuxt.options.rootDir, options.translationDir || "locales");
|
|
34
34
|
const pagesDir = path.join(nuxt.options.rootDir, options.translationDir || "locales", "pages");
|
|
35
35
|
const localeFiles = fs.readdirSync(localesDir);
|
|
36
|
-
const
|
|
36
|
+
const pageDirs = fs.readdirSync(pagesDir).filter((file) => fs.lstatSync(path.join(pagesDir, file)).isDirectory());
|
|
37
37
|
const locales = options.locales?.map((locale) => locale.code) || [];
|
|
38
38
|
return locales.map((locale) => {
|
|
39
39
|
const files = localeFiles.filter((file) => file.startsWith(locale));
|
|
@@ -42,11 +42,13 @@ function setupDevToolsUI(options, resolve) {
|
|
|
42
42
|
acc[file] = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
43
43
|
return acc;
|
|
44
44
|
}, {});
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
pageDirs.forEach((dir) => {
|
|
46
|
+
const pageLocaleFilePath = path.join(pagesDir, dir, `${locale}.json`);
|
|
47
|
+
if (fs.existsSync(pageLocaleFilePath)) {
|
|
48
|
+
const fileKey = path.join(dir, `${locale}.json`);
|
|
49
|
+
content[fileKey] = JSON.parse(fs.readFileSync(pageLocaleFilePath, "utf-8"));
|
|
50
|
+
files.push(fileKey);
|
|
51
|
+
}
|
|
50
52
|
});
|
|
51
53
|
return { locale, files, content };
|
|
52
54
|
});
|
|
@@ -91,7 +93,7 @@ const module = defineNuxtModule({
|
|
|
91
93
|
return (forms.length > 2 ? forms[2].trim() : forms[forms.length - 1].trim()).replace('{count}', count.toString())
|
|
92
94
|
}`
|
|
93
95
|
},
|
|
94
|
-
setup: function(options, nuxt) {
|
|
96
|
+
setup: async function(options, nuxt) {
|
|
95
97
|
const resolver = createResolver(import.meta.url);
|
|
96
98
|
nuxt.options.runtimeConfig.public.i18nConfig = {
|
|
97
99
|
rootDir: nuxt.options.rootDir,
|
|
@@ -136,6 +138,11 @@ const module = defineNuxtModule({
|
|
|
136
138
|
route: "/_locales/:page/:locale/data.json",
|
|
137
139
|
handler: resolver.resolve("./runtime/server/middleware/i18n-loader")
|
|
138
140
|
});
|
|
141
|
+
await addComponentsDir({
|
|
142
|
+
path: resolver.resolve("./runtime/components"),
|
|
143
|
+
pathPrefix: false,
|
|
144
|
+
extensions: ["vue"]
|
|
145
|
+
});
|
|
139
146
|
const localeRegex = options.locales.filter((locale) => locale.code !== options.defaultLocale || options.includeDefaultLocaleRoute).map((locale) => locale.code).join("|");
|
|
140
147
|
const pagesDir = path.resolve(nuxt.options.rootDir, options.translationDir, "pages");
|
|
141
148
|
extendPages((pages) => {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<component
|
|
3
|
+
:is="tag"
|
|
4
|
+
v-if="!hideIfEmpty || translation"
|
|
5
|
+
>
|
|
6
|
+
<slot
|
|
7
|
+
v-if="!html"
|
|
8
|
+
:translation="translation"
|
|
9
|
+
>
|
|
10
|
+
{{ translation }}
|
|
11
|
+
</slot>
|
|
12
|
+
<span
|
|
13
|
+
v-else
|
|
14
|
+
v-html="translation"
|
|
15
|
+
/>
|
|
16
|
+
</component>
|
|
17
|
+
</template>
|
|
18
|
+
|
|
19
|
+
<script lang="ts" setup>
|
|
20
|
+
import { computed, defineProps, withDefaults } from 'vue'
|
|
21
|
+
import { useNuxtApp } from '#app'
|
|
22
|
+
|
|
23
|
+
interface Props {
|
|
24
|
+
keypath: string
|
|
25
|
+
plural?: number | null
|
|
26
|
+
tag?: string
|
|
27
|
+
scope?: string
|
|
28
|
+
params?: Record<string, string | number | boolean>
|
|
29
|
+
defaultValue?: string
|
|
30
|
+
html?: boolean
|
|
31
|
+
locale?: string
|
|
32
|
+
wrap?: boolean
|
|
33
|
+
customPluralRule?: (value: string, count: number, locale: string) => string
|
|
34
|
+
hideIfEmpty?: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const props = withDefaults(defineProps<Props>(), {
|
|
38
|
+
plural: null,
|
|
39
|
+
tag: 'span',
|
|
40
|
+
scope: 'global',
|
|
41
|
+
params: () => ({}),
|
|
42
|
+
defaultValue: '',
|
|
43
|
+
html: false,
|
|
44
|
+
locale: undefined,
|
|
45
|
+
wrap: true,
|
|
46
|
+
hideIfEmpty: false,
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
const nuxtApp = useNuxtApp()
|
|
50
|
+
|
|
51
|
+
const translation = computed<string>(() => {
|
|
52
|
+
const localeToUse = props.locale || nuxtApp.$getLocale()
|
|
53
|
+
const translation = props.plural !== null
|
|
54
|
+
? props.customPluralRule
|
|
55
|
+
? props.customPluralRule(nuxtApp.$t(props.keypath, props.params), props.plural, localeToUse)
|
|
56
|
+
: nuxtApp.$tc(props.keypath, props.plural)
|
|
57
|
+
: nuxtApp.$t(props.keypath, props.params) as string
|
|
58
|
+
|
|
59
|
+
return translation || props.defaultValue || props.keypath
|
|
60
|
+
})
|
|
61
|
+
</script>
|
|
@@ -45,19 +45,6 @@ declare module 'nuxt/dist/app/nuxt' {
|
|
|
45
45
|
interface NuxtApp extends PluginsInjections {
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
declare module '@vue/runtime-core' {
|
|
49
|
-
interface ComponentCustomProperties {
|
|
50
|
-
$getLocale: () => string;
|
|
51
|
-
$getLocales: () => string[];
|
|
52
|
-
$t: <T extends Record<string, string | number | boolean>>(key: string, params?: T, defaultValue?: string) => string | number | boolean | Translations | PluralTranslations | unknown[] | unknown | null;
|
|
53
|
-
$tc: (key: string, count: number, defaultValue?: string) => string;
|
|
54
|
-
$has: (key: string) => boolean;
|
|
55
|
-
$mergeTranslations: (newTranslations: Translations) => void;
|
|
56
|
-
$switchLocale: (locale: string) => void;
|
|
57
|
-
$localeRoute: (to: RouteLocationRaw, locale?: string) => RouteLocationRaw;
|
|
58
|
-
$loadPageTranslations: (locale: string, routeName: string) => Promise<void>;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
48
|
declare module 'vue' {
|
|
62
49
|
interface ComponentCustomProperties extends PluginsInjections {
|
|
63
50
|
}
|
|
@@ -142,7 +142,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
142
142
|
const route2 = useRoute();
|
|
143
143
|
const routeName = route2.name.replace(`localized-`, "");
|
|
144
144
|
const locale = (route2.params?.locale ?? i18nConfig.defaultLocale).toString();
|
|
145
|
-
i18nHelper.margeTranslation(
|
|
145
|
+
i18nHelper.margeTranslation(locale, routeName, newTranslations);
|
|
146
146
|
},
|
|
147
147
|
switchLocale: (locale) => {
|
|
148
148
|
const route2 = useRoute();
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
declare const _default: import("#app").Plugin<{
|
|
2
2
|
defineI18nRoute: (routeDefinition: {
|
|
3
|
-
locales?: string[]
|
|
3
|
+
locales?: string[] | Record<string, Record<string, string>>;
|
|
4
4
|
}) => Promise<void | import("vue-router").NavigationFailure | undefined> | undefined;
|
|
5
5
|
}> & import("#app").ObjectPlugin<{
|
|
6
6
|
defineI18nRoute: (routeDefinition: {
|
|
7
|
-
locales?: string[]
|
|
7
|
+
locales?: string[] | Record<string, Record<string, string>>;
|
|
8
8
|
}) => Promise<void | import("vue-router").NavigationFailure | undefined> | undefined;
|
|
9
9
|
}>;
|
|
10
10
|
export default _default;
|
|
@@ -1,15 +1,30 @@
|
|
|
1
|
-
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
1
|
+
import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from "#app";
|
|
2
2
|
import { useRoute, useRouter } from "#imports";
|
|
3
3
|
export default defineNuxtPlugin((_nuxtApp) => {
|
|
4
4
|
const config = useRuntimeConfig();
|
|
5
5
|
const route = useRoute();
|
|
6
6
|
const router = useRouter();
|
|
7
7
|
const i18nConfig = config.public.i18nConfig;
|
|
8
|
+
const normalizeLocales = (locales) => {
|
|
9
|
+
if (Array.isArray(locales)) {
|
|
10
|
+
return locales.reduce((acc, locale) => {
|
|
11
|
+
acc[locale] = {};
|
|
12
|
+
return acc;
|
|
13
|
+
}, {});
|
|
14
|
+
} else if (typeof locales === "object" && locales !== null) {
|
|
15
|
+
return locales;
|
|
16
|
+
}
|
|
17
|
+
return {};
|
|
18
|
+
};
|
|
8
19
|
const defineI18nRoute = (routeDefinition) => {
|
|
9
20
|
const currentLocale = (route.params.locale || i18nConfig.defaultLocale).toString();
|
|
10
|
-
const
|
|
21
|
+
const normalizedLocales = normalizeLocales(routeDefinition.locales);
|
|
11
22
|
const { name } = route;
|
|
12
|
-
if (
|
|
23
|
+
if (normalizedLocales[currentLocale]) {
|
|
24
|
+
const translation = normalizedLocales[currentLocale];
|
|
25
|
+
const nuxtApp = useNuxtApp();
|
|
26
|
+
nuxtApp.$mergeTranslations(translation);
|
|
27
|
+
} else {
|
|
13
28
|
let defaultRouteName = name?.toString().replace("localized-", "");
|
|
14
29
|
const resolvedRoute = router.resolve({ name: defaultRouteName });
|
|
15
30
|
const newParams = { ...route.params };
|
|
@@ -40,7 +40,7 @@ export function useTranslationHelper() {
|
|
|
40
40
|
},
|
|
41
41
|
margeTranslation(locale, routeName, newTranslations) {
|
|
42
42
|
if (!routeLocaleCache[`${locale}:${routeName}`]) {
|
|
43
|
-
console.error(`marge: route ${routeName} not loaded`);
|
|
43
|
+
console.error(`marge: route ${locale}:${routeName} not loaded`);
|
|
44
44
|
}
|
|
45
45
|
routeLocaleCache[`${locale}:${routeName}`] = {
|
|
46
46
|
...generalLocaleCache[locale],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-i18n-micro",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.14",
|
|
4
4
|
"description": "Nuxt I18n Micro is a lightweight, high-performance internationalization module for Nuxt, designed to handle multi-language support with minimal overhead, fast build times, and efficient runtime performance.",
|
|
5
5
|
"repository": "s00d/nuxt-i18n-micro",
|
|
6
6
|
"license": "MIT",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"id":"e3ab2284-9b25-4b7a-b8d6-8e1f2eb8cf35","timestamp":1724081837773,"matcher":{"static":{},"wildcard":{},"dynamic":{}},"prerendered":[]}
|