fastify-intlayer 7.5.10

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 ADDED
@@ -0,0 +1,282 @@
1
+ <p align="center">
2
+ <a href="https://intlayer.org" rel="">
3
+ <img src="https://raw.githubusercontent.com/aymericzip/intlayer/main/docs/assets/cover.png" width="60%" alt="Intlayer Logo" />
4
+ </a>
5
+ </p>
6
+
7
+ <h1 align="center">
8
+ <strong>Per-component i18n</strong>
9
+ </h1>
10
+ <h2 align="center">
11
+ <strong>AI-powered translation. Visual Editor. Multilingual CMS.</strong>
12
+ </h2>
13
+
14
+ <br />
15
+
16
+ <p align="center">
17
+ <a href="https://intlayer.org/doc/concept/content" rel="">Docs</a> •
18
+ <a href="https://intlayer.org/doc/environment/nextjs" rel="">Next.js</a> •
19
+ <a href="https://intlayer.org/doc/environment/vite-and-react" rel="">React + Vite</a> •
20
+ <a href="https://intlayer.org/doc/concept/cms" rel="">CMS</a> •
21
+ <a href="https://discord.gg/7uxamYVeCk" rel="noopener noreferrer nofollow">Discord</a>
22
+ </p>
23
+ <p align="center" style="margin-top:15px;">
24
+ <a href="https://www.npmjs.com/package/intlayer" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/npm/v/intlayer?style=for-the-badge&labelColor=FFFFFF&color=000000&logoColor=FFFFFF" alt="npm version" height="24"/></a>
25
+ <a href="https://github.com/aymericzip/intlayer/stargazers" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/github/stars/aymericzip/intlayer?style=for-the-badge&labelColor=000000&color=FFFFFF&logo=github&logoColor=FFD700" alt="GitHub Stars" height="24"/></a>
26
+ <a href="https://www.npmjs.org/package/intlayer" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/npm/dm/intlayer?style=for-the-badge&labelColor=000000&color=FFFFFF&logoColor=000000&cacheSeconds=86400" alt="monthly downloads" height="24"/></a>
27
+ <a href="https://github.com/aymericzip/intlayer/blob/main/LICENSE" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/github/license/aymericzip/intlayer?style=for-the-badge&labelColor=000000&color=FFFFFF&logoColor=000000&cacheSeconds=86400" alt="license"/></a>
28
+ <a href="https://github.com/aymericzip/intlayer/commits/main" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/github/last-commit/aymericzip/intlayer?style=for-the-badge&labelColor=000000&color=FFFFFF&logoColor=000000&cacheSeconds=86400" alt="last commit"/>
29
+ </a>
30
+ <a href="https://www.bountyhub.dev/en/bounty/view/a2f24259-80ae-4a19-82e7-288718fba449/adapt-markdown-parser-in-a-custom-packages" target="_blank" rel="noopener noreferrer nofollow"><img src="https://img.shields.io/badge/Bounties-on%20BountyHub-yellow?style=for-the-badge&labelColor=000000&color=FFFFFF&logoColor=000000&cacheSeconds=86400" alt="Bounties on BountyHub"/>
31
+ </a>
32
+ </p>
33
+
34
+ ![Watch the video](https://github.com/aymericzip/intlayer/blob/main/docs/assets/demo_video.gif)
35
+
36
+ <a href="https://intlayer.org/doc/concept/content" rel="">
37
+ <img src="https://img.shields.io/badge/Get_Started-FFFFFF?style=for-the-badge&logo=rocket&logoColor=black" />
38
+ </a>
39
+
40
+ ## What is Intlayer?
41
+
42
+ Most i18n libraries are either too complex, too rigid, or not built for modern frameworks.
43
+
44
+ Intlayer is a **modern i18n solution** for web and mobile apps.
45
+ It’s framework-agnostic, **AI-powered**, and includes a free **CMS & visual editor**.
46
+
47
+ With **per-locale content files**, **TypeScript autocompletion**, **tree-shakable dictionaries**, and **CI/CD integration**, Intlayer makes internationalization **faster, cleaner, and smarter**.
48
+
49
+ ## Keys benefits of Intlayer:
50
+
51
+ | Feature | Description |
52
+ | --------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
53
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/frameworks.png?raw=true" alt="Feature" width="700"> | **Cross-Frameworks Support**<br><br>Intlayer is compatible with all major frameworks and libraries, including Next.js, React, Vite, Vue.js, Nuxt, Preact, Express, and more. |
54
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/javascript_content_management.jpg?raw=true" alt="Feature" width="700"> | **JavaScript-Powered Content Management**<br><br>Harness the flexibility of JavaScript to define and manage your content efficiently. <br><br> - [Content declaration](https://intlayer.org/doc/concept/content) |
55
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/per_locale_content_declaration_file.png?raw=true" alt="Feature" width="700"> | **Per-Locale Content Declaration File**<br><br>Speed up your development by declaring your content once, before auto generation.<br><br> - [Per-Locale Content Declaration File](https://intlayer.org/doc/concept/per-locale-file) |
56
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/autocompletion.png?raw=true" alt="Feature" width="700"> | **Type-Safe Environment**<br><br>Leverage TypeScript to ensure your content definitions and code are error-free, while also benefiting from IDE autocompletion.<br><br> - [TypeScript configuration](https://intlayer.org/doc/environment/vite-and-react#configure-typescript) |
57
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/config_file.png?raw=true" alt="Feature" width="700"> | **Simplified Setup**<br><br>Get up and running quickly with minimal configuration. Adjust settings for internationalization, routing, AI, build, and content handling with ease. <br><br> - [Explore Next.js integration](https://intlayer.org/doc/environment/nextjs) |
58
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/content_retrieval.png?raw=true" alt="Feature" width="700"> | **Simplified Content Retrieval**<br><br>No need to call your `t` function for each piece of content. Retrieve all your content directly using a single hook.<br><br> - [React integration](https://intlayer.org/doc/environment/create-react-app) |
59
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/server_component.png?raw=true" alt="Feature" width="700"> | **Consistent Server Component Implementation**<br><br>Perfectly suited for Next.js server components, use the same implementation for both client and server components, no need to pass your `t` function across each server component. <br><br> - [Server Components](https://intlayer.org/doc/environment/nextjs#step-7-utilize-content-in-your-code) |
60
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/file_tree.png?raw=true" alt="Feature" width="700"> | **Organized Codebase**<br><br>Keep your codebase more organized: 1 component = 1 dictionary in the same folder. Translations close to their respective components, enhance maintainability and clarity. <br><br> - [How Intlayer works](https://intlayer.org/doc/concept/how-works-intlayer) |
61
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/url_routing.png?raw=true" alt="Feature" width="700"> | **Enhanced Routing**<br><br>Full support of app routing, adapting seamlessly to complex application structures, for Next.js, React, Vite, Vue.js, etc.<br><br> - [Explore Next.js integration](https://intlayer.org/doc/environment/nextjs) |
62
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/markdown.png?raw=true" alt="Feature" width="700"> | **Markdown Support**<br><br>Import and interpret, locale files and remote Markdown for multilingual content like privacy policies, documentation, etc. Interpret and make Markdown metadata accessible in your code.<br><br> - [Content files](https://intlayer.org/doc/concept/content/file) |
63
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/visual_editor.png?raw=true" alt="Feature" width="700"> | **Free Visual Editor & CMS**<br><br>A free visual editor and CMS are available for content writers, removing the need for a localization platform. Keep your content synchronized using Git, or externalize it totally or partially with the CMS.<br><br> - [Intlayer Editor](https://intlayer.org/doc/concept/editor) <br> - [Intlayer CMS](https://intlayer.org/doc/concept/cms) |
64
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/bundle.png?raw=true" alt="Feature" width="700"> | **Tree-shakable Content**<br><br>Tree-shakable content, reducing the size of the final bundle. Loads content per component, excluding any unused content from your bundle. Supports lazy loading to enhance app loading efficiency. <br><br> - [App build optimization](https://intlayer.org/doc/concept/how-works-intlayer#app-build-optimization) |
65
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/static_rendering.png?raw=true" alt="Feature" width="700"> | **Static Rendering**<br><br>Doesn't block Static Rendering. <br><br> - [Next.js integration](https://intlayer.org/doc/environment/nextjs) |
66
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/AI_translation.png?raw=true" alt="Feature" width="700"> | **AI-Powered Translation**<br><br>Transform your website into 231 languages with just one click using Intlayer's advanced AI-powered translation tools using your own AI provider / API key. <br><br> - [CI/CD integration](https://intlayer.org/doc/concept/ci-cd) <br> - [Intlayer CLI](https://intlayer.org/doc/concept/cli) <br> - [Auto fill](https://intlayer.org/doc/concept/auto-fill) |
67
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/mcp.png?raw=true" alt="Feature" width="700"> | **MCP Server Integration**<br><br>Provides an MCP (Model Context Protocol) server for IDE automation, enabling seamless content management and i18n workflows directly within your development environment. <br><br> - [MCP Server](https://github.com/aymericzip/intlayer/blob/main/docs/docs/en/mcp_server.md) |
68
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/vscode_extension.png?raw=true" alt="Feature" width="700"> | **VSCode Extension**<br><br>Intlayer provides a VSCode extension to help you manage your content and translations, building your dictionaries, translating your content, and more. <br><br> - [VSCode Extension](https://intlayer.org/doc/vs-code-extension) |
69
+ | <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/interoperability.png?raw=true" alt="Feature" width="700"> | **Interoperability**<br><br>Allow interoperability with react-i18next, next-i18next, next-intl, and react-intl. <br><br> - [Intlayer and react-intl](https://intlayer.org/blog/intlayer-with-react-intl) <br> - [Intlayer and next-intl](https://intlayer.org/blog/intlayer-with-next-intl) <br> - [Intlayer and next-i18next](https://intlayer.org/blog/intlayer-with-next-i18next) |
70
+
71
+ ---
72
+
73
+ ## 📦 Installation
74
+
75
+ Start your journey with Intlayer today and experience a smoother, more powerful approach to internationalization.
76
+
77
+ <a href="https://intlayer.org/doc/concept/content" rel="">
78
+ <img src="https://img.shields.io/badge/Get_Started-FFFFFF?style=for-the-badge&logo=rocket&logoColor=black" />
79
+ </a>
80
+
81
+ ```bash
82
+ npm install intlayer react-intlayer
83
+ ```
84
+
85
+ ⚡ Quick Start (Next.js)
86
+
87
+ ```ts
88
+ // intlayer.config.ts
89
+ import { Locales, type IntlayerConfig } from "intlayer";
90
+
91
+ const config: IntlayerConfig = {
92
+ internationalization: {
93
+ locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
94
+ defaultLocale: Locales.ENGLISH,
95
+ },
96
+ };
97
+
98
+ export default config;
99
+ ```
100
+
101
+ ```ts
102
+ // app/home.content.ts
103
+ import { t, type Dictionary } from "intlayer";
104
+
105
+ const content = {
106
+ key: "home",
107
+ content: {
108
+ title: t({
109
+ en: "Home",
110
+ fr: "Accueil",
111
+ es: "Inicio",
112
+ }),
113
+ },
114
+ } satisfies Dictionary;
115
+
116
+ export default content;
117
+ ```
118
+
119
+ ```tsx
120
+ // app/page.tsx
121
+ import { useIntlayer } from "react-intlayer";
122
+
123
+ const HomePage = () => {
124
+ const { title } = useIntlayer("home");
125
+
126
+ return <h1>{title}</h1>;
127
+ };
128
+ ```
129
+
130
+ <a href="https://intlayer.org/doc/environment/nextjs"> Get the full guide → </a>
131
+
132
+ ## 🎥 Live tutorial on YouTube
133
+
134
+ [![How to Internationalize your application using Intlayer](https://i.ytimg.com/vi/e_PPG7PTqGU/hqdefault.jpg?sqp=-oaymwEcCNACELwBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLDtyJ4uYotEjl12nZ_gZKZ_kjEgOQ)](https://youtu.be/e_PPG7PTqGU?si=GyU_KpVhr61razRw)
135
+
136
+ <a href="https://intlayer.org/doc/concept/content" rel="">
137
+ <img src="https://img.shields.io/badge/Get_Started-FFFFFF?style=for-the-badge&logo=rocket&logoColor=black" />
138
+ </a>
139
+
140
+ ## Table of Contents
141
+
142
+ Explore our comprehensive documentation to get started with Intlayer and learn how to integrate it into your projects.
143
+
144
+ <details open>
145
+ <summary style="font-size:16px; font-weight:bold;">📘 Get Started</summary>
146
+ <ul>
147
+ <li><a href="https://intlayer.org/doc/why" rel=''>Why Intlayer?</a></li>
148
+ <li><a href="https://intlayer.org/doc" rel=''>Introduction</a></li>
149
+ </ul>
150
+ </details>
151
+
152
+ <details>
153
+ <summary style="font-size:16px; font-weight:bold;">⚙️ Concept</summary>
154
+ <ul>
155
+ <li><a href="https://intlayer.org/doc/concept/how-works-intlayer" rel=''>How Intlayer Works</a></li>
156
+ <li><a href="https://intlayer.org/doc/concept/configuration" rel=''>Configuration</a></li>
157
+ <li><a href="https://intlayer.org/doc/concept/cli" rel=''>Intlayer CLI</a></li>
158
+ <li><a href="https://intlayer.org/doc/concept/editor" rel=''>Intlayer Editor</a></li>
159
+ <li><a href="https://intlayer.org/doc/concept/cms" rel=''>Intlayer CMS</a></li>
160
+ <li><a href="https://intlayer.org/doc/concept/content" rel=''>Dictionary</a>
161
+ <ul>
162
+ <li><a href="https://intlayer.org/doc/concept/content/per-locale-file" rel=''>Per-Locale Content Declaration File</a></li>
163
+ <li><a href="https://intlayer.org/doc/concept/content/translation" rel=''>Translation</a></li>
164
+ <li><a href="https://intlayer.org/doc/concept/content/enumeration" rel=''>Enumeration</a></li>
165
+ <li><a href="https://intlayer.org/doc/concept/content/condition" rel=''>Condition</a></li>
166
+ <li><a href="https://intlayer.org/doc/concept/content/nesting" rel=''>Nesting</a></li>
167
+ <li><a href="https://intlayer.org/doc/concept/content/markdown" rel=''>Markdown</a></li>
168
+ <li><a href="https://intlayer.org/doc/concept/content/function-fetching" rel=''>Function Fetching</a></li>
169
+ <li><a href="https://intlayer.org/doc/concept/content/insertion" rel=''>Insertion</a></li>
170
+ <li><a href="https://intlayer.org/doc/concept/content/file" rel=''>File</a></li>
171
+ </ul>
172
+ </li>
173
+ </ul>
174
+ </details>
175
+
176
+ <details open>
177
+ <summary style="font-size:16px; font-weight:bold;">🌐 Environment</summary>
178
+ <ul>
179
+ <li><a href="https://intlayer.org/doc/environment/nextjs" rel=''>Intlayer with Next.js 16</a>
180
+ <ul>
181
+ <li><a href="https://intlayer.org/doc/environment/nextjs/15" rel=''>Next.js 15</a></li>
182
+ <li><a href="https://intlayer.org/doc/environment/nextjs/14" rel=''>Next.js 14 (App Router)</a></li>
183
+ <li><a href="https://intlayer.org/doc/environment/nextjs/next-with-Page-Router" rel=''>Next.js Page Router</a></li>
184
+ </ul>
185
+ </li>
186
+ <li><a href="https://intlayer.org/doc/environment/create-react-app" rel=''>React CRA</a></li>
187
+ <li><a href="https://intlayer.org/doc/environment/vite-and-react" rel=''>Vite + React</a>
188
+ <ul>
189
+ <li><a href="https://intlayer.org/doc/environment/vite-and-react/react-router-v7" rel=''>React-router-v7</a></li>
190
+ <li><a href="https://intlayer.org/doc/environment/vite-and-react/tanstack-start" rel=''>Tanstack start</a></li>
191
+ </ul>
192
+ </li>
193
+ <li><a href="https://intlayer.org/doc/environment/react-native-and-expo" rel=''>React Native</a></li>
194
+ <li><a href="https://intlayer.org/doc/environment/lynx-and-react" rel=''>Lynx + React</a></li>
195
+ <li><a href="https://intlayer.org/doc/environment/vite-and-svelte" rel=''>Vite + Svelte</a></li>
196
+ <li><a href="https://intlayer.org/doc/environment/sveltekit" rel=''>SvelteKit</a></li>
197
+ <li><a href="https://intlayer.org/doc/environment/vite-and-preact" rel=''>Vite + Preact</a></li>
198
+ <li><a href="https://intlayer.org/doc/environment/vite-and-vue" rel=''>Vite + Vue</a></li>
199
+ <li><a href="https://intlayer.org/doc/environment/vite-and-nuxt" rel=''>Vite + Nuxt</a></li>
200
+ <li><a href="https://intlayer.org/doc/environment/vite-and-solid" rel=''>Vite + Solid</a></li>
201
+ <li><a href="https://intlayer.org/doc/environment/angular" rel=''>Angular</a></li>
202
+ <li><a href="https://intlayer.org/doc/environment/express" rel=''>Express</a></li>
203
+ <li><a href="https://intlayer.org/doc/environment/nest" rel=''>NestJS</a></li>
204
+ </ul>
205
+ </details>
206
+
207
+ <details>
208
+ <summary style="font-size:16px; font-weight:bold;">📰 Blog</summary>
209
+ <ul>
210
+ <li><a href="https://github.com/aymericzip/intlayer/blob/main/docs/blog/en/what_is_internationalization.md" rel=''>What is i18n</a></li>
211
+ <li><a href="https://intlayer.org/blog/SEO-and-i18n" rel=''>i18n and SEO</a></li>
212
+ <li><a href="https://intlayer.org/blog/intlayer-with-next-i18next" rel=''>Intlayer and i18next</a></li>
213
+ <li><a href="https://intlayer.org/blog/intlayer-with-react-i18next" rel=''>Intlayer and react-intl</a></li>
214
+ <li><a href="https://intlayer.org/blog/intlayer-with-next-intl" rel=''>Intlayer and next-intl</a></li>
215
+ </ul>
216
+ </details>
217
+
218
+ ## 🌐 Readme in other languages
219
+
220
+ <p align="center">
221
+ <a href="https://github.com/aymericzip/intlayer/blob/main/readme.md">English</a> •
222
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/readme.md">简体中文</a> •
223
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/ru/readme.md">Русский</a> •
224
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/ja/readme.md">日本語</a> •
225
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/fr/readme.md">Français</a> •
226
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/ko/readme.md">한국어</a> •
227
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/es/readme.md">Español</a> •
228
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/de/readme.md">Deutsch</a> •
229
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/ar/readme.md">العربية</a> •
230
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/it/readme.md">Italiano</a> •
231
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/en-GB/readme.md">English (UK)</a> •
232
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/pt/readme.md">Português</a> •
233
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/hi/readme.md">हिन्दी</a> •
234
+ <a href="https://github.com/aymericzip/intlayer/blob/main/docs/docs/tr/readme.md">Türkçe</a>
235
+ </p>
236
+
237
+ ## 🤝 Community
238
+
239
+ Intlayer is built with and for the community and we’d love your input!
240
+
241
+ - Have a suggestion? [Open an issue](https://github.com/aymericzip/intlayer/issues)
242
+ - Found a bug or improvement? [Submit a PR](https://github.com/aymericzip/intlayer/pulls)
243
+ - Need help or want to connect? [Join our Discord](https://discord.gg/7uxamYVeCk)
244
+
245
+ You can also follow us on :
246
+
247
+ <div>
248
+ <br/>
249
+ <p align="center">
250
+ <a href="https://discord.gg/528mBV4N" target="blank" rel='noopener noreferrer nofollow'><img align="center"
251
+ src="https://img.shields.io/badge/discord-5865F2.svg?style=for-the-badge&logo=discord&logoColor=white"
252
+ alt="Intlayer Discord" height="30"/></a>
253
+ <a href="https://www.linkedin.com/company/intlayerorg" target="blank" rel='noopener noreferrer nofollow'><img align="center"
254
+ src="https://img.shields.io/badge/linkedin-%231DA1F2.svg?style=for-the-badge&logo=linkedin&logoColor=white"
255
+ alt="Intlayer LinkedIn" height="30"/></a>
256
+ <a href="https://www.instagram.com/intlayer/" target="blank" rel='noopener noreferrer nofollow'><img align="center"
257
+ src="https://img.shields.io/badge/instagram-%23E4405F.svg?style=for-the-badge&logo=Instagram&logoColor=white"
258
+ alt="Intlayer Instagram" height="30"/></a>
259
+ <a href="https://x.com/Intlayer183096" target="blank" rel='noopener noreferrer nofollow'><img align="center"
260
+ src="https://img.shields.io/badge/x-1DA1F2.svg?style=for-the-badge&logo=x&logoColor=white"
261
+ alt="Intlayer X" height="30"/></a>
262
+ <a href="https://www.youtube.com/@intlayer" target="blank" rel='noopener noreferrer nofollow'><img align="center"
263
+ src="https://img.shields.io/badge/youtube-FF0000.svg?style=for-the-badge&logo=youtube&logoColor=white"
264
+ alt="Intlayer YouTube" height="30"/></a>
265
+ <a href="https://www.tiktok.com/@intlayer" target="blank" rel='noopener noreferrer nofollow'><img align="center"
266
+ src="https://img.shields.io/badge/tiktok-000000.svg?style=for-the-badge&logo=tiktok&logoColor=white"
267
+ alt="Intlayer TikTok" height="30"/></a>
268
+ <br>
269
+ </p>
270
+ </div>
271
+
272
+ ### Contribution
273
+
274
+ For more detailed guidelines on contributing to this project, please refer to the [`CONTRIBUTING.md`](https://github.com/aymericzip/intlayer/blob/main/CONTRIBUTING.md) file. It contains essential information on our development process, commit message conventions, and release procedures. Your contributions are valuable to us, and we appreciate your efforts in making this project better!
275
+
276
+ Contribute on [GitHub](https://github.com/aymericzip/intlayer), [GitLab](https://gitlab.com/ay.pineau/intlayer), or [Bitbucket](https://bitbucket.org/intlayer/intlayer/).
277
+
278
+ ### Thank You for the Support
279
+
280
+ If you like Intlayer, give us a ⭐ on GitHub. It helps others discover the project! [See why GitHub Stars matter](https://github.com/aymericzip/intlayer/blob/main/CONTRIBUTING.md#why-github-stars-matter-).
281
+
282
+ [![Star History Chart](https://api.star-history.com/svg?repos=aymericzip/intlayer&type=Date)](https://star-history.com/#aymericzip/intlayer&Date)
@@ -0,0 +1,29 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+
29
+ exports.__toESM = __toESM;
@@ -0,0 +1,110 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ let _intlayer_chokidar = require("@intlayer/chokidar");
3
+ let _intlayer_config = require("@intlayer/config");
4
+ let _intlayer_core = require("@intlayer/core");
5
+ let cls_hooked = require("cls-hooked");
6
+ let fastify_plugin = require("fastify-plugin");
7
+ fastify_plugin = require_rolldown_runtime.__toESM(fastify_plugin);
8
+
9
+ //#region src/index.ts
10
+ const configuration = (0, _intlayer_config.getConfiguration)();
11
+ const { internationalization } = configuration;
12
+ /**
13
+ * Retrieves the locale from storage (cookies, headers).
14
+ * Note: req.cookies requires @fastify/cookie to be registered.
15
+ * We cast req to any to avoid hard dependency on @fastify/cookie types.
16
+ */
17
+ const getStorageLocale = (req) => (0, _intlayer_core.getLocaleFromStorage)({
18
+ getCookie: (name) => req.cookies?.[name],
19
+ getHeader: (name) => req.headers?.[name]
20
+ });
21
+ const appNamespace = (0, cls_hooked.createNamespace)("app");
22
+ (0, _intlayer_chokidar.prepareIntlayer)(configuration);
23
+ const translateFunction = (req) => (content, locale) => {
24
+ const { locale: currentLocale, defaultLocale } = req.intlayer;
25
+ const targetLocale = locale ?? currentLocale;
26
+ if (typeof content === "undefined") return "";
27
+ if (typeof content === "string") return content;
28
+ if (typeof content?.[targetLocale] === "undefined") if (typeof content?.[defaultLocale] === "undefined") return content;
29
+ else return (0, _intlayer_core.getTranslation)(content, defaultLocale);
30
+ return (0, _intlayer_core.getTranslation)(content, targetLocale);
31
+ };
32
+ /**
33
+ * Fastify Plugin to detect locale and load it into request context
34
+ */
35
+ const fastifyIntlayer = async (fastify, _opts) => {
36
+ if (!fastify.hasRequestDecorator("intlayer")) fastify.decorateRequest("intlayer", null);
37
+ fastify.addHook("preHandler", (req, _reply, done) => {
38
+ const localeFromStorage = getStorageLocale(req);
39
+ const negotiatorHeaders = {};
40
+ if (req && typeof req.headers === "object") for (const key in req.headers) {
41
+ const val = req.headers[key];
42
+ if (typeof val === "string") negotiatorHeaders[key] = val;
43
+ else if (Array.isArray(val)) negotiatorHeaders[key] = val.join(",");
44
+ }
45
+ const localeDetected = (0, _intlayer_core.localeDetector)(negotiatorHeaders, internationalization.locales, internationalization.defaultLocale);
46
+ const locale = localeFromStorage ?? localeDetected;
47
+ const defaultLocale = internationalization.defaultLocale;
48
+ const getIntlayerWrapped = (key, localeArg = localeDetected, ...props) => (0, _intlayer_core.getIntlayer)(key, localeArg, ...props);
49
+ const getDictionaryWrapped = (key, localeArg = localeDetected, ...props) => (0, _intlayer_core.getDictionary)(key, localeArg, ...props);
50
+ req.intlayer = {
51
+ locale_storage: localeFromStorage,
52
+ locale_detected: localeDetected,
53
+ locale,
54
+ defaultLocale,
55
+ getIntlayer: getIntlayerWrapped,
56
+ getDictionary: getDictionaryWrapped,
57
+ t: void 0
58
+ };
59
+ const t$1 = translateFunction(req);
60
+ req.intlayer.t = t$1;
61
+ appNamespace.run(() => {
62
+ appNamespace.set("t", t$1);
63
+ appNamespace.set("getIntlayer", getIntlayerWrapped);
64
+ appNamespace.set("getDictionary", getDictionaryWrapped);
65
+ done();
66
+ });
67
+ });
68
+ };
69
+ const intlayer = (0, fastify_plugin.default)(fastifyIntlayer, {
70
+ name: "fastify-intlayer",
71
+ fastify: "5.x"
72
+ });
73
+ const t = (content, locale) => {
74
+ try {
75
+ if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.");
76
+ if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"fastify-intlayer\" is not supported in your environment outside of a request context or proper setup. Use req.intlayer.t instead.");
77
+ return appNamespace.get("t")(content, locale);
78
+ } catch (error) {
79
+ if (process.env.NODE_ENV === "development") console.error(error.message);
80
+ return (0, _intlayer_core.getTranslation)(content, locale ?? internationalization.defaultLocale);
81
+ }
82
+ };
83
+ const getIntlayer = (...args) => {
84
+ try {
85
+ if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.");
86
+ if (typeof appNamespace.get("getIntlayer") !== "function") throw new Error("Context not found. Ensure you are inside a request handling flow.");
87
+ return appNamespace.get("getIntlayer")(...args);
88
+ } catch (error) {
89
+ if (process.env.NODE_ENV === "development") console.error(error.message);
90
+ return (0, _intlayer_core.getIntlayer)(...args);
91
+ }
92
+ };
93
+ const getDictionary = (...args) => {
94
+ try {
95
+ if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.");
96
+ if (typeof appNamespace.get("getDictionary") !== "function") throw new Error("Context not found. Ensure you are inside a request handling flow.");
97
+ return appNamespace.get("getDictionary")(...args);
98
+ } catch (error) {
99
+ if (process.env.NODE_ENV === "development") console.error(error.message);
100
+ return (0, _intlayer_core.getDictionary)(...args);
101
+ }
102
+ };
103
+
104
+ //#endregion
105
+ exports.getDictionary = getDictionary;
106
+ exports.getIntlayer = getIntlayer;
107
+ exports.intlayer = intlayer;
108
+ exports.t = t;
109
+ exports.translateFunction = translateFunction;
110
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["fastifyIntlayer: FastifyPluginAsync","negotiatorHeaders: Record<string, string>","getIntlayerWrapped: typeof getIntlayerFunction","getDictionaryWrapped: typeof getDictionaryFunction","t","getIntlayer: typeof getIntlayerFunction","getDictionary: typeof getDictionaryFunction"],"sources":["../../src/index.ts"],"sourcesContent":["import { prepareIntlayer } from '@intlayer/chokidar';\nimport { getConfiguration } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getLocaleFromStorage,\n getTranslation,\n localeDetector,\n} from '@intlayer/core';\nimport type { Locale, StrictModeLocaleMap } from '@intlayer/types';\nimport { createNamespace } from 'cls-hooked';\nimport type { FastifyPluginAsync, FastifyRequest } from 'fastify';\nimport fp from 'fastify-plugin';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n * Note: req.cookies requires @fastify/cookie to be registered.\n * We cast req to any to avoid hard dependency on @fastify/cookie types.\n */\nconst getStorageLocale = (req: FastifyRequest): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => (req as any).cookies?.[name],\n getHeader: (name: string) => req.headers?.[name] as string | undefined,\n });\n\nconst appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\n// Module augmentation to type the request decoration\ndeclare module 'fastify' {\n interface FastifyRequest {\n intlayer: {\n locale: Locale;\n defaultLocale: Locale;\n locale_storage?: Locale;\n locale_detected?: Locale;\n t: <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ) => T;\n getIntlayer: typeof getIntlayerFunction;\n getDictionary: typeof getDictionaryFunction;\n };\n }\n}\n\nexport const translateFunction =\n (req: FastifyRequest) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n // Access the decorated state from the request\n const { locale: currentLocale, defaultLocale } = req.intlayer;\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[\n targetLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Fastify Plugin to detect locale and load it into request context\n */\nconst fastifyIntlayer: FastifyPluginAsync = async (fastify, _opts) => {\n // Decorate the request object to ensure types are stable.\n // We use 'null as any' to bypass the initial type check, knowing\n // the preHandler will populate it before any route handler runs.\n if (!fastify.hasRequestDecorator('intlayer')) {\n fastify.decorateRequest('intlayer', null as any);\n }\n\n fastify.addHook('preHandler', (req, _reply, done) => {\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeFromStorage = getStorageLocale(req);\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Copy all headers from the request to negotiatorHeaders\n if (req && typeof req.headers === 'object') {\n for (const key in req.headers) {\n const val = req.headers[key];\n if (typeof val === 'string') {\n negotiatorHeaders[key] = val;\n } else if (Array.isArray(val)) {\n // Handle array headers (unlikely for accept-language but possible in Fastify)\n negotiatorHeaders[key] = val.join(',');\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n const defaultLocale = internationalization.defaultLocale;\n\n // Helper functions bound to the current request context\n const getIntlayerWrapped: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionaryWrapped: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n // Assign data to request decoration\n req.intlayer = {\n locale_storage: localeFromStorage,\n locale_detected: localeDetected,\n locale,\n defaultLocale,\n getIntlayer: getIntlayerWrapped,\n getDictionary: getDictionaryWrapped,\n t: undefined as unknown as any, // Placeholder\n };\n\n // Now bind t using the updated req\n const t = translateFunction(req);\n req.intlayer.t = t;\n\n // Run CLS context\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayerWrapped);\n appNamespace.set('getDictionary', getDictionaryWrapped);\n\n done();\n });\n });\n};\n\n// Export as a Fastify Plugin (wrapped in fp to skip encapsulation)\nexport const intlayer = fp(fastifyIntlayer, {\n name: 'fastify-intlayer',\n fastify: '5.x',\n});\n\n// Global exports that rely on CLS (Async Local Storage)\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"fastify-intlayer\" is not supported in your environment outside of a request context or proper setup. Use req.intlayer.t instead.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.'\n );\n }\n\n if (typeof appNamespace.get('getIntlayer') !== 'function') {\n throw new Error(\n 'Context not found. Ensure you are inside a request handling flow.'\n );\n }\n\n return appNamespace.get('getIntlayer')(...args);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n return getIntlayerFunction(...args);\n }\n};\n\nexport const getDictionary: typeof getDictionaryFunction = (...args) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.'\n );\n }\n\n if (typeof appNamespace.get('getDictionary') !== 'function') {\n throw new Error(\n 'Context not found. Ensure you are inside a request handling flow.'\n );\n }\n\n return appNamespace.get('getDictionary')(...args);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n return getDictionaryFunction(...args);\n }\n};\n"],"mappings":"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"}
@@ -0,0 +1,104 @@
1
+ import { prepareIntlayer } from "@intlayer/chokidar";
2
+ import { getConfiguration } from "@intlayer/config";
3
+ import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1, getLocaleFromStorage, getTranslation, localeDetector } from "@intlayer/core";
4
+ import { createNamespace } from "cls-hooked";
5
+ import fp from "fastify-plugin";
6
+
7
+ //#region src/index.ts
8
+ const configuration = getConfiguration();
9
+ const { internationalization } = configuration;
10
+ /**
11
+ * Retrieves the locale from storage (cookies, headers).
12
+ * Note: req.cookies requires @fastify/cookie to be registered.
13
+ * We cast req to any to avoid hard dependency on @fastify/cookie types.
14
+ */
15
+ const getStorageLocale = (req) => getLocaleFromStorage({
16
+ getCookie: (name) => req.cookies?.[name],
17
+ getHeader: (name) => req.headers?.[name]
18
+ });
19
+ const appNamespace = createNamespace("app");
20
+ prepareIntlayer(configuration);
21
+ const translateFunction = (req) => (content, locale) => {
22
+ const { locale: currentLocale, defaultLocale } = req.intlayer;
23
+ const targetLocale = locale ?? currentLocale;
24
+ if (typeof content === "undefined") return "";
25
+ if (typeof content === "string") return content;
26
+ if (typeof content?.[targetLocale] === "undefined") if (typeof content?.[defaultLocale] === "undefined") return content;
27
+ else return getTranslation(content, defaultLocale);
28
+ return getTranslation(content, targetLocale);
29
+ };
30
+ /**
31
+ * Fastify Plugin to detect locale and load it into request context
32
+ */
33
+ const fastifyIntlayer = async (fastify, _opts) => {
34
+ if (!fastify.hasRequestDecorator("intlayer")) fastify.decorateRequest("intlayer", null);
35
+ fastify.addHook("preHandler", (req, _reply, done) => {
36
+ const localeFromStorage = getStorageLocale(req);
37
+ const negotiatorHeaders = {};
38
+ if (req && typeof req.headers === "object") for (const key in req.headers) {
39
+ const val = req.headers[key];
40
+ if (typeof val === "string") negotiatorHeaders[key] = val;
41
+ else if (Array.isArray(val)) negotiatorHeaders[key] = val.join(",");
42
+ }
43
+ const localeDetected = localeDetector(negotiatorHeaders, internationalization.locales, internationalization.defaultLocale);
44
+ const locale = localeFromStorage ?? localeDetected;
45
+ const defaultLocale = internationalization.defaultLocale;
46
+ const getIntlayerWrapped = (key, localeArg = localeDetected, ...props) => getIntlayer$1(key, localeArg, ...props);
47
+ const getDictionaryWrapped = (key, localeArg = localeDetected, ...props) => getDictionary$1(key, localeArg, ...props);
48
+ req.intlayer = {
49
+ locale_storage: localeFromStorage,
50
+ locale_detected: localeDetected,
51
+ locale,
52
+ defaultLocale,
53
+ getIntlayer: getIntlayerWrapped,
54
+ getDictionary: getDictionaryWrapped,
55
+ t: void 0
56
+ };
57
+ const t$1 = translateFunction(req);
58
+ req.intlayer.t = t$1;
59
+ appNamespace.run(() => {
60
+ appNamespace.set("t", t$1);
61
+ appNamespace.set("getIntlayer", getIntlayerWrapped);
62
+ appNamespace.set("getDictionary", getDictionaryWrapped);
63
+ done();
64
+ });
65
+ });
66
+ };
67
+ const intlayer = fp(fastifyIntlayer, {
68
+ name: "fastify-intlayer",
69
+ fastify: "5.x"
70
+ });
71
+ const t = (content, locale) => {
72
+ try {
73
+ if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.");
74
+ if (typeof appNamespace.get("t") !== "function") throw new Error("Using the import { t } from \"fastify-intlayer\" is not supported in your environment outside of a request context or proper setup. Use req.intlayer.t instead.");
75
+ return appNamespace.get("t")(content, locale);
76
+ } catch (error) {
77
+ console.error(error.message);
78
+ return getTranslation(content, locale ?? internationalization.defaultLocale);
79
+ }
80
+ };
81
+ const getIntlayer = (...args) => {
82
+ try {
83
+ if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.");
84
+ if (typeof appNamespace.get("getIntlayer") !== "function") throw new Error("Context not found. Ensure you are inside a request handling flow.");
85
+ return appNamespace.get("getIntlayer")(...args);
86
+ } catch (error) {
87
+ console.error(error.message);
88
+ return getIntlayer$1(...args);
89
+ }
90
+ };
91
+ const getDictionary = (...args) => {
92
+ try {
93
+ if (typeof appNamespace === "undefined") throw new Error("Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.");
94
+ if (typeof appNamespace.get("getDictionary") !== "function") throw new Error("Context not found. Ensure you are inside a request handling flow.");
95
+ return appNamespace.get("getDictionary")(...args);
96
+ } catch (error) {
97
+ console.error(error.message);
98
+ return getDictionary$1(...args);
99
+ }
100
+ };
101
+
102
+ //#endregion
103
+ export { getDictionary, getIntlayer, intlayer, t, translateFunction };
104
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":["fastifyIntlayer: FastifyPluginAsync","negotiatorHeaders: Record<string, string>","getIntlayerWrapped: typeof getIntlayerFunction","getIntlayerFunction","getDictionaryWrapped: typeof getDictionaryFunction","getDictionaryFunction","t","getIntlayer: typeof getIntlayerFunction","getDictionary: typeof getDictionaryFunction"],"sources":["../../src/index.ts"],"sourcesContent":["import { prepareIntlayer } from '@intlayer/chokidar';\nimport { getConfiguration } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getLocaleFromStorage,\n getTranslation,\n localeDetector,\n} from '@intlayer/core';\nimport type { Locale, StrictModeLocaleMap } from '@intlayer/types';\nimport { createNamespace } from 'cls-hooked';\nimport type { FastifyPluginAsync, FastifyRequest } from 'fastify';\nimport fp from 'fastify-plugin';\n\nconst configuration = getConfiguration();\nconst { internationalization } = configuration;\n\n/**\n * Retrieves the locale from storage (cookies, headers).\n * Note: req.cookies requires @fastify/cookie to be registered.\n * We cast req to any to avoid hard dependency on @fastify/cookie types.\n */\nconst getStorageLocale = (req: FastifyRequest): Locale | undefined =>\n getLocaleFromStorage({\n getCookie: (name: string) => (req as any).cookies?.[name],\n getHeader: (name: string) => req.headers?.[name] as string | undefined,\n });\n\nconst appNamespace = createNamespace('app');\n\nprepareIntlayer(configuration);\n\n// Module augmentation to type the request decoration\ndeclare module 'fastify' {\n interface FastifyRequest {\n intlayer: {\n locale: Locale;\n defaultLocale: Locale;\n locale_storage?: Locale;\n locale_detected?: Locale;\n t: <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ) => T;\n getIntlayer: typeof getIntlayerFunction;\n getDictionary: typeof getDictionaryFunction;\n };\n }\n}\n\nexport const translateFunction =\n (req: FastifyRequest) =>\n <T extends string>(\n content: StrictModeLocaleMap<T> | string,\n locale?: Locale\n ): T => {\n // Access the decorated state from the request\n const { locale: currentLocale, defaultLocale } = req.intlayer;\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[\n targetLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof StrictModeLocaleMap<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Fastify Plugin to detect locale and load it into request context\n */\nconst fastifyIntlayer: FastifyPluginAsync = async (fastify, _opts) => {\n // Decorate the request object to ensure types are stable.\n // We use 'null as any' to bypass the initial type check, knowing\n // the preHandler will populate it before any route handler runs.\n if (!fastify.hasRequestDecorator('intlayer')) {\n fastify.decorateRequest('intlayer', null as any);\n }\n\n fastify.addHook('preHandler', (req, _reply, done) => {\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeFromStorage = getStorageLocale(req);\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // Copy all headers from the request to negotiatorHeaders\n if (req && typeof req.headers === 'object') {\n for (const key in req.headers) {\n const val = req.headers[key];\n if (typeof val === 'string') {\n negotiatorHeaders[key] = val;\n } else if (Array.isArray(val)) {\n // Handle array headers (unlikely for accept-language but possible in Fastify)\n negotiatorHeaders[key] = val.join(',');\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n const locale = localeFromStorage ?? localeDetected;\n const defaultLocale = internationalization.defaultLocale;\n\n // Helper functions bound to the current request context\n const getIntlayerWrapped: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionaryWrapped: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n // Assign data to request decoration\n req.intlayer = {\n locale_storage: localeFromStorage,\n locale_detected: localeDetected,\n locale,\n defaultLocale,\n getIntlayer: getIntlayerWrapped,\n getDictionary: getDictionaryWrapped,\n t: undefined as unknown as any, // Placeholder\n };\n\n // Now bind t using the updated req\n const t = translateFunction(req);\n req.intlayer.t = t;\n\n // Run CLS context\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayerWrapped);\n appNamespace.set('getDictionary', getDictionaryWrapped);\n\n done();\n });\n });\n};\n\n// Export as a Fastify Plugin (wrapped in fp to skip encapsulation)\nexport const intlayer = fp(fastifyIntlayer, {\n name: 'fastify-intlayer',\n fastify: '5.x',\n});\n\n// Global exports that rely on CLS (Async Local Storage)\nexport const t = <Content = string>(\n content: StrictModeLocaleMap<Content>,\n locale?: Locale\n): Content => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.'\n );\n }\n\n if (typeof appNamespace.get('t') !== 'function') {\n throw new Error(\n 'Using the import { t } from \"fastify-intlayer\" is not supported in your environment outside of a request context or proper setup. Use req.intlayer.t instead.'\n );\n }\n\n return appNamespace.get('t')(content, locale);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n\n return getTranslation(\n content,\n locale ?? internationalization.defaultLocale\n );\n }\n};\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.'\n );\n }\n\n if (typeof appNamespace.get('getIntlayer') !== 'function') {\n throw new Error(\n 'Context not found. Ensure you are inside a request handling flow.'\n );\n }\n\n return appNamespace.get('getIntlayer')(...args);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n return getIntlayerFunction(...args);\n }\n};\n\nexport const getDictionary: typeof getDictionaryFunction = (...args) => {\n try {\n if (typeof appNamespace === 'undefined') {\n throw new Error(\n 'Intlayer is not initialized. Register the plugin `fastify.register(intlayer)`.'\n );\n }\n\n if (typeof appNamespace.get('getDictionary') !== 'function') {\n throw new Error(\n 'Context not found. Ensure you are inside a request handling flow.'\n );\n }\n\n return appNamespace.get('getDictionary')(...args);\n } catch (error) {\n if (process.env.NODE_ENV === 'development') {\n console.error((error as Error).message);\n }\n return getDictionaryFunction(...args);\n }\n};\n"],"mappings":"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"}
@@ -0,0 +1,26 @@
1
+ import { getDictionary as getDictionary$1, getIntlayer as getIntlayer$1 } from "@intlayer/core";
2
+ import { Locale, StrictModeLocaleMap } from "@intlayer/types";
3
+ import { FastifyPluginAsync, FastifyRequest } from "fastify";
4
+
5
+ //#region src/index.d.ts
6
+ declare module 'fastify' {
7
+ interface FastifyRequest {
8
+ intlayer: {
9
+ locale: Locale;
10
+ defaultLocale: Locale;
11
+ locale_storage?: Locale;
12
+ locale_detected?: Locale;
13
+ t: <T extends string>(content: StrictModeLocaleMap<T> | string, locale?: Locale) => T;
14
+ getIntlayer: typeof getIntlayer$1;
15
+ getDictionary: typeof getDictionary$1;
16
+ };
17
+ }
18
+ }
19
+ declare const translateFunction: (req: FastifyRequest) => <T extends string>(content: StrictModeLocaleMap<T> | string, locale?: Locale) => T;
20
+ declare const intlayer: FastifyPluginAsync;
21
+ declare const t: <Content = string>(content: StrictModeLocaleMap<Content>, locale?: Locale) => Content;
22
+ declare const getIntlayer: typeof getIntlayer$1;
23
+ declare const getDictionary: typeof getDictionary$1;
24
+ //#endregion
25
+ export { getDictionary, getIntlayer, intlayer, t, translateFunction };
26
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;IAWkE,QAAA,EAAA;MAAA,MAAA,EAyBpD,MAzBoD;MAAA,aAAA,EA0B7C,MA1B6C;MAyBpD,cAAA,CAAA,EAES,MAFT;MACO,eAAA,CAAA,EAEG,MAFH;MACE,CAAA,EAAA,CAAA,UAAA,MAAA,CAAA,CAAA,OAAA,EAGN,mBAHM,CAGc,CAHd,CAAA,GAAA,MAAA,EAAA,MAAA,CAAA,EAIN,MAJM,EAAA,GAKZ,CALY;MACC,WAAA,EAAA,OAKE,aALF;MAEa,aAAA,EAAA,OAIT,eAJS;IAApB,CAAA;EACA;;AAES,cAMb,iBANa,EAAA,CAAA,GAAA,EAOlB,cAPkB,EAAA,GAAA,CAAA,UAAA,MAAA,CAAA,CAAA,OAAA,EASb,mBATa,CASO,CATP,CAAA,GAAA,MAAA,EAAA,MAAA,CAAA,EAUb,MAVa,EAAA,GAWrB,CAXqB;AACE,cA0Hf,QA1He,EA0HP,kBA1HO;AAAqB,cAgIpC,CAhIoC,EAAA,CAAA,UAAA,MAAA,CAAA,CAAA,OAAA,EAiItC,mBAjIsC,CAiIlB,OAjIkB,CAAA,EAAA,MAAA,CAAA,EAkItC,MAlIsC,EAAA,GAmI9C,OAnI8C;AAAA,cA8JpC,WA9JoC,EAAA,OA8JhB,aA9JgB;AAAA,cAqLpC,aArLoC,EAAA,OAqLd,eArLc"}
package/package.json ADDED
@@ -0,0 +1,105 @@
1
+ {
2
+ "name": "fastify-intlayer",
3
+ "version": "7.5.10",
4
+ "private": false,
5
+ "description": "Manage internationalization i18n in a simple way for Fastify applications.",
6
+ "keywords": [
7
+ "intlayer",
8
+ "fastify",
9
+ "backend",
10
+ "i18n",
11
+ "api",
12
+ "application",
13
+ "typescript",
14
+ "javascript",
15
+ "plugin"
16
+ ],
17
+ "homepage": "https://intlayer.org",
18
+ "bugs": {
19
+ "url": "https://github.com/aymericzip/intlayer/issues"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/aymericzip/intlayer.git"
24
+ },
25
+ "license": "Apache-2.0",
26
+ "author": {
27
+ "name": "Aymeric PINEAU",
28
+ "url": "https://github.com/aymericzip"
29
+ },
30
+ "contributors": [
31
+ {
32
+ "name": "Aymeric Pineau",
33
+ "email": "ay.pineau@gmail.com",
34
+ "url": "https://github.com/aymericzip"
35
+ }
36
+ ],
37
+ "sideEffects": false,
38
+ "exports": {
39
+ ".": {
40
+ "types": "./dist/types/index.d.ts",
41
+ "require": "./dist/cjs/index.cjs",
42
+ "import": "./dist/esm/index.mjs"
43
+ },
44
+ "./package.json": "./package.json"
45
+ },
46
+ "main": "dist/cjs/index.cjs",
47
+ "module": "dist/esm/index.mjs",
48
+ "types": "dist/types/index.d.ts",
49
+ "typesVersions": {
50
+ "*": {
51
+ "package.json": [
52
+ "./package.json"
53
+ ]
54
+ }
55
+ },
56
+ "files": [
57
+ "./dist",
58
+ "./package.json"
59
+ ],
60
+ "scripts": {
61
+ "_prepublish": "cp -f ../../README.md ./README.md",
62
+ "build": "tsdown --config tsdown.config.ts",
63
+ "build:ci": "tsdown --config tsdown.config.ts",
64
+ "clean": "rimraf ./dist .turbo",
65
+ "dev": "tsdown --config tsdown.config.ts --watch",
66
+ "format": "biome format . --check",
67
+ "format:fix": "biome format --write .",
68
+ "lint": "biome lint .",
69
+ "lint:fix": "biome lint --write .",
70
+ "prepublish": "echo prepublish temporally disabled to avoid rewrite readme",
71
+ "publish": "bun publish || true",
72
+ "publish:canary": "bun publish --access public --tag canary || true",
73
+ "publish:latest": "bun publish --access public --tag latest || true",
74
+ "test": "vitest run",
75
+ "test:watch": "vitest",
76
+ "typecheck": "tsc --noEmit --project tsconfig.types.json"
77
+ },
78
+ "dependencies": {
79
+ "@intlayer/chokidar": "7.5.10",
80
+ "@intlayer/config": "7.5.10",
81
+ "@intlayer/core": "7.5.10",
82
+ "@intlayer/types": "7.5.10",
83
+ "cls-hooked": "4.2.2",
84
+ "fastify-plugin": "^5.0.0",
85
+ "intlayer": "7.5.10"
86
+ },
87
+ "devDependencies": {
88
+ "@types/cls-hooked": "4.3.9",
89
+ "@types/node": "25.0.3",
90
+ "@utils/ts-config": "1.0.4",
91
+ "@utils/ts-config-types": "1.0.4",
92
+ "@utils/tsdown-config": "1.0.4",
93
+ "fastify": "^5.0.0",
94
+ "rimraf": "6.1.2",
95
+ "tsdown": "0.18.2",
96
+ "typescript": "5.9.3",
97
+ "vitest": "4.0.16"
98
+ },
99
+ "engines": {
100
+ "node": ">=14.18"
101
+ },
102
+ "bug": {
103
+ "url": "https://github.com/aymericzip/intlayer/issues"
104
+ }
105
+ }