@yiiamee/multilinguist 1.2.8 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -30
- package/dist/module.json +7 -1
- package/dist/module.mjs +19 -8
- package/dist/runtime/composables/useLocalization.d.ts +3 -2
- package/dist/runtime/composables/useLocalization.js +7 -3
- package/dist/runtime/composables/useMultilinguist.d.ts +3 -1
- package/dist/runtime/plugin.js +12 -3
- package/dist/runtime/types/generated-locales.d.ts +40 -0
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
# Nuxt
|
|
1
|
+
# Nuxt Multilinguist module
|
|
2
2
|
|
|
3
|
-
Multilinguist is a simple but smoothly working module for easy and seamless localization implementation for Nuxt
|
|
3
|
+
Multilinguist is a simple but smoothly working module for easy and seamless localization implementation for Nuxt
|
|
4
|
+
applications.
|
|
4
5
|
|
|
5
6
|
## Key Features
|
|
6
7
|
|
|
@@ -8,6 +9,7 @@ Multilinguist is a simple but smoothly working module for easy and seamless loca
|
|
|
8
9
|
- Works perfectly fine on both SSR + CSR
|
|
9
10
|
- No memory leaks and running out of memory errors
|
|
10
11
|
- Autocompletion & validation of keys
|
|
12
|
+
- Autodetection of browser's locale
|
|
11
13
|
|
|
12
14
|
## Installation
|
|
13
15
|
|
|
@@ -20,7 +22,7 @@ Then, add the module to your nuxt.config
|
|
|
20
22
|
```nuxt.config.ts
|
|
21
23
|
export default defineNuxtConfig({
|
|
22
24
|
modules: [
|
|
23
|
-
@yiiamee/multilinguist,
|
|
25
|
+
"@yiiamee/multilinguist",
|
|
24
26
|
],
|
|
25
27
|
multilinguist: {
|
|
26
28
|
defaultLocale: "en", // string representing key to your default (fallback) locale
|
|
@@ -35,18 +37,65 @@ Then, create a "locales" directory in /public directory. This is necessary for m
|
|
|
35
37
|
|
|
36
38
|
Now, you're ready to use Multilinguist Module!
|
|
37
39
|
|
|
40
|
+
After running your project, you can see the following warning:
|
|
41
|
+
|
|
42
|
+

|
|
43
|
+
|
|
44
|
+
**Dont be scared**, this is just a message from the module, indicating that your locales are typed, and that the module
|
|
45
|
+
can properly perform type-checks and autocompletion.
|
|
46
|
+
|
|
47
|
+
Also, on both SSR and CSR, you will see the following message:
|
|
48
|
+
|
|
49
|
+

|
|
50
|
+
|
|
51
|
+
It indicated that the module has been initialized successfully.
|
|
52
|
+
|
|
53
|
+
To disable logs, you can set the `logging` option in your nuxt.config to `false` (by default, it is `true`):
|
|
54
|
+
|
|
55
|
+
```nuxt.config.ts
|
|
56
|
+
export default defineNuxtConfig({
|
|
57
|
+
modules: [
|
|
58
|
+
"@yiiamee/multilinguist",
|
|
59
|
+
],
|
|
60
|
+
multilinguist: {
|
|
61
|
+
defaultLocale: "en",
|
|
62
|
+
supportedLanguages: ["en", "es"],
|
|
63
|
+
logging: false, // Boolean value to define if the module should send internal logs to console
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
```
|
|
67
|
+
Also, Multilinguist offers a functionality to set the default browser's locale as default if it is available in the supported languages array.
|
|
68
|
+
|
|
69
|
+
To disable this option, you can set the `setBrowserLanguage` option to `false` as it is `true` by default:
|
|
70
|
+
|
|
71
|
+
```nuxt.config.ts
|
|
72
|
+
export default defineNuxtConfig({
|
|
73
|
+
modules: [
|
|
74
|
+
"@yiiamee/multilinguist",
|
|
75
|
+
],
|
|
76
|
+
multilinguist: {
|
|
77
|
+
defaultLocale: "en",
|
|
78
|
+
supportedLanguages: ["en", "es"],
|
|
79
|
+
logging: false,
|
|
80
|
+
setBrowserLanguage: false, // by default: true
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
|
|
38
86
|
# Usage
|
|
39
87
|
|
|
40
|
-
### t()—famous translate function
|
|
41
|
-
|
|
88
|
+
### t()—famous translate function
|
|
89
|
+
|
|
42
90
|
```vue
|
|
91
|
+
|
|
43
92
|
<script setup lang="ts">
|
|
44
|
-
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
|
|
45
|
-
|
|
46
|
-
const pageTitle = computed(() => {
|
|
47
|
-
|
|
48
|
-
});
|
|
49
|
-
</script>
|
|
93
|
+
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
|
|
94
|
+
|
|
95
|
+
const pageTitle = computed(() => {
|
|
96
|
+
return t("Hello, World");
|
|
97
|
+
});
|
|
98
|
+
</script>
|
|
50
99
|
|
|
51
100
|
<template>
|
|
52
101
|
<h3>{{ pageTitle }}</h3>
|
|
@@ -55,9 +104,10 @@ const pageTitle = computed(() => {
|
|
|
55
104
|
```
|
|
56
105
|
|
|
57
106
|
It also supports nested keys and dynamic keys with variables;
|
|
58
|
-
you only need to pass the second argument, an object with used in the key variables:
|
|
107
|
+
you only need to pass the second argument, an object with used in the key variables:
|
|
59
108
|
|
|
60
109
|
```vue
|
|
110
|
+
|
|
61
111
|
<template>
|
|
62
112
|
<span>{{ t("Paste your variable here", { variable: locale }) }}</span>
|
|
63
113
|
</template>
|
|
@@ -72,13 +122,14 @@ And your JSON must look like that:
|
|
|
72
122
|
```
|
|
73
123
|
|
|
74
124
|
```vue
|
|
125
|
+
|
|
75
126
|
<script setup lang="ts">
|
|
76
|
-
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
|
|
77
|
-
|
|
78
|
-
const pageTitle = computed(() => {
|
|
79
|
-
|
|
80
|
-
});
|
|
81
|
-
</script>
|
|
127
|
+
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
|
|
128
|
+
|
|
129
|
+
const pageTitle = computed(() => {
|
|
130
|
+
return t("Hello, World");
|
|
131
|
+
});
|
|
132
|
+
</script>
|
|
82
133
|
|
|
83
134
|
<template>
|
|
84
135
|
<h3>{{ pageTitle }}</h3>
|
|
@@ -101,15 +152,16 @@ And validation:
|
|
|
101
152
|
### Set another value to the current locale:
|
|
102
153
|
|
|
103
154
|
```vue
|
|
155
|
+
|
|
104
156
|
<script setup lang="ts">
|
|
105
|
-
const { t, setLocale } = useMultilinguist();
|
|
106
|
-
// setLocale function accepts a string that should match one of defined
|
|
107
|
-
// in the nuxt.config strings from supportedLanguages array
|
|
108
|
-
|
|
109
|
-
const pageTitle = computed(() => {
|
|
110
|
-
|
|
111
|
-
});
|
|
112
|
-
</script>
|
|
157
|
+
const { t, setLocale } = useMultilinguist();
|
|
158
|
+
// setLocale function accepts a string that should match one of defined
|
|
159
|
+
// in the nuxt.config strings from supportedLanguages array
|
|
160
|
+
|
|
161
|
+
const pageTitle = computed(() => {
|
|
162
|
+
return t("Hello, World");
|
|
163
|
+
});
|
|
164
|
+
</script>
|
|
113
165
|
|
|
114
166
|
<template>
|
|
115
167
|
<h3>{{ pageTitle }}</h3>
|
|
@@ -120,18 +172,45 @@ const pageTitle = computed(() => {
|
|
|
120
172
|
### Get current locale
|
|
121
173
|
|
|
122
174
|
```vue
|
|
175
|
+
|
|
176
|
+
<script setup lang="ts">
|
|
177
|
+
const { t, setLocale, locale } = useMultilinguist();
|
|
178
|
+
// locale is SSR friendly, shared among the app ref state
|
|
179
|
+
|
|
180
|
+
const pageTitle = computed(() => {
|
|
181
|
+
return t("Hello, World");
|
|
182
|
+
});
|
|
183
|
+
</script>
|
|
184
|
+
|
|
185
|
+
<template>
|
|
186
|
+
<h3>{{ pageTitle }}</h3>
|
|
187
|
+
<h6>Current Locale: {{ locale }}</h6>
|
|
188
|
+
<button @click="setLocale('es')">{{ t("Switch Locale") }}</button>
|
|
189
|
+
</template>
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Get locales list
|
|
193
|
+
|
|
194
|
+
```vue
|
|
195
|
+
|
|
123
196
|
<script setup lang="ts">
|
|
124
|
-
const { t, setLocale, locale } = useMultilinguist();
|
|
125
|
-
//
|
|
197
|
+
const { t, setLocale, locale, locales } = useMultilinguist();
|
|
198
|
+
// Locales is a computed ref, which value is being set from your nuxt.config
|
|
126
199
|
|
|
127
|
-
const pageTitle = computed(() => {
|
|
200
|
+
const pageTitle = computed(() => {
|
|
128
201
|
return t("Hello, World");
|
|
129
|
-
});
|
|
202
|
+
});
|
|
130
203
|
</script>
|
|
131
204
|
|
|
132
205
|
<template>
|
|
133
206
|
<h3>{{ pageTitle }}</h3>
|
|
134
207
|
<h6>Current Locale: {{ locale }}</h6>
|
|
208
|
+
<span
|
|
209
|
+
v-for="localeItem from locales"
|
|
210
|
+
:key="localeItem"
|
|
211
|
+
>
|
|
212
|
+
| {{ localeItem }} |
|
|
213
|
+
</span>
|
|
135
214
|
<button @click="setLocale('es')">{{ t("Switch Locale") }}</button>
|
|
136
215
|
</template>
|
|
137
216
|
```
|
package/dist/module.json
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yiiamee/multilinguist",
|
|
3
3
|
"configKey": "multilinguist",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.1",
|
|
5
5
|
"compatibility": {
|
|
6
6
|
"nuxt": "^3.0.0"
|
|
7
7
|
},
|
|
8
|
+
"defaults": {
|
|
9
|
+
"logging": true,
|
|
10
|
+
"defaultLocale": "",
|
|
11
|
+
"supportedLanguages": [],
|
|
12
|
+
"setBrowserLanguage": true
|
|
13
|
+
},
|
|
8
14
|
"builder": {
|
|
9
15
|
"@nuxt/module-builder": "1.0.1",
|
|
10
16
|
"unbuild": "3.5.0"
|
package/dist/module.mjs
CHANGED
|
@@ -49,7 +49,7 @@ declare module 'vue' {
|
|
|
49
49
|
}
|
|
50
50
|
`;
|
|
51
51
|
};
|
|
52
|
-
function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath) {
|
|
52
|
+
function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath, logging = true) {
|
|
53
53
|
async function generateTypes() {
|
|
54
54
|
const defaultLocalePath = path.join(localesPath, `${defaultLocaleFromConfig}.json`);
|
|
55
55
|
if (!fs.existsSync(defaultLocalePath)) {
|
|
@@ -61,7 +61,9 @@ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath)
|
|
|
61
61
|
const output = generateOutputString(() => keys.map((key) => ` ${JSON.stringify(key)}: string;`).join("\n"));
|
|
62
62
|
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
63
63
|
fs.writeFileSync(outPath, output, "utf-8");
|
|
64
|
-
|
|
64
|
+
if (logging) {
|
|
65
|
+
console?.warn(`\u2705 Generated types to ${outPath} from ${defaultLocalePath}`);
|
|
66
|
+
}
|
|
65
67
|
}
|
|
66
68
|
return {
|
|
67
69
|
name: "vite-plugin-generate-locales-types",
|
|
@@ -80,22 +82,35 @@ const module = defineNuxtModule({
|
|
|
80
82
|
meta: {
|
|
81
83
|
name: "@yiiamee/multilinguist",
|
|
82
84
|
configKey: "multilinguist",
|
|
83
|
-
version: "1.
|
|
85
|
+
version: "1.3.1",
|
|
84
86
|
compatibility: {
|
|
85
87
|
nuxt: "^3.0.0"
|
|
88
|
+
},
|
|
89
|
+
defaults: {
|
|
90
|
+
logging: true,
|
|
91
|
+
defaultLocale: "",
|
|
92
|
+
supportedLanguages: [],
|
|
93
|
+
setBrowserLanguage: true
|
|
86
94
|
}
|
|
87
95
|
},
|
|
88
96
|
setup(moduleOptions, nuxtApp) {
|
|
89
97
|
const resolver = createResolver(import.meta.url);
|
|
90
98
|
addPlugin(resolver.resolve("runtime/plugin"));
|
|
91
99
|
addImportsDir(resolver.resolve("runtime/composables"));
|
|
100
|
+
nuxtApp.options.runtimeConfig.public.multilinguist = {
|
|
101
|
+
defaultLocale: moduleOptions.defaultLocale,
|
|
102
|
+
supportedLanguages: moduleOptions.supportedLanguages,
|
|
103
|
+
logging: typeof moduleOptions.logging === "boolean" ? moduleOptions.logging : true,
|
|
104
|
+
setBrowserLanguage: typeof moduleOptions.setBrowserLanguage === "boolean" ? moduleOptions.setBrowserLanguage : true
|
|
105
|
+
};
|
|
92
106
|
nuxtApp.hook("vite:extendConfig", (viteConfig) => {
|
|
93
107
|
viteConfig.plugins = viteConfig.plugins || [];
|
|
94
108
|
viteConfig.plugins.push(
|
|
95
109
|
GenerateLocaleKeysPlugin(
|
|
96
110
|
moduleOptions.defaultLocale,
|
|
97
111
|
`${nuxtApp.options.rootDir}/public/locales`,
|
|
98
|
-
resolver.resolve("./runtime/types/generated-locales.d.ts")
|
|
112
|
+
resolver.resolve("./runtime/types/generated-locales.d.ts"),
|
|
113
|
+
moduleOptions.logging
|
|
99
114
|
)
|
|
100
115
|
);
|
|
101
116
|
});
|
|
@@ -104,10 +119,6 @@ const module = defineNuxtModule({
|
|
|
104
119
|
path: resolver.resolve("./runtime/types/generated-locales.d.ts")
|
|
105
120
|
});
|
|
106
121
|
});
|
|
107
|
-
nuxtApp.options.runtimeConfig.public.multilinguist = {
|
|
108
|
-
defaultLocale: moduleOptions.defaultLocale,
|
|
109
|
-
supportedLanguages: moduleOptions.supportedLanguages
|
|
110
|
-
};
|
|
111
122
|
}
|
|
112
123
|
});
|
|
113
124
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Ref } from "vue";
|
|
1
|
+
import { type Ref, type ComputedRef } from "vue";
|
|
2
2
|
import type { LocaleKey } from "../types/generated-locales.js";
|
|
3
3
|
export type TranslationMap = readonly string[];
|
|
4
4
|
export type Locale<T extends TranslationMap> = T[number];
|
|
@@ -8,5 +8,6 @@ export type TMultilinguistResponse<T extends TranslationMap> = {
|
|
|
8
8
|
setLocale: (locale: Locale<T>) => void;
|
|
9
9
|
initLocalization: () => Promise<void>;
|
|
10
10
|
locale: Ref<Locale<T>>;
|
|
11
|
+
locales: ComputedRef<T>;
|
|
11
12
|
};
|
|
12
|
-
export default function useLocalization<const T extends TranslationMap>(supportedLanguages: T, defaultLocale: Locale<T
|
|
13
|
+
export default function useLocalization<const T extends TranslationMap>(supportedLanguages: T, defaultLocale: Locale<T>, setBrowserLanguage?: boolean): TMultilinguistResponse<T>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import useLocale from "../composables/useLocale.js";
|
|
2
2
|
import { useCookie, useState } from "nuxt/app";
|
|
3
3
|
import { computed, watch } from "vue";
|
|
4
|
-
export default function useLocalization(supportedLanguages, defaultLocale) {
|
|
4
|
+
export default function useLocalization(supportedLanguages, defaultLocale, setBrowserLanguage = true) {
|
|
5
5
|
const { locale: userBrowserLocale } = useLocale(supportedLanguages, defaultLocale);
|
|
6
6
|
const userSelectedLocale = useCookie("multilinguist-locale", {
|
|
7
|
-
default: () => supportedLanguages.includes(userBrowserLocale.value) ? userBrowserLocale.value : defaultLocale
|
|
7
|
+
default: () => supportedLanguages.includes(userBrowserLocale.value) && setBrowserLanguage ? userBrowserLocale.value : defaultLocale
|
|
8
8
|
});
|
|
9
9
|
const localeFiles = import.meta.glob("@/public/locales/*.json", { eager: true });
|
|
10
10
|
const loadedLanguages = useState("loaded-languages", () => ({}));
|
|
@@ -50,6 +50,9 @@ export default function useLocalization(supportedLanguages, defaultLocale) {
|
|
|
50
50
|
await loadLocaleMessages(defaultLocale);
|
|
51
51
|
await setLocale(userPrefferableLocale.value);
|
|
52
52
|
};
|
|
53
|
+
const locales = computed(() => {
|
|
54
|
+
return supportedLanguages;
|
|
55
|
+
});
|
|
53
56
|
watch(
|
|
54
57
|
() => locale.value,
|
|
55
58
|
async (val) => {
|
|
@@ -62,6 +65,7 @@ export default function useLocalization(supportedLanguages, defaultLocale) {
|
|
|
62
65
|
t,
|
|
63
66
|
setLocale,
|
|
64
67
|
initLocalization,
|
|
65
|
-
locale
|
|
68
|
+
locale,
|
|
69
|
+
locales
|
|
66
70
|
};
|
|
67
71
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import type { TranslationMap } from "./useLocalization.js";
|
|
1
2
|
export default function useMultilinguist(): {
|
|
2
|
-
t<K extends LocaleKey>(key: K, variables?: Record<string, string>): string;
|
|
3
|
+
t<K extends import("../types/generated-locales.js").LocaleKey>(key: K, variables?: Record<string, string>): string;
|
|
3
4
|
setLocale: (locale: string) => void;
|
|
4
5
|
initLocalization: () => Promise<void>;
|
|
5
6
|
locale: import("vue").Ref<string, string>;
|
|
7
|
+
locales: import("vue").ComputedRef<TranslationMap>;
|
|
6
8
|
};
|
package/dist/runtime/plugin.js
CHANGED
|
@@ -3,13 +3,22 @@ import useLocalization from "../runtime/composables/useLocalization";
|
|
|
3
3
|
export default defineNuxtPlugin(async (nuxtApp) => {
|
|
4
4
|
const config = nuxtApp.$config.public.multilinguist || {
|
|
5
5
|
defaultLocale: "en",
|
|
6
|
-
supportedLanguages: ["en"]
|
|
6
|
+
supportedLanguages: ["en"],
|
|
7
|
+
logging: true,
|
|
8
|
+
setBrowserLanguage: true
|
|
7
9
|
};
|
|
8
10
|
const supportedLanguages = config.supportedLanguages;
|
|
9
11
|
const defaultLocale = config.defaultLocale;
|
|
10
|
-
const
|
|
12
|
+
const shouldSetBrowserLanguage = config.setBrowserLanguage;
|
|
13
|
+
const { initLocalization, ...localizationProperties } = useLocalization(
|
|
14
|
+
supportedLanguages,
|
|
15
|
+
defaultLocale,
|
|
16
|
+
shouldSetBrowserLanguage
|
|
17
|
+
);
|
|
11
18
|
await initLocalization();
|
|
12
|
-
|
|
19
|
+
if (nuxtApp.$config.public.multilinguist.logging) {
|
|
20
|
+
console.log("[multilinguist] is initialised");
|
|
21
|
+
}
|
|
13
22
|
nuxtApp.provide("localization", localizationProperties);
|
|
14
23
|
nuxtApp.provide("t", localizationProperties.t);
|
|
15
24
|
nuxtApp._localization = localizationProperties;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
// AUTO-GENERATED FILE — DO NOT EDIT MANUALLY
|
|
3
|
+
|
|
4
|
+
export interface TranslationMessages {
|
|
5
|
+
"Hello, World": string;
|
|
6
|
+
"Switch Locale": string;
|
|
7
|
+
"Paste your variable here": string;
|
|
8
|
+
"nested.Nested key": string;
|
|
9
|
+
"nested.Language": string;
|
|
10
|
+
"nested.nested second level.nested second level language": string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type LocaleKey = keyof TranslationMessages;
|
|
14
|
+
|
|
15
|
+
export type TFunction = <K extends LocaleKey>(
|
|
16
|
+
key: K,
|
|
17
|
+
variables?: Record<string, string>
|
|
18
|
+
) => string;
|
|
19
|
+
|
|
20
|
+
declare global {
|
|
21
|
+
type LocaleKey = keyof TranslationMessages;
|
|
22
|
+
type TFunction = <K extends LocaleKey>(
|
|
23
|
+
key: K,
|
|
24
|
+
variables?: Record<string, string>
|
|
25
|
+
) => string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare module '#app' {
|
|
29
|
+
interface NuxtApp {
|
|
30
|
+
$t: TFunction;
|
|
31
|
+
t: TFunction;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare module 'vue' {
|
|
36
|
+
interface ComponentCustomProperties {
|
|
37
|
+
$t: TFunction;
|
|
38
|
+
t: TFunction;
|
|
39
|
+
}
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yiiamee/multilinguist",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Nuxt
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "Nuxt Multilinguist module for localizations",
|
|
5
5
|
"repository": "yiiameeMich/multilinguist",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
@@ -27,7 +27,10 @@
|
|
|
27
27
|
"translation",
|
|
28
28
|
"localization nuxt",
|
|
29
29
|
"translation nuxt",
|
|
30
|
-
"nuxt translation"
|
|
30
|
+
"nuxt translation",
|
|
31
|
+
"nuxt multilinguist",
|
|
32
|
+
"multilinguist nuxt",
|
|
33
|
+
"ssr localization"
|
|
31
34
|
],
|
|
32
35
|
"bugs": {
|
|
33
36
|
"email": "michaelkukh.dev@gmail.com"
|