express-intlayer 4.0.0 → 4.0.2

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.
Files changed (2) hide show
  1. package/README.md +144 -35
  2. package/package.json +10 -10
package/README.md CHANGED
@@ -17,9 +17,11 @@
17
17
  </a>
18
18
  </div>
19
19
 
20
- # express-intlayer
20
+ # express-intlayer: JavaScript Package to internationalize (i18n) an Express.js application
21
21
 
22
- `express-intlayer` is a powerful internationalization (i18n) middleware for Express applications, designed to make your backend services globally accessible by providing localized responses based on the client's preferences.
22
+ **Intlayer** is a suite of packages designed specifically for JavaScript developers. It is compatible with frameworks like React, Next.js, and Express.js.
23
+
24
+ **The `express-intlayer` package** allows you to internationalize your Express.js application. It provides a middleware to detect the user's preferred locale, and returns the appropriate dictionary for the user.
23
25
 
24
26
  ## Why Internationalize Your Backend?
25
27
 
@@ -39,32 +41,36 @@ Internationalizing your backend is essential for serving a global audience effec
39
41
 
40
42
  By internationalizing the backend, your application not only respects cultural differences but also aligns better with global market needs, making it a key step in scaling your services worldwide.
41
43
 
42
- ## Getting Started
44
+ ## Why to integrate Intlayer?
45
+
46
+ - **Type-Safe Environment**: Leverage TypeScript to ensure all your content definitions are precise and error-free.
43
47
 
44
- ### Installation
48
+ ## Installation
45
49
 
46
- To begin using `express-intlayer`, install the package using npm:
50
+ Install the necessary package using your preferred package manager:
47
51
 
48
52
  ```bash
49
- npm install intlayer express-intlayer
53
+ npm install express-intlayer
50
54
  ```
51
55
 
52
- ### Setup
56
+ ```bash
57
+ yarn add express-intlayer
58
+ ```
53
59
 
54
- Configure the internationalization settings by creating an `intlayer.config.ts` in your project root:
60
+ ```bash
61
+ pnpm add express-intlayer
62
+ ```
55
63
 
56
- ```typescript
57
- // intlayer.config.ts
64
+ ### Configure Intlayer
65
+
66
+ Intlayer provides a configuration file to set up your project. Place this file in the root of your project.
67
+
68
+ ```typescript fileName="intlayer.config.ts" codeFormat="typescript"
58
69
  import { Locales, type IntlayerConfig } from "intlayer";
59
70
 
60
71
  const config: IntlayerConfig = {
61
72
  internationalization: {
62
- locales: [
63
- Locales.ENGLISH,
64
- Locales.FRENCH,
65
- Locales.SPANISH_MEXICO,
66
- Locales.SPANISH_SPAIN,
67
- ],
73
+ locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
68
74
  defaultLocale: Locales.ENGLISH,
69
75
  },
70
76
  };
@@ -72,12 +78,41 @@ const config: IntlayerConfig = {
72
78
  export default config;
73
79
  ```
74
80
 
75
- ### Express Application Setup
81
+ ```javascript fileName="intlayer.config.mjs" codeFormat="esm"
82
+ import { Locales } from "intlayer";
83
+
84
+ /** @type {import('intlayer').IntlayerConfig} */
85
+ const config = {
86
+ internationalization: {
87
+ locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
88
+ defaultLocale: Locales.ENGLISH,
89
+ },
90
+ };
91
+
92
+ export default config;
93
+ ```
94
+
95
+ ```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
96
+ const { Locales } = require("intlayer");
97
+
98
+ /** @type {import('intlayer').IntlayerConfig} */
99
+ const config = {
100
+ internationalization: {
101
+ locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
102
+ defaultLocale: Locales.ENGLISH,
103
+ },
104
+ };
105
+
106
+ module.exports = config;
107
+ ```
108
+
109
+ > For a complete list of available parameters, refer to the [configuration documentation](https://github.com/aymericzip/intlayer/blob/main/docs/en/configuration.md).
110
+
111
+ ## Example of usage
76
112
 
77
113
  Setup your Express application to use `express-intlayer`:
78
114
 
79
- ```typescript
80
- // src/index.ts
115
+ ```typescript fileName="src/index.ts" codeFormat="typescript"
81
116
  import express, { type Express } from "express";
82
117
  import { intlayer, t } from "express-intlayer";
83
118
 
@@ -98,50 +133,124 @@ app.get("/", (_req, res) => {
98
133
  );
99
134
  });
100
135
 
101
- app.get("/error", (_req, res) => {
102
- res.status(500).send(
136
+ // Start server
137
+ app.listen(3000, () => console.log(`Listening on port 3000`));
138
+ ```
139
+
140
+ ```javascript fileName="src/index.mjs" codeFormat="esm"
141
+ import express from "express";
142
+ import { intlayer, t } from "express-intlayer";
143
+
144
+ const app = express();
145
+
146
+ // Load internationalization request handler
147
+ app.use(intlayer());
148
+
149
+ // Routes
150
+ app.get("/", (_req, res) => {
151
+ res.send(
103
152
  t({
104
- en: "Example of returned error content in English",
105
- fr: "Exemple de contenu d'erreur renvoyé en français",
106
- "es-ES": "Ejemplo de contenido de error devuelto en español (España)",
107
- "es-MX": "Ejemplo de contenido de error devuelto en español (México)",
153
+ en: "Example of returned content in English",
154
+ fr: "Exemple de contenu renvoyé en français",
155
+ "es-MX": "Ejemplo de contenido devuelto en español (México)",
156
+ "es-ES": "Ejemplo de contenido devuelto en español (España)",
108
157
  })
109
158
  );
110
159
  });
111
160
 
112
161
  // Start server
113
- app.listen(3000, () => {
114
- console.info(`Listening on port 3000`);
162
+ app.listen(3000, () => console.log(`Listening on port 3000`));
163
+ ```
164
+
165
+ ```javascript fileName="src/index.cjs" codeFormat="commonjs"
166
+ const express = require("express");
167
+ const { intlayer, t } = require("express-intlayer");
168
+
169
+ const app = express();
170
+
171
+ // Load internationalization request handler
172
+ app.use(intlayer());
173
+
174
+ // Routes
175
+ app.get("/", (_req, res) => {
176
+ res.send(
177
+ t({
178
+ en: "Example of returned content in English",
179
+ fr: "Exemple de contenu renvoyé en français",
180
+ "es-MX": "Ejemplo de contenido devuelto en español (México)",
181
+ "es-ES": "Ejemplo de contenido devuelto en español (España)",
182
+ })
183
+ );
115
184
  });
185
+
186
+ // Start server
187
+ app.listen(3000, () => console.log(`Listening on port 3000`));
116
188
  ```
117
189
 
118
190
  ### Compatibility
119
191
 
120
192
  `express-intlayer` is fully compatible with:
121
193
 
122
- - `react-intlayer` for React applications
123
- - `next-intlayer` for Next.js applications
194
+ - [`react-intlayer`](https://github.com/aymericzip/intlayer/blob/main/docs/en/packages/react-intlayer/index.md) for React applications
195
+ - [`next-intlayer`](https://github.com/aymericzip/intlayer/blob/main/docs/en/packages/next-intlayer/index.md) for Next.js applications
196
+ - [`vite-intlayer`](https://github.com/aymericzip/intlayer/blob/main/docs/en/packages/vite-intlayer/index.md) for Vite applications
124
197
 
125
198
  It also works seamlessly with any internationalization solution across various environments, including browsers and API requests. You can customize the middleware to detect locale through headers or cookies:
126
199
 
127
- ```typescript
200
+ ```typescript fileName="intlayer.config.ts" codeFormat="typescript"
128
201
  import { Locales, type IntlayerConfig } from "intlayer";
129
202
 
130
203
  const config: IntlayerConfig = {
131
- // Other configuration options
204
+ // ... Other configuration options
132
205
  middleware: {
133
206
  headerName: "my-locale-header",
134
207
  cookieName: "my-locale-cookie",
135
208
  },
136
209
  };
210
+
211
+ export default config;
212
+ ```
213
+
214
+ ```javascript fileName="intlayer.config.mjs" codeFormat="esm"
215
+ import { Locales } from "intlayer";
216
+
217
+ /** @type {import('intlayer').IntlayerConfig} */
218
+ const config = {
219
+ // ... Other configuration options
220
+ middleware: {
221
+ headerName: "my-locale-header",
222
+ cookieName: "my-locale-cookie",
223
+ },
224
+ };
225
+
226
+ export default config;
227
+ ```
228
+
229
+ ```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
230
+ const { Locales } = require("intlayer");
231
+
232
+ /** @type {import('intlayer').IntlayerConfig} */
233
+ const config = {
234
+ // ... Other configuration options
235
+ middleware: {
236
+ headerName: "my-locale-header",
237
+ cookieName: "my-locale-cookie",
238
+ },
239
+ };
240
+
241
+ module.exports = config;
137
242
  ```
138
243
 
139
244
  By default, `express-intlayer` will interpret the `Accept-Language` header to determine the client's preferred language.
140
245
 
141
- > For more information on configuration and advanced topics, visit our [documentation](https://github.com/aymericzip/intlayer/blob/main/docs/en/configuration.md).
246
+ ## Functions provided by `express-intlayer` package
247
+
248
+ - [`t()`](https://github.com/aymericzip/intlayer/blob/main/docs/en/packages/express-intlayer/t.md)
142
249
 
143
- ## Powered by TypeScript
250
+ ## Read about Intlayer
144
251
 
145
- `express-intlayer` leverages the robust capabilities of TypeScript to enhance the internationalization process. TypeScript's static typing ensures that every translation key is accounted for, reducing the risk of missing translations and improving maintainability.
252
+ - [Intlayer Website](https://intlayer.org)
253
+ - [Intlayer Documentation](https://intlayer.org/docs)
254
+ - [Intlayer GitHub](https://github.com/aymericzip/intlayer)
146
255
 
147
- > Ensure the generated types (by default at ./types/intlayer.d.ts) are included in your tsconfig.json file.
256
+ - [Ask your questions to our smart documentation](https://intlayer.org/docs/chat)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-intlayer",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "private": false,
5
5
  "description": "Manage internationalization in a simple way for express application.",
6
6
  "keywords": [
@@ -61,10 +61,10 @@
61
61
  ],
62
62
  "dependencies": {
63
63
  "cls-hooked": "^4.2.2",
64
- "@intlayer/chokidar": "4.0.0",
65
- "@intlayer/config": "4.0.0",
66
- "@intlayer/core": "4.0.0",
67
- "intlayer": "4.0.0"
64
+ "@intlayer/chokidar": "4.0.2",
65
+ "@intlayer/config": "4.0.2",
66
+ "intlayer": "4.0.2",
67
+ "@intlayer/core": "4.0.2"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@changesets/changelog-github": "0.5.0",
@@ -82,15 +82,15 @@
82
82
  "tsup": "^8.3.5",
83
83
  "typescript": "^5.7.3",
84
84
  "@utils/eslint-config": "1.0.4",
85
- "@utils/ts-config": "1.0.4",
86
85
  "@utils/ts-config-types": "1.0.4",
86
+ "@utils/ts-config": "1.0.4",
87
87
  "@utils/tsup-config": "1.0.4"
88
88
  },
89
89
  "peerDependencies": {
90
- "@intlayer/config": "4.0.0",
91
- "@intlayer/core": "4.0.0",
92
- "@intlayer/chokidar": "4.0.0",
93
- "intlayer": "^4.0.0"
90
+ "@intlayer/chokidar": "4.0.2",
91
+ "@intlayer/config": "4.0.2",
92
+ "@intlayer/core": "4.0.2",
93
+ "intlayer": "^4.0.2"
94
94
  },
95
95
  "engines": {
96
96
  "node": ">=14.18"