@yiiamee/multilinguist 1.5.2 → 1.6.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/dist/module.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yiiamee/multilinguist",
|
|
3
3
|
"configKey": "multilinguist",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.6.1",
|
|
5
5
|
"compatibility": {
|
|
6
6
|
"nuxt": "^3.0.0 || ^4.0.0"
|
|
7
7
|
},
|
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
"logging": true,
|
|
10
10
|
"defaultLocale": "",
|
|
11
11
|
"supportedLanguages": [],
|
|
12
|
-
"setBrowserLanguage": true
|
|
13
|
-
"localesPath": "./locales"
|
|
12
|
+
"setBrowserLanguage": true
|
|
14
13
|
},
|
|
15
14
|
"builder": {
|
|
16
15
|
"@nuxt/module-builder": "1.0.1",
|
package/dist/module.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineNuxtModule, createResolver, addPlugin, addImportsDir } from '@nuxt/kit';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
-
import path
|
|
3
|
+
import path from 'path';
|
|
4
4
|
|
|
5
5
|
function flattenKeys(obj, prefix = "") {
|
|
6
6
|
return Object.entries(obj).flatMap(([key, value]) => {
|
|
@@ -82,7 +82,7 @@ const module = defineNuxtModule({
|
|
|
82
82
|
meta: {
|
|
83
83
|
name: "@yiiamee/multilinguist",
|
|
84
84
|
configKey: "multilinguist",
|
|
85
|
-
version: "1.
|
|
85
|
+
version: "1.6.1",
|
|
86
86
|
compatibility: {
|
|
87
87
|
nuxt: "^3.0.0 || ^4.0.0"
|
|
88
88
|
},
|
|
@@ -90,32 +90,25 @@ const module = defineNuxtModule({
|
|
|
90
90
|
logging: true,
|
|
91
91
|
defaultLocale: "",
|
|
92
92
|
supportedLanguages: [],
|
|
93
|
-
setBrowserLanguage: true
|
|
94
|
-
localesPath: "./locales"
|
|
93
|
+
setBrowserLanguage: true
|
|
95
94
|
}
|
|
96
95
|
},
|
|
97
96
|
setup(moduleOptions, nuxtApp) {
|
|
98
97
|
const resolver = createResolver(import.meta.url);
|
|
99
|
-
const localesPath = moduleOptions.localesPath || "./locales";
|
|
100
|
-
const resolvedLocalesPath = resolve(nuxtApp.options.rootDir, localesPath);
|
|
101
98
|
addPlugin(resolver.resolve("runtime/plugin"));
|
|
102
99
|
addImportsDir(resolver.resolve("runtime/composables"));
|
|
103
100
|
nuxtApp.options.runtimeConfig.public.multilinguist = {
|
|
104
101
|
defaultLocale: moduleOptions.defaultLocale,
|
|
105
102
|
supportedLanguages: moduleOptions.supportedLanguages,
|
|
106
103
|
logging: typeof moduleOptions.logging === "boolean" ? moduleOptions.logging : true,
|
|
107
|
-
setBrowserLanguage: typeof moduleOptions.setBrowserLanguage === "boolean" ? moduleOptions.setBrowserLanguage : true
|
|
108
|
-
localesPath
|
|
109
|
-
};
|
|
110
|
-
nuxtApp.options.runtimeConfig.multilinguist = {
|
|
111
|
-
resolvedLocalesPath
|
|
104
|
+
setBrowserLanguage: typeof moduleOptions.setBrowserLanguage === "boolean" ? moduleOptions.setBrowserLanguage : true
|
|
112
105
|
};
|
|
113
106
|
nuxtApp.hook("vite:extendConfig", (viteConfig) => {
|
|
114
107
|
viteConfig.plugins = viteConfig.plugins || [];
|
|
115
108
|
viteConfig.plugins.push(
|
|
116
109
|
GenerateLocaleKeysPlugin(
|
|
117
110
|
moduleOptions.defaultLocale,
|
|
118
|
-
|
|
111
|
+
`${nuxtApp.options.rootDir}/public/locales`,
|
|
119
112
|
resolver.resolve("./runtime/types/generated-locales.d.ts"),
|
|
120
113
|
moduleOptions.logging
|
|
121
114
|
)
|
|
@@ -126,17 +119,6 @@ const module = defineNuxtModule({
|
|
|
126
119
|
path: resolver.resolve("./runtime/types/generated-locales.d.ts")
|
|
127
120
|
});
|
|
128
121
|
});
|
|
129
|
-
if (!localesPath.includes("public")) {
|
|
130
|
-
nuxtApp.hook("nitro:config", async (nitroConfig) => {
|
|
131
|
-
nitroConfig.publicAssets = nitroConfig.publicAssets || [];
|
|
132
|
-
nitroConfig.publicAssets.push({
|
|
133
|
-
dir: resolvedLocalesPath,
|
|
134
|
-
maxAge: 60 * 60 * 24 * 7,
|
|
135
|
-
// 1 week
|
|
136
|
-
baseURL: `/${localesPath.replace(/^\.\//, "").replace(/^public\//, "")}`
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
122
|
}
|
|
141
123
|
});
|
|
142
124
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ComputedRef, type Ref } 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];
|
|
@@ -1,45 +1,33 @@
|
|
|
1
1
|
import useLocale from "../composables/useLocale.js";
|
|
2
|
-
import { useCookie, useState
|
|
2
|
+
import { useCookie, useState } from "nuxt/app";
|
|
3
3
|
import { computed, watch } from "vue";
|
|
4
4
|
export default function useLocalization(supportedLanguages, defaultLocale, setBrowserLanguage = true) {
|
|
5
5
|
const { locale: userBrowserLocale } = useLocale(supportedLanguages, defaultLocale);
|
|
6
|
-
const config = useRuntimeConfig();
|
|
7
6
|
const userSelectedLocale = useCookie("multilinguist-locale", {
|
|
8
7
|
default: () => supportedLanguages.includes(userBrowserLocale.value) && setBrowserLanguage ? userBrowserLocale.value : defaultLocale
|
|
9
8
|
});
|
|
9
|
+
const localeFiles = import.meta.glob("@@/public/locales/*.json", { eager: true });
|
|
10
10
|
const loadedLanguages = useState("loaded-languages", () => ({}));
|
|
11
11
|
const loadLocaleMessages = async (locale2) => {
|
|
12
12
|
if (!loadedLanguages.value[locale2]) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const filePath = join(resolvedPath, `${locale2}.json`);
|
|
22
|
-
if (!existsSync(filePath)) {
|
|
23
|
-
throw new Error(`Locale file not found: ${filePath}`);
|
|
24
|
-
}
|
|
25
|
-
const fileContent = readFileSync(filePath, "utf-8");
|
|
26
|
-
messages = JSON.parse(fileContent);
|
|
13
|
+
const fileKeyVersion4 = `../public/locales/${locale2}.json`;
|
|
14
|
+
const fileKeyVersion3 = `/public/locales/${locale2}.json`;
|
|
15
|
+
const messages = localeFiles[fileKeyVersion4] || localeFiles[fileKeyVersion3];
|
|
16
|
+
if (messages) {
|
|
17
|
+
loadedLanguages.value[locale2] = messages?.default;
|
|
18
|
+
} else {
|
|
19
|
+
if (!localeFiles[fileKeyVersion4]) {
|
|
20
|
+
throw new Error(`Locale file ${localeFiles[fileKeyVersion4]} not found`);
|
|
27
21
|
} else {
|
|
28
|
-
|
|
29
|
-
const response = await $fetch(url);
|
|
30
|
-
messages = response;
|
|
22
|
+
throw new Error(`Locale file ${localeFiles[fileKeyVersion3]} not found`);
|
|
31
23
|
}
|
|
32
|
-
loadedLanguages.value[locale2] = messages;
|
|
33
|
-
} catch (error) {
|
|
34
|
-
console.error(`Failed to load locale '${locale2}':`, error);
|
|
35
|
-
throw new Error(`Locale file for '${locale2}' not found at configured path`);
|
|
36
24
|
}
|
|
37
25
|
}
|
|
38
26
|
};
|
|
39
27
|
const userPrefferableLocale = computed(() => {
|
|
40
28
|
return userSelectedLocale.value ? userSelectedLocale.value : userBrowserLocale.value;
|
|
41
29
|
});
|
|
42
|
-
const locale = useState(() => {
|
|
30
|
+
const locale = useState("current-locale-multilinguist", () => {
|
|
43
31
|
return userPrefferableLocale.value || defaultLocale;
|
|
44
32
|
});
|
|
45
33
|
const t = (key, variables) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yiiamee/multilinguist",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "Nuxt Multilinguist module for localizations",
|
|
5
5
|
"repository": "yiiameeMich/multilinguist",
|
|
6
6
|
"license": "MIT",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"@types/node": "latest",
|
|
66
66
|
"changelogen": "^0.6.1",
|
|
67
67
|
"eslint": "^9.27.0",
|
|
68
|
-
"nuxt": "^4.0.
|
|
68
|
+
"nuxt": "^4.0.3",
|
|
69
69
|
"typescript": "~5.8.3",
|
|
70
70
|
"vitest": "^3.1.4",
|
|
71
71
|
"vue-tsc": "^2.2.10"
|