@yiiamee/multilinguist 1.5.0 → 1.5.2
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 +19 -1
- package/dist/module.json +2 -2
- package/dist/module.mjs +19 -55
- package/dist/runtime/composables/useLocalization.js +23 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,7 +31,7 @@ export default defineNuxtConfig({
|
|
|
31
31
|
})
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
Then, create a "locales" directory in
|
|
34
|
+
Then, create a "locales" directory in root directory of your project.
|
|
35
35
|
|
|
36
36
|

|
|
37
37
|
|
|
@@ -82,6 +82,24 @@ export default defineNuxtConfig({
|
|
|
82
82
|
})
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
Also, if you want to use a different folder name for your locales' files, or you want to specify other path to the directory, you can set the `localesPath` option in your nuxt.config:
|
|
86
|
+
|
|
87
|
+
```nuxt.config.ts
|
|
88
|
+
export default defineNuxtConfig({
|
|
89
|
+
modules: [
|
|
90
|
+
"@yiiamee/multilinguist",
|
|
91
|
+
],
|
|
92
|
+
multilinguist: {
|
|
93
|
+
defaultLocale: "en",
|
|
94
|
+
supportedLanguages: ["en", "es"],
|
|
95
|
+
logging: false,
|
|
96
|
+
setBrowserLanguage: false, // by default: true
|
|
97
|
+
localesPath: "./languages", // by default: "./locales"
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Important! Keep in mind, that it is not recommended to use "public" folder for your locales' files, as it may lead to rendering, security and other issues.
|
|
85
103
|
|
|
86
104
|
# Usage
|
|
87
105
|
|
package/dist/module.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yiiamee/multilinguist",
|
|
3
3
|
"configKey": "multilinguist",
|
|
4
|
-
"version": "1.5.
|
|
4
|
+
"version": "1.5.2",
|
|
5
5
|
"compatibility": {
|
|
6
6
|
"nuxt": "^3.0.0 || ^4.0.0"
|
|
7
7
|
},
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"defaultLocale": "",
|
|
11
11
|
"supportedLanguages": [],
|
|
12
12
|
"setBrowserLanguage": true,
|
|
13
|
-
"localesPath": "./
|
|
13
|
+
"localesPath": "./locales"
|
|
14
14
|
},
|
|
15
15
|
"builder": {
|
|
16
16
|
"@nuxt/module-builder": "1.0.1",
|
package/dist/module.mjs
CHANGED
|
@@ -49,24 +49,7 @@ declare module 'vue' {
|
|
|
49
49
|
}
|
|
50
50
|
`;
|
|
51
51
|
};
|
|
52
|
-
|
|
53
|
-
const generatedFileDir = path.dirname(generatedFilePath);
|
|
54
|
-
const relativeTo = path.relative(generatedFileDir, projectRoot);
|
|
55
|
-
const cleanLocalesPath = relativeLocalesPath.startsWith("./") ? relativeLocalesPath.slice(2) : relativeLocalesPath.startsWith("/") ? relativeLocalesPath.slice(1) : relativeLocalesPath;
|
|
56
|
-
const importBasePath = path.join(relativeTo, cleanLocalesPath).replace(/\\/g, "/");
|
|
57
|
-
const normalizedKeyPath = relativeLocalesPath.startsWith("./") ? relativeLocalesPath.slice(1) : relativeLocalesPath.startsWith("/") ? relativeLocalesPath : `/${relativeLocalesPath}`;
|
|
58
|
-
const imports = supportedLanguages.map((lang, index) => `import locale_${index} from "${importBasePath}/${lang}.json";`).join("\n");
|
|
59
|
-
const localeMap = supportedLanguages.map((lang, index) => ` "${normalizedKeyPath}/${lang}.json": { default: locale_${index} }`).join(",\n");
|
|
60
|
-
return `
|
|
61
|
-
// AUTO-GENERATED LOCALE IMPORTS
|
|
62
|
-
${imports}
|
|
63
|
-
|
|
64
|
-
export const localeFiles = {
|
|
65
|
-
${localeMap}
|
|
66
|
-
};
|
|
67
|
-
`;
|
|
68
|
-
};
|
|
69
|
-
function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, absoluteLocalesPath, outPath, logging = true, relativeLocalesPath = "./public/locales", supportedLanguages = [], projectRoot = process.cwd()) {
|
|
52
|
+
function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, absoluteLocalesPath, outPath, logging = true) {
|
|
70
53
|
async function generateTypes() {
|
|
71
54
|
const defaultLocalePath = path.join(absoluteLocalesPath, `${defaultLocaleFromConfig}.json`);
|
|
72
55
|
if (!fs.existsSync(defaultLocalePath)) {
|
|
@@ -82,40 +65,14 @@ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, absoluteLocalesPath,
|
|
|
82
65
|
console?.warn(`\u2705 Generated types to ${outPath} from ${defaultLocalePath}`);
|
|
83
66
|
}
|
|
84
67
|
}
|
|
85
|
-
async function generateLocaleImportsFile() {
|
|
86
|
-
const importsPath = path.dirname(outPath) + "/locale-imports.ts";
|
|
87
|
-
const imports = generateLocaleImports(
|
|
88
|
-
absoluteLocalesPath,
|
|
89
|
-
relativeLocalesPath,
|
|
90
|
-
supportedLanguages,
|
|
91
|
-
importsPath,
|
|
92
|
-
projectRoot
|
|
93
|
-
);
|
|
94
|
-
fs.mkdirSync(path.dirname(importsPath), { recursive: true });
|
|
95
|
-
fs.writeFileSync(importsPath, imports, "utf-8");
|
|
96
|
-
if (logging) {
|
|
97
|
-
console?.warn(`\u2705 Generated locale imports to ${importsPath}`);
|
|
98
|
-
const generatedFileDir = path.dirname(importsPath);
|
|
99
|
-
const relativeTo = path.relative(generatedFileDir, projectRoot);
|
|
100
|
-
const cleanLocalesPath = relativeLocalesPath.startsWith("./") ? relativeLocalesPath.slice(2) : relativeLocalesPath.startsWith("/") ? relativeLocalesPath.slice(1) : relativeLocalesPath;
|
|
101
|
-
const finalPath = path.join(relativeTo, cleanLocalesPath).replace(/\\/g, "/");
|
|
102
|
-
console?.warn(`\u2705 Import path calculated: ${finalPath}`);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
68
|
return {
|
|
106
69
|
name: "vite-plugin-generate-locales-types",
|
|
107
70
|
async buildStart() {
|
|
108
71
|
await generateTypes();
|
|
109
|
-
if (supportedLanguages.length > 0) {
|
|
110
|
-
await generateLocaleImportsFile();
|
|
111
|
-
}
|
|
112
72
|
},
|
|
113
73
|
async handleHotUpdate(ctx) {
|
|
114
74
|
if (ctx.file.endsWith(".json") && ctx.file.includes(absoluteLocalesPath)) {
|
|
115
75
|
await generateTypes();
|
|
116
|
-
if (supportedLanguages.length > 0) {
|
|
117
|
-
await generateLocaleImportsFile();
|
|
118
|
-
}
|
|
119
76
|
}
|
|
120
77
|
}
|
|
121
78
|
};
|
|
@@ -125,7 +82,7 @@ const module = defineNuxtModule({
|
|
|
125
82
|
meta: {
|
|
126
83
|
name: "@yiiamee/multilinguist",
|
|
127
84
|
configKey: "multilinguist",
|
|
128
|
-
version: "1.5.
|
|
85
|
+
version: "1.5.2",
|
|
129
86
|
compatibility: {
|
|
130
87
|
nuxt: "^3.0.0 || ^4.0.0"
|
|
131
88
|
},
|
|
@@ -134,12 +91,12 @@ const module = defineNuxtModule({
|
|
|
134
91
|
defaultLocale: "",
|
|
135
92
|
supportedLanguages: [],
|
|
136
93
|
setBrowserLanguage: true,
|
|
137
|
-
localesPath: "./
|
|
94
|
+
localesPath: "./locales"
|
|
138
95
|
}
|
|
139
96
|
},
|
|
140
97
|
setup(moduleOptions, nuxtApp) {
|
|
141
98
|
const resolver = createResolver(import.meta.url);
|
|
142
|
-
const localesPath = moduleOptions.localesPath || "./
|
|
99
|
+
const localesPath = moduleOptions.localesPath || "./locales";
|
|
143
100
|
const resolvedLocalesPath = resolve(nuxtApp.options.rootDir, localesPath);
|
|
144
101
|
addPlugin(resolver.resolve("runtime/plugin"));
|
|
145
102
|
addImportsDir(resolver.resolve("runtime/composables"));
|
|
@@ -150,6 +107,9 @@ const module = defineNuxtModule({
|
|
|
150
107
|
setBrowserLanguage: typeof moduleOptions.setBrowserLanguage === "boolean" ? moduleOptions.setBrowserLanguage : true,
|
|
151
108
|
localesPath
|
|
152
109
|
};
|
|
110
|
+
nuxtApp.options.runtimeConfig.multilinguist = {
|
|
111
|
+
resolvedLocalesPath
|
|
112
|
+
};
|
|
153
113
|
nuxtApp.hook("vite:extendConfig", (viteConfig) => {
|
|
154
114
|
viteConfig.plugins = viteConfig.plugins || [];
|
|
155
115
|
viteConfig.plugins.push(
|
|
@@ -157,11 +117,7 @@ const module = defineNuxtModule({
|
|
|
157
117
|
moduleOptions.defaultLocale,
|
|
158
118
|
resolvedLocalesPath,
|
|
159
119
|
resolver.resolve("./runtime/types/generated-locales.d.ts"),
|
|
160
|
-
moduleOptions.logging
|
|
161
|
-
localesPath,
|
|
162
|
-
moduleOptions.supportedLanguages,
|
|
163
|
-
nuxtApp.options.rootDir
|
|
164
|
-
// Pass the project root
|
|
120
|
+
moduleOptions.logging
|
|
165
121
|
)
|
|
166
122
|
);
|
|
167
123
|
});
|
|
@@ -169,10 +125,18 @@ const module = defineNuxtModule({
|
|
|
169
125
|
references.push({
|
|
170
126
|
path: resolver.resolve("./runtime/types/generated-locales.d.ts")
|
|
171
127
|
});
|
|
172
|
-
references.push({
|
|
173
|
-
path: resolver.resolve("./runtime/types/locale-imports.ts")
|
|
174
|
-
});
|
|
175
128
|
});
|
|
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
|
+
}
|
|
176
140
|
}
|
|
177
141
|
});
|
|
178
142
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import useLocale from "../composables/useLocale.js";
|
|
2
2
|
import { useCookie, useState, useRuntimeConfig } from "nuxt/app";
|
|
3
3
|
import { computed, watch } from "vue";
|
|
4
|
-
import { localeFiles } from "../types/locale-imports.js";
|
|
5
4
|
export default function useLocalization(supportedLanguages, defaultLocale, setBrowserLanguage = true) {
|
|
6
5
|
const { locale: userBrowserLocale } = useLocale(supportedLanguages, defaultLocale);
|
|
7
6
|
const config = useRuntimeConfig();
|
|
@@ -11,15 +10,29 @@ export default function useLocalization(supportedLanguages, defaultLocale, setBr
|
|
|
11
10
|
const loadedLanguages = useState("loaded-languages", () => ({}));
|
|
12
11
|
const loadLocaleMessages = async (locale2) => {
|
|
13
12
|
if (!loadedLanguages.value[locale2]) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
try {
|
|
14
|
+
const localesPath = config.public.multilinguist.localesPath || "./locales";
|
|
15
|
+
const normalizedPath = localesPath.startsWith("./") ? localesPath.slice(2) : localesPath.startsWith("/") ? localesPath.slice(1) : localesPath;
|
|
16
|
+
let messages;
|
|
17
|
+
if (import.meta.server) {
|
|
18
|
+
const { readFileSync, existsSync } = await import("fs");
|
|
19
|
+
const { join } = await import("path");
|
|
20
|
+
const resolvedPath = config.multilinguist?.resolvedLocalesPath;
|
|
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);
|
|
27
|
+
} else {
|
|
28
|
+
const url = `/${normalizedPath}/${locale2}.json`;
|
|
29
|
+
const response = await $fetch(url);
|
|
30
|
+
messages = response;
|
|
31
|
+
}
|
|
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`);
|
|
23
36
|
}
|
|
24
37
|
}
|
|
25
38
|
};
|