@yiiamee/multilinguist 1.2.7 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -30
- package/dist/module.json +6 -1
- package/dist/module.mjs +62 -31
- package/dist/runtime/composables/useLocalization.d.ts +1 -1
- package/dist/runtime/composables/useMultilinguist.d.ts +1 -1
- package/dist/runtime/plugin.js +5 -2
- package/dist/runtime/types/generated-locales.d.ts +40 -0
- package/package.json +23 -5
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
|
|
|
@@ -20,7 +21,7 @@ Then, add the module to your nuxt.config
|
|
|
20
21
|
```nuxt.config.ts
|
|
21
22
|
export default defineNuxtConfig({
|
|
22
23
|
modules: [
|
|
23
|
-
@yiiamee/multilinguist,
|
|
24
|
+
"@yiiamee/multilinguist",
|
|
24
25
|
],
|
|
25
26
|
multilinguist: {
|
|
26
27
|
defaultLocale: "en", // string representing key to your default (fallback) locale
|
|
@@ -35,18 +36,47 @@ Then, create a "locales" directory in /public directory. This is necessary for m
|
|
|
35
36
|
|
|
36
37
|
Now, you're ready to use Multilinguist Module!
|
|
37
38
|
|
|
39
|
+
After running your project, you can see the following warning:
|
|
40
|
+
|
|
41
|
+

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

|
|
49
|
+
|
|
50
|
+
It indicated that the module has been initialized successfully.
|
|
51
|
+
|
|
52
|
+
To disable logs, you can set the logging option in your nuxt.config to false (by default, it is true):
|
|
53
|
+
|
|
54
|
+
```nuxt.config.ts
|
|
55
|
+
export default defineNuxtConfig({
|
|
56
|
+
modules: [
|
|
57
|
+
"@yiiamee/multilinguist",
|
|
58
|
+
],
|
|
59
|
+
multilinguist: {
|
|
60
|
+
defaultLocale: "en",
|
|
61
|
+
supportedLanguages: ["en", "es"],
|
|
62
|
+
logging: false, // Boolean value to define if the module should send internal logs to console
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
38
67
|
# Usage
|
|
39
68
|
|
|
40
|
-
### t()—famous translate function
|
|
41
|
-
|
|
69
|
+
### t()—famous translate function
|
|
70
|
+
|
|
42
71
|
```vue
|
|
72
|
+
|
|
43
73
|
<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>
|
|
74
|
+
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
|
|
75
|
+
|
|
76
|
+
const pageTitle = computed(() => {
|
|
77
|
+
return t("Hello, World");
|
|
78
|
+
});
|
|
79
|
+
</script>
|
|
50
80
|
|
|
51
81
|
<template>
|
|
52
82
|
<h3>{{ pageTitle }}</h3>
|
|
@@ -55,9 +85,10 @@ const pageTitle = computed(() => {
|
|
|
55
85
|
```
|
|
56
86
|
|
|
57
87
|
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:
|
|
88
|
+
you only need to pass the second argument, an object with used in the key variables:
|
|
59
89
|
|
|
60
90
|
```vue
|
|
91
|
+
|
|
61
92
|
<template>
|
|
62
93
|
<span>{{ t("Paste your variable here", { variable: locale }) }}</span>
|
|
63
94
|
</template>
|
|
@@ -72,13 +103,14 @@ And your JSON must look like that:
|
|
|
72
103
|
```
|
|
73
104
|
|
|
74
105
|
```vue
|
|
106
|
+
|
|
75
107
|
<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>
|
|
108
|
+
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
|
|
109
|
+
|
|
110
|
+
const pageTitle = computed(() => {
|
|
111
|
+
return t("Hello, World");
|
|
112
|
+
});
|
|
113
|
+
</script>
|
|
82
114
|
|
|
83
115
|
<template>
|
|
84
116
|
<h3>{{ pageTitle }}</h3>
|
|
@@ -101,15 +133,16 @@ And validation:
|
|
|
101
133
|
### Set another value to the current locale:
|
|
102
134
|
|
|
103
135
|
```vue
|
|
136
|
+
|
|
104
137
|
<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>
|
|
138
|
+
const { t, setLocale } = useMultilinguist();
|
|
139
|
+
// setLocale function accepts a string that should match one of defined
|
|
140
|
+
// in the nuxt.config strings from supportedLanguages array
|
|
141
|
+
|
|
142
|
+
const pageTitle = computed(() => {
|
|
143
|
+
return t("Hello, World");
|
|
144
|
+
});
|
|
145
|
+
</script>
|
|
113
146
|
|
|
114
147
|
<template>
|
|
115
148
|
<h3>{{ pageTitle }}</h3>
|
|
@@ -120,13 +153,14 @@ const pageTitle = computed(() => {
|
|
|
120
153
|
### Get current locale
|
|
121
154
|
|
|
122
155
|
```vue
|
|
156
|
+
|
|
123
157
|
<script setup lang="ts">
|
|
124
|
-
const { t, setLocale, locale } = useMultilinguist();
|
|
125
|
-
// locale is SSR friendly, shared among the app ref state
|
|
158
|
+
const { t, setLocale, locale } = useMultilinguist();
|
|
159
|
+
// locale is SSR friendly, shared among the app ref state
|
|
126
160
|
|
|
127
|
-
const pageTitle = computed(() => {
|
|
161
|
+
const pageTitle = computed(() => {
|
|
128
162
|
return t("Hello, World");
|
|
129
|
-
});
|
|
163
|
+
});
|
|
130
164
|
</script>
|
|
131
165
|
|
|
132
166
|
<template>
|
package/dist/module.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yiiamee/multilinguist",
|
|
3
3
|
"configKey": "multilinguist",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"compatibility": {
|
|
6
6
|
"nuxt": "^3.0.0"
|
|
7
7
|
},
|
|
8
|
+
"defaults": {
|
|
9
|
+
"logging": true,
|
|
10
|
+
"defaultLocale": "",
|
|
11
|
+
"supportedLanguages": []
|
|
12
|
+
},
|
|
8
13
|
"builder": {
|
|
9
14
|
"@nuxt/module-builder": "1.0.1",
|
|
10
15
|
"unbuild": "3.5.0"
|
package/dist/module.mjs
CHANGED
|
@@ -11,7 +11,45 @@ function flattenKeys(obj, prefix = "") {
|
|
|
11
11
|
return [fullKey];
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
const generateOutputString = (func) => {
|
|
15
|
+
return `
|
|
16
|
+
// AUTO-GENERATED FILE \u2014 DO NOT EDIT MANUALLY
|
|
17
|
+
|
|
18
|
+
export interface TranslationMessages {
|
|
19
|
+
${func()}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type LocaleKey = keyof TranslationMessages;
|
|
23
|
+
|
|
24
|
+
export type TFunction = <K extends LocaleKey>(
|
|
25
|
+
key: K,
|
|
26
|
+
variables?: Record<string, string>
|
|
27
|
+
) => string;
|
|
28
|
+
|
|
29
|
+
declare global {
|
|
30
|
+
type LocaleKey = keyof TranslationMessages;
|
|
31
|
+
type TFunction = <K extends LocaleKey>(
|
|
32
|
+
key: K,
|
|
33
|
+
variables?: Record<string, string>
|
|
34
|
+
) => string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare module '#app' {
|
|
38
|
+
interface NuxtApp {
|
|
39
|
+
$t: TFunction;
|
|
40
|
+
t: TFunction;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare module 'vue' {
|
|
45
|
+
interface ComponentCustomProperties {
|
|
46
|
+
$t: TFunction;
|
|
47
|
+
t: TFunction;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
`;
|
|
51
|
+
};
|
|
52
|
+
function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath, logging = true) {
|
|
15
53
|
async function generateTypes() {
|
|
16
54
|
const defaultLocalePath = path.join(localesPath, `${defaultLocaleFromConfig}.json`);
|
|
17
55
|
if (!fs.existsSync(defaultLocalePath)) {
|
|
@@ -20,32 +58,12 @@ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath)
|
|
|
20
58
|
}
|
|
21
59
|
const json = JSON.parse(fs.readFileSync(defaultLocalePath, "utf-8"));
|
|
22
60
|
const keys = flattenKeys(json);
|
|
23
|
-
const output =
|
|
24
|
-
`// AUTO-GENERATED FILE \u2014 DO NOT EDIT MANUALLY`,
|
|
25
|
-
`export interface TranslationMessages {`,
|
|
26
|
-
...keys.map((key) => ` ${JSON.stringify(key)}: string;`),
|
|
27
|
-
`}`,
|
|
28
|
-
``,
|
|
29
|
-
`export type LocaleKey = keyof TranslationMessages;`,
|
|
30
|
-
``,
|
|
31
|
-
`declare module '#app' {`,
|
|
32
|
-
` interface NuxtApp {`,
|
|
33
|
-
" $t<const K extends LocaleKey>(key: K, variables?: Record<string, string>): string; ",
|
|
34
|
-
" t<const K extends LocaleKey>(key: K, variables?: Record<string, string>): string; ",
|
|
35
|
-
` }`,
|
|
36
|
-
`}`,
|
|
37
|
-
``,
|
|
38
|
-
`declare module 'vue' {`,
|
|
39
|
-
` interface ComponentCustomProperties {`,
|
|
40
|
-
" $t<const K extends LocaleKey>(key: K, variables?: Record<string, string>): string; ",
|
|
41
|
-
" t<const K extends LocaleKey>(key: K, variables?: Record<string, string>): string; ",
|
|
42
|
-
` }`,
|
|
43
|
-
`}`,
|
|
44
|
-
``
|
|
45
|
-
].join("\n");
|
|
61
|
+
const output = generateOutputString(() => keys.map((key) => ` ${JSON.stringify(key)}: string;`).join("\n"));
|
|
46
62
|
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
47
63
|
fs.writeFileSync(outPath, output, "utf-8");
|
|
48
|
-
|
|
64
|
+
if (logging) {
|
|
65
|
+
console?.warn(`\u2705 Generated types to ${outPath} from ${defaultLocalePath}`);
|
|
66
|
+
}
|
|
49
67
|
}
|
|
50
68
|
return {
|
|
51
69
|
name: "vite-plugin-generate-locales-types",
|
|
@@ -64,28 +82,41 @@ const module = defineNuxtModule({
|
|
|
64
82
|
meta: {
|
|
65
83
|
name: "@yiiamee/multilinguist",
|
|
66
84
|
configKey: "multilinguist",
|
|
67
|
-
version: "1.
|
|
85
|
+
version: "1.3.0",
|
|
68
86
|
compatibility: {
|
|
69
87
|
nuxt: "^3.0.0"
|
|
88
|
+
},
|
|
89
|
+
defaults: {
|
|
90
|
+
logging: true,
|
|
91
|
+
defaultLocale: "",
|
|
92
|
+
supportedLanguages: []
|
|
70
93
|
}
|
|
71
94
|
},
|
|
72
95
|
setup(moduleOptions, nuxtApp) {
|
|
73
96
|
const resolver = createResolver(import.meta.url);
|
|
74
97
|
addPlugin(resolver.resolve("runtime/plugin"));
|
|
75
98
|
addImportsDir(resolver.resolve("runtime/composables"));
|
|
99
|
+
nuxtApp.options.runtimeConfig.public.multilinguist = {
|
|
100
|
+
defaultLocale: moduleOptions.defaultLocale,
|
|
101
|
+
supportedLanguages: moduleOptions.supportedLanguages,
|
|
102
|
+
logging: typeof moduleOptions.logging === "boolean" ? moduleOptions.logging : true
|
|
103
|
+
};
|
|
76
104
|
nuxtApp.hook("vite:extendConfig", (viteConfig) => {
|
|
77
105
|
viteConfig.plugins = viteConfig.plugins || [];
|
|
78
|
-
viteConfig.plugins.push(
|
|
106
|
+
viteConfig.plugins.push(
|
|
107
|
+
GenerateLocaleKeysPlugin(
|
|
108
|
+
moduleOptions.defaultLocale,
|
|
109
|
+
`${nuxtApp.options.rootDir}/public/locales`,
|
|
110
|
+
resolver.resolve("./runtime/types/generated-locales.d.ts"),
|
|
111
|
+
moduleOptions.logging
|
|
112
|
+
)
|
|
113
|
+
);
|
|
79
114
|
});
|
|
80
115
|
nuxtApp.hook("prepare:types", ({ references }) => {
|
|
81
116
|
references.push({
|
|
82
117
|
path: resolver.resolve("./runtime/types/generated-locales.d.ts")
|
|
83
118
|
});
|
|
84
119
|
});
|
|
85
|
-
nuxtApp.options.runtimeConfig.public.multilinguist = {
|
|
86
|
-
defaultLocale: moduleOptions.defaultLocale,
|
|
87
|
-
supportedLanguages: moduleOptions.supportedLanguages
|
|
88
|
-
};
|
|
89
120
|
}
|
|
90
121
|
});
|
|
91
122
|
|
|
@@ -4,7 +4,7 @@ export type TranslationMap = readonly string[];
|
|
|
4
4
|
export type Locale<T extends TranslationMap> = T[number];
|
|
5
5
|
export type LocaleKeys<T extends TranslationMap> = T[keyof T];
|
|
6
6
|
export type TMultilinguistResponse<T extends TranslationMap> = {
|
|
7
|
-
t<
|
|
7
|
+
t<K extends LocaleKey>(key: K, variables?: Record<string, string>): string;
|
|
8
8
|
setLocale: (locale: Locale<T>) => void;
|
|
9
9
|
initLocalization: () => Promise<void>;
|
|
10
10
|
locale: Ref<Locale<T>>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export default function useMultilinguist(): {
|
|
2
|
-
t<
|
|
2
|
+
t<K extends import("../types/generated-locales.js").LocaleKey>(key: K, variables?: Record<string, string>): string;
|
|
3
3
|
setLocale: (locale: string) => void;
|
|
4
4
|
initLocalization: () => Promise<void>;
|
|
5
5
|
locale: import("vue").Ref<string, string>;
|
package/dist/runtime/plugin.js
CHANGED
|
@@ -3,13 +3,16 @@ 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
|
|
7
8
|
};
|
|
8
9
|
const supportedLanguages = config.supportedLanguages;
|
|
9
10
|
const defaultLocale = config.defaultLocale;
|
|
10
11
|
const { initLocalization, ...localizationProperties } = useLocalization(supportedLanguages, defaultLocale);
|
|
11
12
|
await initLocalization();
|
|
12
|
-
|
|
13
|
+
if (nuxtApp.$config.public.multilinguist.logging) {
|
|
14
|
+
console.log("[multilinguist] is initialised");
|
|
15
|
+
}
|
|
13
16
|
nuxtApp.provide("localization", localizationProperties);
|
|
14
17
|
nuxtApp.provide("t", localizationProperties.t);
|
|
15
18
|
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.0",
|
|
4
|
+
"description": "Nuxt Multilinguist module for localizations",
|
|
5
5
|
"repository": "yiiameeMich/multilinguist",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
@@ -14,9 +14,24 @@
|
|
|
14
14
|
},
|
|
15
15
|
"types": "./dist/types.d.mts",
|
|
16
16
|
"files": [
|
|
17
|
-
"dist"
|
|
17
|
+
"dist",
|
|
18
|
+
"runtime/types"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"localization",
|
|
22
|
+
"nuxt localization",
|
|
23
|
+
"nuxt",
|
|
24
|
+
"internationalization",
|
|
25
|
+
"languages",
|
|
26
|
+
"locales",
|
|
27
|
+
"translation",
|
|
28
|
+
"localization nuxt",
|
|
29
|
+
"translation nuxt",
|
|
30
|
+
"nuxt translation",
|
|
31
|
+
"nuxt multilinguist",
|
|
32
|
+
"multilinguist nuxt",
|
|
33
|
+
"ssr localization"
|
|
18
34
|
],
|
|
19
|
-
"keywords": ["localization", "nuxt localization", "nuxt", "internationalization", "languages", "locales", "translation"],
|
|
20
35
|
"bugs": {
|
|
21
36
|
"email": "michaelkukh.dev@gmail.com"
|
|
22
37
|
},
|
|
@@ -36,7 +51,10 @@
|
|
|
36
51
|
"nuxt": "^3.0.0"
|
|
37
52
|
},
|
|
38
53
|
"dependencies": {
|
|
39
|
-
"@nuxt/kit": "^3.17.4"
|
|
54
|
+
"@nuxt/kit": "^3.17.4",
|
|
55
|
+
"eslint-config-prettier": "^10.1.5",
|
|
56
|
+
"eslint-plugin-prettier": "^5.4.1",
|
|
57
|
+
"prettier": "^3.5.3"
|
|
40
58
|
},
|
|
41
59
|
"devDependencies": {
|
|
42
60
|
"@nuxt/devtools": "^2.4.1",
|