intlayer
Version:
Manage internationalization i18n in a simple way, through TypeScript, declaration file, declare your multilingual content every where in your code.
300 lines (225 loc) • 10.3 kB
Markdown
<div align="center">
<a href="https://intlayer.org">
<img src="https://raw.githubusercontent.com/aymericzip/intlayer/572ae9c9acafb74307b81530c1931a8e98990aef/docs/assets/logo.png" width="500" alt="intlayer" />
</a>
</div>
<div align="center">
<a href="https://www.npmjs.com/package/intlayer">
<img alt="npm" src="https://img.shields.io/npm/v/intlayer.svg?labelColor=49516F&color=8994BC" />
</a>
<a href="https://npmjs.org/package/intlayer">
<img alt="downloads" src="https://badgen.net/npm/dm/intlayer?labelColor=49516F&color=8994BC" />
</a>
<a href="https://npmjs.org/package/intlayer">
<img alt="types included" src="https://badgen.net/npm/types/intlayer?labelColor=49516F&color=8994BC"
/>
</div>
**Intlayer** is a suite of packages designed specifically for JavaScript developers. It is compatible with frameworks like React, Next.js, and Express.js.
**The `intlayer` package** allows you to declare your content anywhere in your code. It converts multilingual content declarations into structured dictionaries that integrate seamlessly into your application. With TypeScript, **Intlayer** enhances your development by providing stronger, more efficient tools.
- **JavaScript-Powered Content Management**: Harness the flexibility of JavaScript to define and manage your content efficiently.
- **Type-Safe Environment**: Leverage TypeScript to ensure all your content definitions are precise and error-free.
- **Integrated Content Files**: Keep your translations close to their respective components, enhancing maintainability and clarity.
Install the necessary package using your preferred package manager:
```bash packageManager="npm"
npm install intlayer
```
```bash packageManager="pnpm"
pnpm add intlayer
```
```bash packageManager="yarn"
yarn add intlayer
```
Intlayer provides a configuration file to set up your project. Place this file in the root of your project.
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
internationalization: {
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
defaultLocale: Locales.ENGLISH,
},
};
export default config;
```
> For a complete list of available parameters, refer to the [configuration documentation](https://intlayer.org/doc/concept/configuration).
With Intlayer, you can declare your content in a structured way anywhere in your codebase.
By default, Intlayer scans for files with the extension `.content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx}`.
> can modify the default extension by setting the `contentDir` property in the [configuration file](https://intlayer.org/doc/concept/configuration).
```bash codeFormat="typescript"
.
├── intlayer.config.ts
└── src
├── ClientComponent
│ ├── index.content.ts
│ └── index.tsx
└── ServerComponent
├── index.content.ts
└── index.tsx
```
Here’s an example of content declaration:
```tsx filePath="src/ClientComponent/index.content.ts" codeFormat="typescript"
import { t, type Dictionary } from "intlayer";
const clientComponentContent = {
key: "client-component",
content: {
myTranslatedContent: t({
en: "Hello World",
fr: "Bonjour le monde",
es: "Hola Mundo",
}),
numberOfCar: enu({
"<-1": "Less than minus one car",
"-1": "Minus one car",
"0": "No cars",
"1": "One car",
">5": "Some cars",
">19": "Many cars",
}),
},
} satisfies Dictionary;
export default clientComponentContent;
```
You can build your dictionaries using the [intlayer-cli](https://github.com/aymericzip/intlayer/blob/main/packages/intlayer-cli/readme.md).
```bash packageManager="npm"
npx intlayer build
```
```bash packageManager="yarn"
yarn intlayer build
```
```bash packageManager="pnpm"
pnpm intlayer build
```
This command scans all `*.content.*` files, compiles them, and writes the results to the directory specified in your **`intlayer.config.ts`** (by default, `./.intlayer`).
A typical output might look like:
```bash
.
└── .intlayer
├── dictionary
│ ├── client-component.json
│ └── server-component.json
├── main
│ ├── dictionary.cjs
│ └── dictionary.mjs
└── types
├── intlayer.d.ts
├── client-component.d.ts
└── server-component.d.ts
```
Intlayer can be configured to build dictionaries for [i18next](https://www.i18next.com/). For that you need to add the following configuration to your `intlayer.config.ts` file:
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
/* ... */
content: {
// Tells Intlayer to generate message files for i18next
dictionaryOutput: ["i18next"],
// The directory where Intlayer will write your message JSON files
i18nextResourcesDir: "./i18next/resources",
},
};
```
> For a complete list of available parameters, refer to the [configuration documentation](https://intlayer.org/doc/concept/configuration).
Output:
```bash
.
└── i18next
└── resources
├── en
│ ├── client-component.json
│ └── server-component.json
├── es
│ ├── client-component.json
│ └── server-component.json
└── fr
├── client-component.json
└── server-component.json
```
For example, the **en/client-component.json** might look like:
```json filePath="intlayer/dictionary/en/client-component.json"
{
"myTranslatedContent": "Hello World",
"zero_numberOfCar": "No cars",
"one_numberOfCar": "One car",
"two_numberOfCar": "Two cars",
"other_numberOfCar": "Some cars"
}
```
Intlayer can be configured to build dictionaries for [i18next](https://www.i18next.com/) or [next-intl](https://github.com/formatjs/react-intl/tree/main/packages/next-intl). For that you need to add the following configuration to your `intlayer.config.ts` file:
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
import { Locales, type IntlayerConfig } from "intlayer";
const config: IntlayerConfig = {
/* ... */
content: {
// Tells Intlayer to generate message files for i18next
dictionaryOutput: ["next-intl"],
// The directory where Intlayer will write your message JSON files
nextIntlMessagesDir: "./i18next/messages",
},
};
```
> For a complete list of available parameters, refer to the [configuration documentation](https://intlayer.org/doc/concept/configuration).
Output:
```bash
.
└── intl
└── messages
├── en
│ ├── client-component.json
│ └── server-component.json
├── es
│ ├── client-component.json
│ └── server-component.json
└── fr
├── client-component.json
└── server-component.json
```
For example, the **en/client-component.json** might look like:
```json filePath="intlayer/dictionary/en/client-component.json"
{
"myTranslatedContent": "Hello World",
"zero_numberOfCar": "No cars",
"one_numberOfCar": "One car",
"two_numberOfCar": "Two cars",
"other_numberOfCar": "Some cars"
}
```
Intlayer provides a CLI tool to:
- audit your content declarations and complete missing translations
- build dictionaries from your content declarations
- push and pull distant dictionaries from your CMS to your locale project
Consult [intlayer-cli](https://intlayer.org/doc/concept/cli) for more information.
One your content declared, you can consume your Intlayer dictionaries in your application.
Intlayer is available as a package for your application.
To use Intlayer in your React application, you can use [react-intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/en/packages/react-intlayer/index.md).
To use Intlayer in your Next.js application, you can use [next-intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/en/packages/next-intlayer/index.md).
To use Intlayer in your Express application, you can use [express-intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/en/packages/express-intlayer/index.md).
The `intlayer` package also provides some functions to help you to internationalize your application.
- [`getConfiguration()`](https://intlayer.org/doc/packages/intlayer/getConfiguration)
- [`getTranslation()`](https://intlayer.org/doc/packages/intlayer/getTranslation)
- [`getEnumeration()`](https://intlayer.org/doc/packages/intlayer/getEnumeration)
- [`getLocaleName()`](https://intlayer.org/doc/packages/intlayer/getLocaleName)
- [`getLocaleLang()`](https://intlayer.org/doc/packages/intlayer/getLocaleLang)
- [`getHTMLTextDir()`](https://intlayer.org/doc/packages/intlayer/getHTMLTextDir)
- [`getPathWithoutLocale()`](https://intlayer.org/doc/packages/intlayer/getPathWithoutLocale)
- [`getMultilingualUrls()`](https://intlayer.org/doc/packages/intlayer/getMultilingualUrls)
- [`getLocalizedUrl()`](https://intlayer.org/doc/packages/intlayer/getLocalizedUrl)
- [`getPathWithoutLocale()`](https://intlayer.org/doc/packages/intlayer/getPathWithoutLocale)
- [Intlayer Website](https://intlayer.org)
- [Intlayer Documentation](https://intlayer.org/docs)
- [Intlayer GitHub](https://github.com/aymericzip/intlayer)
- [Ask your questions to our smart documentation](https://intlayer.org/docs/chat)