nuxt-i18n-micro 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,198 @@
1
+ # Nuxt I18n Micro
2
+
3
+ <div class="links">
4
+ <a href="https://www.npmjs.com/package/nuxt-i18n-micro" target="_blank">
5
+ <img src="https://img.shields.io/npm/v/nuxt-i18n-micro/latest?style=for-the-badge" alt="npm version">
6
+ </a>
7
+ <a href="https://www.npmjs.com/package/nuxt-i18n-micro" target="_blank">
8
+ <img src="https://img.shields.io/npm/dw/nuxt-i18n-micro?style=for-the-badge" alt="npm downloads">
9
+ </a>
10
+ <a href="https://www.npmjs.com/package/nuxt-i18n-micro" target="_blank">
11
+ <img src="https://img.shields.io/npm/l/nuxt-i18n-micro?style=for-the-badge" alt="License">
12
+ </a>
13
+ <a href="https://nuxt.com" target="_blank">
14
+ <img src="https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js&style=for-the-badge" alt="Nuxt">
15
+ </a>
16
+ </div>
17
+
18
+ `Nuxt I18n Micro` is a fast, simple, and lightweight internationalization (i18n) module for Nuxt. Despite its compact size, it's designed with large projects in mind, offering significant performance improvements over traditional i18n solutions like `nuxt-i18n`. The module was built from the ground up to be highly efficient, focusing on minimizing build times, reducing server load, and shrinking bundle sizes.
19
+
20
+ ## Why Nuxt I18n Micro?
21
+
22
+ The `Nuxt I18n Micro` module was created to address critical performance issues found in the original `nuxt-i18n` module, particularly in high-traffic environments and projects with large translation files. Key issues with `nuxt-i18n` include:
23
+
24
+ - **High Memory Consumption**: Consumes significant memory during both build and runtime, leading to performance bottlenecks.
25
+ - **Slow Performance**: Especially with large translation files, it causes noticeable slowdowns in build times and server response.
26
+ - **Large Bundle Size**: Generates a large bundle, negatively impacting application performance.
27
+ - **Memory Leaks and Bugs**: Known for memory leaks and unpredictable behavior under heavy load.
28
+
29
+ ### Performance Comparison
30
+
31
+ To showcase the efficiency of `Nuxt I18n Micro`, we conducted tests under identical conditions. Both modules were tested with a 10MB translation file on the same hardware.
32
+
33
+ #### Build Time and Resource Consumption
34
+
35
+ **Nuxt I18n**:
36
+ - **Total size**: 54.7 MB (3.29 MB gzip)
37
+ - **Max CPU Usage**: 394.0%
38
+ - **Max Memory Usage**: 8746 MB
39
+ - **Elapsed Time**: 0h 1m 30s
40
+
41
+ **Nuxt I18n Micro**:
42
+ - **Total size**: 22.4 MB (2.9 MB gzip) — **59% smaller**
43
+ - **Max CPU Usage**: 305.3% — **23% lower**
44
+ - **Max Memory Usage**: 3247 MB — **63% less memory**
45
+ - **Elapsed Time**: 0h 0m 17s — **81% faster**
46
+
47
+ #### Server Performance (10k Requests)
48
+
49
+ **Nuxt I18n**:
50
+ - **Requests per second**: 49.05 [#/sec] (mean)
51
+ - **Time per request**: 611.599 ms (mean)
52
+ - **Max Memory Usage**: 703.73 MB
53
+
54
+ **Nuxt I18n Micro**:
55
+ - **Requests per second**: 61.18 [#/sec] (mean) — **25% more requests per second**
56
+ - **Time per request**: 490.379 ms (mean) — **20% faster**
57
+ - **Max Memory Usage**: 323.00 MB — **54% less memory usage**
58
+
59
+ These results clearly demonstrate that `Nuxt I18n Micro` significantly outperforms the original module in every critical area.
60
+
61
+ ## Key Features
62
+
63
+ - 🌐 **Compact Yet Powerful**: Despite its small size, `Nuxt I18n Micro` is designed for large-scale projects, focusing on performance and efficiency.
64
+ - ⚡ **Optimized Build and Runtime**: Reduces build times, memory usage, and server load, making it ideal for high-traffic applications.
65
+ - 🛠 **Minimalist Design**: The module is structured around just 5 components (1 module and 4 plugins), making it easy to understand, extend, and maintain.
66
+ - 📏 **Efficient Routing**: Generates only 2 routes regardless of the number of locales, thanks to dynamic regex-based routing, unlike other i18n modules that generate separate routes for each locale.
67
+ - 🗂 **Streamlined Translation Loading**: Only JSON files are supported, with translations split between a global file for common texts (e.g., menus) and page-specific files, which are auto-generated in the `dev` mode if not present.
68
+
69
+ ## Quick Setup
70
+
71
+ Install the module in your Nuxt application with:
72
+
73
+ ```bash
74
+ npm install nuxt-i18n-micro
75
+ ```
76
+
77
+ Then, add it to your `nuxt.config.ts`:
78
+
79
+ ```typescript
80
+ export default defineNuxtConfig({
81
+ modules: [
82
+ 'nuxt-i18n-micro',
83
+ ],
84
+ i18n: {
85
+ locales: [
86
+ { code: 'en', iso: 'en-US', dir: 'ltr' },
87
+ { code: 'fr', iso: 'fr-FR', dir: 'ltr' },
88
+ { code: 'ar', iso: 'ar-SA', dir: 'rtl' },
89
+ ],
90
+ defaultLocale: 'en',
91
+ translationDir: 'locales',
92
+ meta: true,
93
+ },
94
+ })
95
+ ```
96
+
97
+ That's it! You're now ready to use Nuxt I18n Micro in your Nuxt app.
98
+
99
+ ## Folder Structure
100
+
101
+ Translations are organized into global and page-specific files:
102
+
103
+ ```
104
+ /locales
105
+ /pages
106
+ /index
107
+ en.json
108
+ fr.json
109
+ ar.json
110
+ /about
111
+ en.json
112
+ fr.json
113
+ ar.json
114
+ en.json
115
+ fr.json
116
+ ar.json
117
+ ```
118
+
119
+ - **Global File**: Located at `locales/{locale}.json` (e.g., `locales/en.json`). Used for common texts shared across multiple pages, such as menus.
120
+ - **Page-Specific Files**: Located at `locales/pages/{routeName}/{locale}.json` (e.g., `locales/pages/index/en.json`). These are used for translations specific to individual pages.
121
+
122
+ ## Plugin Methods
123
+
124
+ ### `$getLocale()`
125
+ Returns the current locale code.
126
+
127
+ ```typescript
128
+ const locale = $getLocale()
129
+ ```
130
+
131
+ ### `$getLocales()`
132
+ Returns an array of all available locales configured in the module.
133
+
134
+ ```typescript
135
+ const locales = $getLocales()
136
+ ```
137
+
138
+ ### `$t(key: string, params?: Record<string, any>, defaultValue?: string)`
139
+ Fetches a translation for the given key. Optionally interpolates parameters into the translation.
140
+
141
+ ```typescript
142
+ const welcomeMessage = $t('welcome', { username: 'Alice', unreadCount: 5 })
143
+ ```
144
+
145
+ ### `$tc(key: string, count: number, defaultValue?: string)`
146
+ Fetches a pluralized translation for the given key based on the count.
147
+
148
+ ```typescript
149
+ const appleCountMessage = $tc('apples', 10)
150
+ ```
151
+
152
+ ### `$switchLocale(locale: string)`
153
+ Switches to the given locale and redirects the user to the appropriate localized route.
154
+
155
+ ```typescript
156
+ $switchLocale('fr')
157
+ ```
158
+
159
+ ### `$localeRoute(to: RouteLocationRaw): RouteLocationRaw`
160
+ Generates a localized route object based on the target route.
161
+
162
+ ```typescript
163
+ const localizedRoute = $localeRoute({ name: 'index' })
164
+ ```
165
+
166
+ ### `$mergeTranslations(newTranslations: Translations)`
167
+ Merges new translations into the existing translation cache for the current route and locale.
168
+
169
+ ```typescript
170
+ $mergeTranslations({
171
+ welcome: 'Bienvenue, {username}!'
172
+ })
173
+ ```
174
+
175
+ ## Module Options
176
+
177
+ The module accepts the following options in the Nuxt configuration:
178
+
179
+ - **locales**: An array of locale objects. Each locale should have a `code`, and optionally, `iso` and `dir` (for RTL/LTR).
180
+ - Example:
181
+ ```typescript
182
+ locales: [
183
+ { code: 'en', iso: 'en-US', dir: 'ltr' },
184
+ { code: 'fr', iso: 'fr-FR', dir: 'ltr' },
185
+ { code: 'ar', iso: 'ar-SA', dir: 'rtl' }
186
+ ]
187
+ ```
188
+ - **meta**: A boolean indicating whether to automatically generate SEO-related meta tags (like `alternate` links).
189
+ - **defaultLocale**: The default locale code (e.g., `'en'`).
190
+ - **translationDir**: The directory where translation files are stored (e.g., `'locales'`).
191
+ - **autoDetectLanguage**: If `true`, automatically detects the user's preferred language and redirects accordingly.
192
+ - **plural**: A custom function for handling pluralization.
193
+
194
+ ## Conclusion
195
+
196
+ `Nuxt I18n Micro` offers a highly efficient, minimalist approach to internationalization in Nuxt applications. By focusing on performance and simplicity, it provides a powerful alternative to heavier, more complex i18n solutions. Whether you're building a small website or a large-scale application, `Nuxt I18n Micro` helps you manage multilingual content with ease.
197
+
198
+ For more details and updates, visit the [Nuxt I18n Micro GitHub repository](https://github.com/s00d/nuxt-i18n-micro).
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html><html data-capo=""><head><meta charset="utf-8">
2
+ <meta name="viewport" content="width=device-width, initial-scale=1">
3
+ <link rel="stylesheet" href="/__nuxt-i18n-micro/_nuxt/entry.BkbVpYOg.css">
4
+ <link rel="modulepreload" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js">
5
+ <link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-404.CjTTbIxB.css">
6
+ <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/BdbjBFjr.js">
7
+ <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/DdqCv46g.js">
8
+ <link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-500.B4KzowuE.css">
9
+ <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/HZLiFEh-.js">
10
+ <script type="module" src="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" id="__NUXT_DATA__" data-ssr="false">[{"serverRendered":1},false]</script>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"3cd90d35-1876-4439-af68-a6e627aa85e6",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html><html data-capo=""><head><meta charset="utf-8">
2
+ <meta name="viewport" content="width=device-width, initial-scale=1">
3
+ <link rel="stylesheet" href="/__nuxt-i18n-micro/_nuxt/entry.BkbVpYOg.css">
4
+ <link rel="modulepreload" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js">
5
+ <link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-404.CjTTbIxB.css">
6
+ <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/BdbjBFjr.js">
7
+ <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/DdqCv46g.js">
8
+ <link rel="prefetch" as="style" href="/__nuxt-i18n-micro/_nuxt/error-500.B4KzowuE.css">
9
+ <link rel="prefetch" as="script" crossorigin href="/__nuxt-i18n-micro/_nuxt/HZLiFEh-.js">
10
+ <script type="module" src="/__nuxt-i18n-micro/_nuxt/6EJ4fAZ2.js" crossorigin></script></head><body><div id="__nuxt"></div><div id="teleports"></div><script type="application/json" id="__NUXT_DATA__" data-ssr="false">[{"serverRendered":1},false]</script>
11
+ <script>window.__NUXT__={};window.__NUXT__.config={public:{},app:{baseURL:"/__nuxt-i18n-micro",buildId:"3cd90d35-1876-4439-af68-a6e627aa85e6",buildAssetsDir:"/_nuxt/",cdnURL:""}}</script></body></html>