@yiiamee/multilinguist 1.1.0 → 1.2.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 +43 -5
- package/dist/module.json +3 -0
- package/dist/module.mjs +16 -4
- package/dist/runtime/composables/useLocalization.d.ts +1 -1
- package/dist/runtime/composables/useLocalization.js +13 -3
- package/dist/runtime/composables/useMultilinguist.d.ts +1 -1
- package/dist/runtime/plugin.js +6 -6
- package/dist/runtime/types/generated-locales.d.ts +4 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Nuxt MultiLinguist module
|
|
2
2
|
|
|
3
|
-
Multilinguist is simple
|
|
3
|
+
Multilinguist is a simple but smoothly working module for easy and seamless localization implementation for Nuxt applications.
|
|
4
4
|
|
|
5
5
|
## Key Features
|
|
6
6
|
|
|
@@ -35,11 +35,45 @@ Then, create a "locales" directory in /public directory. This is necessary for m
|
|
|
35
35
|
|
|
36
36
|
Now, you're ready to use Multilinguist Module!
|
|
37
37
|
|
|
38
|
+
# Usage
|
|
39
|
+
|
|
38
40
|
### t()—famous translate function
|
|
39
41
|
|
|
40
42
|
```vue
|
|
41
43
|
<script setup lang="ts">
|
|
42
|
-
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the
|
|
44
|
+
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
|
|
45
|
+
|
|
46
|
+
const pageTitle = computed(() => {
|
|
47
|
+
return t("Hello, World");
|
|
48
|
+
});
|
|
49
|
+
</script>
|
|
50
|
+
|
|
51
|
+
<template>
|
|
52
|
+
<h3>{{ pageTitle }}</h3>
|
|
53
|
+
<button>{{ t("Switch Locale") }}</button>
|
|
54
|
+
</template>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
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:
|
|
59
|
+
|
|
60
|
+
```vue
|
|
61
|
+
<template>
|
|
62
|
+
<span>{{ t("Paste your variable here", { variable: locale }) }}</span>
|
|
63
|
+
</template>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
And your JSON must look like that:
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{
|
|
70
|
+
"Paste your variable here": "Here is your variable: {variable}"
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```vue
|
|
75
|
+
<script setup lang="ts">
|
|
76
|
+
const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
|
|
43
77
|
|
|
44
78
|
const pageTitle = computed(() => {
|
|
45
79
|
return t("Hello, World");
|
|
@@ -49,10 +83,14 @@ const pageTitle = computed(() => {
|
|
|
49
83
|
<template>
|
|
50
84
|
<h3>{{ pageTitle }}</h3>
|
|
51
85
|
<button>{{ t("Switch Locale") }}</button>
|
|
86
|
+
<br />
|
|
87
|
+
<span>{{ t("Paste your variable here", { variable: locale }) }}</span>
|
|
88
|
+
<br />
|
|
89
|
+
<h1>{{ t("nested.Nested key") }}: <span>{{ t("nested.Language") }}</span></h1>
|
|
52
90
|
</template>
|
|
53
91
|
```
|
|
54
92
|
|
|
55
|
-
You can already see how
|
|
93
|
+
You can already see how keys auto-completion works:
|
|
56
94
|
|
|
57
95
|

|
|
58
96
|
|
|
@@ -60,12 +98,12 @@ And validation:
|
|
|
60
98
|
|
|
61
99
|

|
|
62
100
|
|
|
63
|
-
### Set another value to current locale:
|
|
101
|
+
### Set another value to the current locale:
|
|
64
102
|
|
|
65
103
|
```vue
|
|
66
104
|
<script setup lang="ts">
|
|
67
105
|
const { t, setLocale } = useMultilinguist();
|
|
68
|
-
// setLocale function accepts string
|
|
106
|
+
// setLocale function accepts a string that should match one of defined
|
|
69
107
|
// in the nuxt.config strings from supportedLanguages array
|
|
70
108
|
|
|
71
109
|
const pageTitle = computed(() => {
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -2,15 +2,24 @@ import { defineNuxtModule, createResolver, addPlugin, addImportsDir, addTypeTemp
|
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
|
|
5
|
+
function flattenKeys(obj, prefix = "") {
|
|
6
|
+
return Object.entries(obj).flatMap(([key, value]) => {
|
|
7
|
+
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
8
|
+
if (typeof value === "object" && value !== null) {
|
|
9
|
+
return flattenKeys(value, fullKey);
|
|
10
|
+
}
|
|
11
|
+
return [fullKey];
|
|
12
|
+
});
|
|
13
|
+
}
|
|
5
14
|
function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath) {
|
|
6
15
|
async function generateTypes() {
|
|
7
16
|
const defaultLocalePath = path.join(localesPath, `${defaultLocaleFromConfig}.json`);
|
|
8
17
|
if (!fs.existsSync(defaultLocalePath)) {
|
|
9
|
-
console?.error(
|
|
18
|
+
console?.error(`\u274C Default locale file not found: ${defaultLocalePath}`);
|
|
10
19
|
return;
|
|
11
20
|
}
|
|
12
21
|
const json = JSON.parse(fs.readFileSync(defaultLocalePath, "utf-8"));
|
|
13
|
-
const keys =
|
|
22
|
+
const keys = flattenKeys(json);
|
|
14
23
|
const output = [
|
|
15
24
|
`// AUTO-GENERATED FILE \u2014 DO NOT EDIT MANUALLY`,
|
|
16
25
|
`export interface TranslationMessages {`,
|
|
@@ -21,7 +30,7 @@ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath)
|
|
|
21
30
|
].join("\n");
|
|
22
31
|
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
23
32
|
fs.writeFileSync(outPath, output, "utf-8");
|
|
24
|
-
console?.warn(
|
|
33
|
+
console?.warn(`\u2705 Generated types to ${outPath} from ${defaultLocalePath}`);
|
|
25
34
|
}
|
|
26
35
|
return {
|
|
27
36
|
name: "vite-plugin-generate-locales-types",
|
|
@@ -40,7 +49,10 @@ const module = defineNuxtModule({
|
|
|
40
49
|
meta: {
|
|
41
50
|
name: "@yiiamee/multilinguist",
|
|
42
51
|
configKey: "multilinguist",
|
|
43
|
-
version: "0.0.1"
|
|
52
|
+
version: "0.0.1",
|
|
53
|
+
compatibility: {
|
|
54
|
+
nuxt: "^3.14.0"
|
|
55
|
+
}
|
|
44
56
|
},
|
|
45
57
|
setup(moduleOptions, nuxtApp) {
|
|
46
58
|
const resolver = createResolver(import.meta.url);
|
|
@@ -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: (key: LocaleKey) => string;
|
|
7
|
+
t: (key: LocaleKey, variables?: Record<string, string>) => string;
|
|
8
8
|
setLocale: (locale: Locale<T>) => void;
|
|
9
9
|
initLocalization: () => Promise<void>;
|
|
10
10
|
locale: Ref<Locale<T>>;
|
|
@@ -23,9 +23,19 @@ export default function useLocalization(supportedLanguages, defaultLocale) {
|
|
|
23
23
|
const locale = useState(() => {
|
|
24
24
|
return userPrefferableLocale.value || defaultLocale;
|
|
25
25
|
});
|
|
26
|
-
const t = (key) => {
|
|
27
|
-
const
|
|
28
|
-
|
|
26
|
+
const t = (key, variables) => {
|
|
27
|
+
const messages = loadedLanguages.value[locale.value];
|
|
28
|
+
let translatedText = messages?.[key] ?? key;
|
|
29
|
+
if (key.includes(".")) {
|
|
30
|
+
const nestedKeyArray = key.split(".");
|
|
31
|
+
translatedText = nestedKeyArray.reduce((acc, key2) => acc?.[key2], messages);
|
|
32
|
+
}
|
|
33
|
+
if (variables && typeof translatedText === "string") {
|
|
34
|
+
translatedText = translatedText.replace(/{([^}]+)}/g, (_, varKey) => {
|
|
35
|
+
return variables[varKey] ?? `{${varKey}}`;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return String(translatedText);
|
|
29
39
|
};
|
|
30
40
|
const setLocale = async (newLocale) => {
|
|
31
41
|
if (!loadedLanguages.value[newLocale] && supportedLanguages.includes(newLocale)) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export default function useMultilinguist(): {
|
|
2
|
-
t: (key: import("../types/generated-locales.js").LocaleKey) => string;
|
|
2
|
+
t: (key: import("../types/generated-locales.js").LocaleKey, 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
|
@@ -7,10 +7,10 @@ export default defineNuxtPlugin(async (nuxtApp) => {
|
|
|
7
7
|
};
|
|
8
8
|
const supportedLanguages = config.supportedLanguages;
|
|
9
9
|
const defaultLocale = config.defaultLocale;
|
|
10
|
-
const
|
|
11
|
-
await
|
|
12
|
-
console.log("[multilinguist] is initialised"
|
|
13
|
-
nuxtApp.provide("localization",
|
|
14
|
-
nuxtApp.provide("t",
|
|
15
|
-
nuxtApp._localization =
|
|
10
|
+
const { initLocalization, ...localizationProperties } = useLocalization(supportedLanguages, defaultLocale);
|
|
11
|
+
await initLocalization();
|
|
12
|
+
console.log("[multilinguist] is initialised");
|
|
13
|
+
nuxtApp.provide("localization", localizationProperties);
|
|
14
|
+
nuxtApp.provide("t", localizationProperties.t);
|
|
15
|
+
nuxtApp._localization = localizationProperties;
|
|
16
16
|
});
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
export interface TranslationMessages {
|
|
3
3
|
"Hello, World": string;
|
|
4
4
|
"Switch Locale": string;
|
|
5
|
+
"Paste your variable here": string;
|
|
6
|
+
"nested.Nested key": string;
|
|
7
|
+
"nested.Language": string;
|
|
8
|
+
"nested.nested second level.nested second level language": string;
|
|
5
9
|
}
|
|
6
10
|
|
|
7
11
|
export type LocaleKey = keyof TranslationMessages;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yiiamee/multilinguist",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Nuxt 3 Multilinguist Localization module",
|
|
5
5
|
"repository": "yiiameeMich/multilinguist",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
"files": [
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
|
+
"keywords": ["localization", "nuxt localization", "nuxt", "internationalization", "languages", "locales", "translation"],
|
|
20
|
+
"bugs": {
|
|
21
|
+
"email": "michaelkukh.dev@gmail.com"
|
|
22
|
+
},
|
|
19
23
|
"scripts": {
|
|
20
24
|
"prepack": "nuxt-module-build build",
|
|
21
25
|
"dev": "nuxi dev playground",
|