@yiiamee/multilinguist 1.4.2 → 1.5.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.2",
4
+ "version": "1.5.1",
5
5
  "compatibility": {
6
6
  "nuxt": "^3.0.0 || ^4.0.0"
7
7
  },
@@ -9,7 +9,8 @@
9
9
  "logging": true,
10
10
  "defaultLocale": "",
11
11
  "supportedLanguages": [],
12
- "setBrowserLanguage": true
12
+ "setBrowserLanguage": true,
13
+ "localesPath": "./public/locales"
13
14
  },
14
15
  "builder": {
15
16
  "@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 from 'path';
3
+ import path, { resolve } from 'path';
4
4
 
5
5
  function flattenKeys(obj, prefix = "") {
6
6
  return Object.entries(obj).flatMap(([key, value]) => {
@@ -49,9 +49,9 @@ declare module 'vue' {
49
49
  }
50
50
  `;
51
51
  };
52
- function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath, logging = true) {
52
+ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, absoluteLocalesPath, outPath, logging = true) {
53
53
  async function generateTypes() {
54
- const defaultLocalePath = path.join(localesPath, `${defaultLocaleFromConfig}.json`);
54
+ const defaultLocalePath = path.join(absoluteLocalesPath, `${defaultLocaleFromConfig}.json`);
55
55
  if (!fs.existsSync(defaultLocalePath)) {
56
56
  console?.error(`\u274C Default locale file not found: ${defaultLocalePath}`);
57
57
  return;
@@ -71,7 +71,7 @@ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath,
71
71
  await generateTypes();
72
72
  },
73
73
  async handleHotUpdate(ctx) {
74
- if (ctx.file.endsWith(".json") && ctx.file.includes(localesPath)) {
74
+ if (ctx.file.endsWith(".json") && ctx.file.includes(absoluteLocalesPath)) {
75
75
  await generateTypes();
76
76
  }
77
77
  }
@@ -82,7 +82,7 @@ const module = defineNuxtModule({
82
82
  meta: {
83
83
  name: "@yiiamee/multilinguist",
84
84
  configKey: "multilinguist",
85
- version: "1.4.2",
85
+ version: "1.5.1",
86
86
  compatibility: {
87
87
  nuxt: "^3.0.0 || ^4.0.0"
88
88
  },
@@ -90,25 +90,32 @@ const module = defineNuxtModule({
90
90
  logging: true,
91
91
  defaultLocale: "",
92
92
  supportedLanguages: [],
93
- setBrowserLanguage: true
93
+ setBrowserLanguage: true,
94
+ localesPath: "./public/locales"
94
95
  }
95
96
  },
96
97
  setup(moduleOptions, nuxtApp) {
97
98
  const resolver = createResolver(import.meta.url);
99
+ const localesPath = moduleOptions.localesPath || "./public/locales";
100
+ const resolvedLocalesPath = resolve(nuxtApp.options.rootDir, localesPath);
98
101
  addPlugin(resolver.resolve("runtime/plugin"));
99
102
  addImportsDir(resolver.resolve("runtime/composables"));
100
103
  nuxtApp.options.runtimeConfig.public.multilinguist = {
101
104
  defaultLocale: moduleOptions.defaultLocale,
102
105
  supportedLanguages: moduleOptions.supportedLanguages,
103
106
  logging: typeof moduleOptions.logging === "boolean" ? moduleOptions.logging : true,
104
- setBrowserLanguage: typeof moduleOptions.setBrowserLanguage === "boolean" ? moduleOptions.setBrowserLanguage : true
107
+ setBrowserLanguage: typeof moduleOptions.setBrowserLanguage === "boolean" ? moduleOptions.setBrowserLanguage : true,
108
+ localesPath
109
+ };
110
+ nuxtApp.options.runtimeConfig.multilinguist = {
111
+ resolvedLocalesPath
105
112
  };
106
113
  nuxtApp.hook("vite:extendConfig", (viteConfig) => {
107
114
  viteConfig.plugins = viteConfig.plugins || [];
108
115
  viteConfig.plugins.push(
109
116
  GenerateLocaleKeysPlugin(
110
117
  moduleOptions.defaultLocale,
111
- `${nuxtApp.options.rootDir}/public/locales`,
118
+ resolvedLocalesPath,
112
119
  resolver.resolve("./runtime/types/generated-locales.d.ts"),
113
120
  moduleOptions.logging
114
121
  )
@@ -119,6 +126,17 @@ const module = defineNuxtModule({
119
126
  path: resolver.resolve("./runtime/types/generated-locales.d.ts")
120
127
  });
121
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
+ }
122
140
  }
123
141
  });
124
142
 
@@ -1,21 +1,38 @@
1
1
  import useLocale from "../composables/useLocale.js";
2
- import { useCookie, useState } from "nuxt/app";
2
+ import { useCookie, useState, useRuntimeConfig } 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();
6
7
  const userSelectedLocale = useCookie("multilinguist-locale", {
7
8
  default: () => supportedLanguages.includes(userBrowserLocale.value) && setBrowserLanguage ? userBrowserLocale.value : defaultLocale
8
9
  });
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
- const fileKey = `/public/locales/${locale2}.json`;
14
- const messages = localeFiles[fileKey];
15
- if (messages) {
16
- loadedLanguages.value[locale2] = messages?.default;
17
- } else {
18
- throw new Error(`Locale file ${fileKey} not found`);
13
+ try {
14
+ const localesPath = config.public.multilinguist.localesPath || "./public/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`);
19
36
  }
20
37
  }
21
38
  };
@@ -0,0 +1,30 @@
1
+ export declare const localeFiles: {
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
+ };
15
+ };
16
+ "/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
+ };
29
+ };
30
+ };
@@ -0,0 +1,6 @@
1
+ import locale_0 from "../../../playground/locales/en.json";
2
+ import locale_1 from "../../../playground/locales/es.json";
3
+ export const localeFiles = {
4
+ "/locales/en.json": { default: locale_0 },
5
+ "/locales/es.json": { default: locale_1 }
6
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yiiamee/multilinguist",
3
- "version": "1.4.2",
3
+ "version": "1.5.1",
4
4
  "description": "Nuxt Multilinguist module for localizations",
5
5
  "repository": "yiiameeMich/multilinguist",
6
6
  "license": "MIT",