@yiiamee/multilinguist 1.5.2 → 1.6.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 CHANGED
@@ -31,7 +31,7 @@ export default defineNuxtConfig({
31
31
  })
32
32
  ```
33
33
 
34
- Then, create a "locales" directory in root directory of your project.
34
+ Then, create a "locales" directory in /public directory. This is necessary for module to access your languages.
35
35
 
36
36
  ![directory_structure.png](directory_structure.png)
37
37
 
@@ -82,24 +82,6 @@ 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.
103
85
 
104
86
  # Usage
105
87
 
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@yiiamee/multilinguist",
3
3
  "configKey": "multilinguist",
4
- "version": "1.5.2",
4
+ "version": "1.6.0",
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, { resolve } from 'path';
3
+ import path from 'path';
4
4
 
5
5
  function flattenKeys(obj, prefix = "") {
6
6
  return Object.entries(obj).flatMap(([key, value]) => {
@@ -49,9 +49,10 @@ declare module 'vue' {
49
49
  }
50
50
  `;
51
51
  };
52
- function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, absoluteLocalesPath, outPath, logging = true) {
52
+ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath, logging = true) {
53
53
  async function generateTypes() {
54
- const defaultLocalePath = path.join(absoluteLocalesPath, `${defaultLocaleFromConfig}.json`);
54
+ const defaultLocalePath = path.join(localesPath, `${defaultLocaleFromConfig}.json`);
55
+ console.log(defaultLocalePath);
55
56
  if (!fs.existsSync(defaultLocalePath)) {
56
57
  console?.error(`\u274C Default locale file not found: ${defaultLocalePath}`);
57
58
  return;
@@ -71,7 +72,7 @@ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, absoluteLocalesPath,
71
72
  await generateTypes();
72
73
  },
73
74
  async handleHotUpdate(ctx) {
74
- if (ctx.file.endsWith(".json") && ctx.file.includes(absoluteLocalesPath)) {
75
+ if (ctx.file.endsWith(".json") && ctx.file.includes(localesPath)) {
75
76
  await generateTypes();
76
77
  }
77
78
  }
@@ -82,7 +83,7 @@ const module = defineNuxtModule({
82
83
  meta: {
83
84
  name: "@yiiamee/multilinguist",
84
85
  configKey: "multilinguist",
85
- version: "1.5.2",
86
+ version: "1.6.0",
86
87
  compatibility: {
87
88
  nuxt: "^3.0.0 || ^4.0.0"
88
89
  },
@@ -90,32 +91,25 @@ const module = defineNuxtModule({
90
91
  logging: true,
91
92
  defaultLocale: "",
92
93
  supportedLanguages: [],
93
- setBrowserLanguage: true,
94
- localesPath: "./locales"
94
+ setBrowserLanguage: true
95
95
  }
96
96
  },
97
97
  setup(moduleOptions, nuxtApp) {
98
98
  const resolver = createResolver(import.meta.url);
99
- const localesPath = moduleOptions.localesPath || "./locales";
100
- const resolvedLocalesPath = resolve(nuxtApp.options.rootDir, localesPath);
101
99
  addPlugin(resolver.resolve("runtime/plugin"));
102
100
  addImportsDir(resolver.resolve("runtime/composables"));
103
101
  nuxtApp.options.runtimeConfig.public.multilinguist = {
104
102
  defaultLocale: moduleOptions.defaultLocale,
105
103
  supportedLanguages: moduleOptions.supportedLanguages,
106
104
  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
105
+ setBrowserLanguage: typeof moduleOptions.setBrowserLanguage === "boolean" ? moduleOptions.setBrowserLanguage : true
112
106
  };
113
107
  nuxtApp.hook("vite:extendConfig", (viteConfig) => {
114
108
  viteConfig.plugins = viteConfig.plugins || [];
115
109
  viteConfig.plugins.push(
116
110
  GenerateLocaleKeysPlugin(
117
111
  moduleOptions.defaultLocale,
118
- resolvedLocalesPath,
112
+ `${nuxtApp.options.rootDir}/public/locales`,
119
113
  resolver.resolve("./runtime/types/generated-locales.d.ts"),
120
114
  moduleOptions.logging
121
115
  )
@@ -126,17 +120,6 @@ const module = defineNuxtModule({
126
120
  path: resolver.resolve("./runtime/types/generated-locales.d.ts")
127
121
  });
128
122
  });
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
123
  }
141
124
  });
142
125
 
@@ -1,38 +1,26 @@
1
1
  import useLocale from "../composables/useLocale.js";
2
- import { useCookie, useState, useRuntimeConfig } from "nuxt/app";
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
- 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);
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
- const url = `/${normalizedPath}/${locale2}.json`;
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
  };
@@ -1,30 +1,8 @@
1
1
  export declare const localeFiles: {
2
2
  "/locales/en.json": {
3
- default: {
4
- "Hello, World": string;
5
- "Switch Locale": string;
6
- "Paste your variable here": string;
7
- nested: {
8
- "Nested key": string;
9
- Language: string;
10
- "nested second level": {
11
- "nested second level language": string;
12
- };
13
- };
14
- };
3
+ default: any;
15
4
  };
16
5
  "/locales/es.json": {
17
- default: {
18
- "Hello, World": string;
19
- "Switch Locale": string;
20
- "Paste your variable here": string;
21
- nested: {
22
- "Nested key": string;
23
- Language: string;
24
- "nested second level": {
25
- "nested second level language": string;
26
- };
27
- };
28
- };
6
+ default: any;
29
7
  };
30
8
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yiiamee/multilinguist",
3
- "version": "1.5.2",
3
+ "version": "1.6.0",
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.1",
68
+ "nuxt": "^4.0.3",
69
69
  "typescript": "~5.8.3",
70
70
  "vitest": "^3.1.4",
71
71
  "vue-tsc": "^2.2.10"