@yiiamee/multilinguist 1.2.8 → 1.3.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
@@ -1,6 +1,7 @@
1
- # Nuxt MultiLinguist module
1
+ # Nuxt Multilinguist module
2
2
 
3
- Multilinguist is a simple but smoothly working module for easy and seamless localization implementation for Nuxt applications.
3
+ Multilinguist is a simple but smoothly working module for easy and seamless localization implementation for Nuxt
4
+ applications.
4
5
 
5
6
  ## Key Features
6
7
 
@@ -20,7 +21,7 @@ Then, add the module to your nuxt.config
20
21
  ```nuxt.config.ts
21
22
  export default defineNuxtConfig({
22
23
  modules: [
23
- @yiiamee/multilinguist,
24
+ "@yiiamee/multilinguist",
24
25
  ],
25
26
  multilinguist: {
26
27
  defaultLocale: "en", // string representing key to your default (fallback) locale
@@ -35,18 +36,47 @@ Then, create a "locales" directory in /public directory. This is necessary for m
35
36
 
36
37
  Now, you're ready to use Multilinguist Module!
37
38
 
39
+ After running your project, you can see the following warning:
40
+
41
+ ![vite-warning.png](vite-warning.png)
42
+
43
+ **Dont be scared**, this is just a message from the module, indicating that your locales are typed, and that the module
44
+ can properly perform type-checks and autocompletion.
45
+
46
+ Also, on both SSR and CSR, you will see the following message:
47
+
48
+ ![initialized.png](initialized.png)
49
+
50
+ It indicated that the module has been initialized successfully.
51
+
52
+ To disable logs, you can set the logging option in your nuxt.config to false (by default, it is true):
53
+
54
+ ```nuxt.config.ts
55
+ export default defineNuxtConfig({
56
+ modules: [
57
+ "@yiiamee/multilinguist",
58
+ ],
59
+ multilinguist: {
60
+ defaultLocale: "en",
61
+ supportedLanguages: ["en", "es"],
62
+ logging: false, // Boolean value to define if the module should send internal logs to console
63
+ },
64
+ })
65
+ ```
66
+
38
67
  # Usage
39
68
 
40
- ### t()—famous translate function
41
-
69
+ ### t()—famous translate function
70
+
42
71
  ```vue
72
+
43
73
  <script setup lang="ts">
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>
74
+ const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
75
+
76
+ const pageTitle = computed(() => {
77
+ return t("Hello, World");
78
+ });
79
+ </script>
50
80
 
51
81
  <template>
52
82
  <h3>{{ pageTitle }}</h3>
@@ -55,9 +85,10 @@ const pageTitle = computed(() => {
55
85
  ```
56
86
 
57
87
  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:
88
+ you only need to pass the second argument, an object with used in the key variables:
59
89
 
60
90
  ```vue
91
+
61
92
  <template>
62
93
  <span>{{ t("Paste your variable here", { variable: locale }) }}</span>
63
94
  </template>
@@ -72,13 +103,14 @@ And your JSON must look like that:
72
103
  ```
73
104
 
74
105
  ```vue
106
+
75
107
  <script setup lang="ts">
76
- const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
77
-
78
- const pageTitle = computed(() => {
79
- return t("Hello, World");
80
- });
81
- </script>
108
+ const { t } = useMultilinguist(); // Call useMultilinguist composable to get the translation function
109
+
110
+ const pageTitle = computed(() => {
111
+ return t("Hello, World");
112
+ });
113
+ </script>
82
114
 
83
115
  <template>
84
116
  <h3>{{ pageTitle }}</h3>
@@ -101,15 +133,16 @@ And validation:
101
133
  ### Set another value to the current locale:
102
134
 
103
135
  ```vue
136
+
104
137
  <script setup lang="ts">
105
- const { t, setLocale } = useMultilinguist();
106
- // setLocale function accepts a string that should match one of defined
107
- // in the nuxt.config strings from supportedLanguages array
108
-
109
- const pageTitle = computed(() => {
110
- return t("Hello, World");
111
- });
112
- </script>
138
+ const { t, setLocale } = useMultilinguist();
139
+ // setLocale function accepts a string that should match one of defined
140
+ // in the nuxt.config strings from supportedLanguages array
141
+
142
+ const pageTitle = computed(() => {
143
+ return t("Hello, World");
144
+ });
145
+ </script>
113
146
 
114
147
  <template>
115
148
  <h3>{{ pageTitle }}</h3>
@@ -120,13 +153,14 @@ const pageTitle = computed(() => {
120
153
  ### Get current locale
121
154
 
122
155
  ```vue
156
+
123
157
  <script setup lang="ts">
124
- const { t, setLocale, locale } = useMultilinguist();
125
- // locale is SSR friendly, shared among the app ref state
158
+ const { t, setLocale, locale } = useMultilinguist();
159
+ // locale is SSR friendly, shared among the app ref state
126
160
 
127
- const pageTitle = computed(() => {
161
+ const pageTitle = computed(() => {
128
162
  return t("Hello, World");
129
- });
163
+ });
130
164
  </script>
131
165
 
132
166
  <template>
package/dist/module.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "@yiiamee/multilinguist",
3
3
  "configKey": "multilinguist",
4
- "version": "1.2.8",
4
+ "version": "1.3.0",
5
5
  "compatibility": {
6
6
  "nuxt": "^3.0.0"
7
7
  },
8
+ "defaults": {
9
+ "logging": true,
10
+ "defaultLocale": "",
11
+ "supportedLanguages": []
12
+ },
8
13
  "builder": {
9
14
  "@nuxt/module-builder": "1.0.1",
10
15
  "unbuild": "3.5.0"
package/dist/module.mjs CHANGED
@@ -49,7 +49,7 @@ declare module 'vue' {
49
49
  }
50
50
  `;
51
51
  };
52
- function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath) {
52
+ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath, logging = true) {
53
53
  async function generateTypes() {
54
54
  const defaultLocalePath = path.join(localesPath, `${defaultLocaleFromConfig}.json`);
55
55
  if (!fs.existsSync(defaultLocalePath)) {
@@ -61,7 +61,9 @@ function GenerateLocaleKeysPlugin(defaultLocaleFromConfig, localesPath, outPath)
61
61
  const output = generateOutputString(() => keys.map((key) => ` ${JSON.stringify(key)}: string;`).join("\n"));
62
62
  fs.mkdirSync(path.dirname(outPath), { recursive: true });
63
63
  fs.writeFileSync(outPath, output, "utf-8");
64
- console?.warn(`\u2705 Generated types to ${outPath} from ${defaultLocalePath}`);
64
+ if (logging) {
65
+ console?.warn(`\u2705 Generated types to ${outPath} from ${defaultLocalePath}`);
66
+ }
65
67
  }
66
68
  return {
67
69
  name: "vite-plugin-generate-locales-types",
@@ -80,22 +82,33 @@ const module = defineNuxtModule({
80
82
  meta: {
81
83
  name: "@yiiamee/multilinguist",
82
84
  configKey: "multilinguist",
83
- version: "1.2.8",
85
+ version: "1.3.0",
84
86
  compatibility: {
85
87
  nuxt: "^3.0.0"
88
+ },
89
+ defaults: {
90
+ logging: true,
91
+ defaultLocale: "",
92
+ supportedLanguages: []
86
93
  }
87
94
  },
88
95
  setup(moduleOptions, nuxtApp) {
89
96
  const resolver = createResolver(import.meta.url);
90
97
  addPlugin(resolver.resolve("runtime/plugin"));
91
98
  addImportsDir(resolver.resolve("runtime/composables"));
99
+ nuxtApp.options.runtimeConfig.public.multilinguist = {
100
+ defaultLocale: moduleOptions.defaultLocale,
101
+ supportedLanguages: moduleOptions.supportedLanguages,
102
+ logging: typeof moduleOptions.logging === "boolean" ? moduleOptions.logging : true
103
+ };
92
104
  nuxtApp.hook("vite:extendConfig", (viteConfig) => {
93
105
  viteConfig.plugins = viteConfig.plugins || [];
94
106
  viteConfig.plugins.push(
95
107
  GenerateLocaleKeysPlugin(
96
108
  moduleOptions.defaultLocale,
97
109
  `${nuxtApp.options.rootDir}/public/locales`,
98
- resolver.resolve("./runtime/types/generated-locales.d.ts")
110
+ resolver.resolve("./runtime/types/generated-locales.d.ts"),
111
+ moduleOptions.logging
99
112
  )
100
113
  );
101
114
  });
@@ -104,10 +117,6 @@ const module = defineNuxtModule({
104
117
  path: resolver.resolve("./runtime/types/generated-locales.d.ts")
105
118
  });
106
119
  });
107
- nuxtApp.options.runtimeConfig.public.multilinguist = {
108
- defaultLocale: moduleOptions.defaultLocale,
109
- supportedLanguages: moduleOptions.supportedLanguages
110
- };
111
120
  }
112
121
  });
113
122
 
@@ -1,5 +1,5 @@
1
1
  export default function useMultilinguist(): {
2
- t<K extends LocaleKey>(key: K, variables?: Record<string, string>): string;
2
+ t<K extends import("../types/generated-locales.js").LocaleKey>(key: K, variables?: Record<string, string>): string;
3
3
  setLocale: (locale: string) => void;
4
4
  initLocalization: () => Promise<void>;
5
5
  locale: import("vue").Ref<string, string>;
@@ -3,13 +3,16 @@ import useLocalization from "../runtime/composables/useLocalization";
3
3
  export default defineNuxtPlugin(async (nuxtApp) => {
4
4
  const config = nuxtApp.$config.public.multilinguist || {
5
5
  defaultLocale: "en",
6
- supportedLanguages: ["en"]
6
+ supportedLanguages: ["en"],
7
+ logging: true
7
8
  };
8
9
  const supportedLanguages = config.supportedLanguages;
9
10
  const defaultLocale = config.defaultLocale;
10
11
  const { initLocalization, ...localizationProperties } = useLocalization(supportedLanguages, defaultLocale);
11
12
  await initLocalization();
12
- console.log("[multilinguist] is initialised");
13
+ if (nuxtApp.$config.public.multilinguist.logging) {
14
+ console.log("[multilinguist] is initialised");
15
+ }
13
16
  nuxtApp.provide("localization", localizationProperties);
14
17
  nuxtApp.provide("t", localizationProperties.t);
15
18
  nuxtApp._localization = localizationProperties;
@@ -0,0 +1,40 @@
1
+
2
+ // AUTO-GENERATED FILE — DO NOT EDIT MANUALLY
3
+
4
+ export interface TranslationMessages {
5
+ "Hello, World": string;
6
+ "Switch Locale": string;
7
+ "Paste your variable here": string;
8
+ "nested.Nested key": string;
9
+ "nested.Language": string;
10
+ "nested.nested second level.nested second level language": string;
11
+ }
12
+
13
+ export type LocaleKey = keyof TranslationMessages;
14
+
15
+ export type TFunction = <K extends LocaleKey>(
16
+ key: K,
17
+ variables?: Record<string, string>
18
+ ) => string;
19
+
20
+ declare global {
21
+ type LocaleKey = keyof TranslationMessages;
22
+ type TFunction = <K extends LocaleKey>(
23
+ key: K,
24
+ variables?: Record<string, string>
25
+ ) => string;
26
+ }
27
+
28
+ declare module '#app' {
29
+ interface NuxtApp {
30
+ $t: TFunction;
31
+ t: TFunction;
32
+ }
33
+ }
34
+
35
+ declare module 'vue' {
36
+ interface ComponentCustomProperties {
37
+ $t: TFunction;
38
+ t: TFunction;
39
+ }
40
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@yiiamee/multilinguist",
3
- "version": "1.2.8",
4
- "description": "Nuxt 3 Multilinguist Localization module",
3
+ "version": "1.3.0",
4
+ "description": "Nuxt Multilinguist module for localizations",
5
5
  "repository": "yiiameeMich/multilinguist",
6
6
  "license": "MIT",
7
7
  "type": "module",
@@ -27,7 +27,10 @@
27
27
  "translation",
28
28
  "localization nuxt",
29
29
  "translation nuxt",
30
- "nuxt translation"
30
+ "nuxt translation",
31
+ "nuxt multilinguist",
32
+ "multilinguist nuxt",
33
+ "ssr localization"
31
34
  ],
32
35
  "bugs": {
33
36
  "email": "michaelkukh.dev@gmail.com"