Skip to content

Commit

Permalink
docs: update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
aymericzip committed Apr 25, 2024
1 parent 5ea88dd commit b416a68
Show file tree
Hide file tree
Showing 34 changed files with 1,313 additions and 178 deletions.
206 changes: 61 additions & 145 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,197 +1,113 @@
# Intlayer: Next-Level Content Management in JavaScript
# Intlayer: A closer way to translate your application

**Intlayer** is an innovative Content Management System (CMS) designed specifically for JavaScript developers. It enables seamless transpilation of JavaScript content into structured dictionaries, making integration into your codebase straightforward and efficient.
Intlayer is an innovative internationalization framework designed to replace i18next for Next.js and React applications. It streamlines the process of internationalization with flexible content declaration and robust configuration options.

## Why Choose Intlayer?
## Why Intlayer?

- **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.
- **Simplified Setup**: Get up and running quickly with minimal configuration, especially optimized for Next.js projects.
- **Server Component Support**: Perfectly suited for Next.js server components, ensuring smooth server-side rendering.
- **Enhanced Routing**: Full support for Next.js app routing, adapting seamlessly to complex application structures.
Intlayer offers a more modern approach to internationalization compared to traditional frameworks like i18next. It is designed to fit seamlessly into Next.js and React projects, providing intuitive content declaration, robust configuration options, and a simpler setup process.

## Getting Started with Intlayer
- **Content Declaration**: Declare content dictionaries in the same directory as your components, enhancing maintainability and reducing complexity. [Learn more](https://github.com/aypineau/intlayer/blob/main/docs/content_declaration.md).
- **Customizable Configuration**: Intlayer allows you to customize various aspects of the framework, including internationalization, middleware, and content handling. [Learn how to configure](https://github.com/aypineau/intlayer/blob/main/docs/configuration.md).
- **Integration with Next.js and React**: Intlayer integrates smoothly with Next.js and React applications. It also supports server-side rendering and dynamic routing. [Explore Next.js integration](https://github.com/aypineau/intlayer/blob/main/docs/intlayer_with_nextjs.md) | [Explore React integration](https://github.com/aypineau/intlayer/blob/main/docs/intlayer_with_create_react_app.md).
- **Scalability and Consistency**: With Intlayer, your content dictionaries are structured and consistent, reducing the chances of errors during development. This ensures a more reliable internationalization process.
- **TypeScript Support**: Intlayer provides full support for TypeScript, allowing for more type-safe code. This enhances developer productivity and code quality.

Setting up Intlayer in a Next.js application is straightforward:
## Getting Started

### Step 1: Install Dependencies
To start using Intlayer, follow these steps:

Install the necessary packages using npm:
### Installation

Install the necessary packages for your project:

```bash
npm install intlayer next-intlayer
npm install intlayer
```

```bash
yarn install intlayer next-intlayer
yarn add intlayer
```

```bash
pnpm install intlayer next-intlayer
pnpm add intlayer
```

### Step 2: Integrate Intlayer in Your Next.js Configuration
### Declaring Your Content

Configure your Next.js setup to use Intlayer:
Intlayer allows you to declare your content in various formats, including TypeScript, ECMAScript modules, CommonJS modules, and JSON. Here's an example of a content declaration using TypeScript:

```javascript
// next.config.mjs
import { withIntlayer } from "next-intlayer/server";
```typescript
import { t, type ContentModule } from "intlayer";

const nextConfig = {};
const exampleContent: ContentModule = {
id: "example",
welcome: t({
en: "Welcome",
fr: "Bienvenue",
es: "Bienvenido",
}),
};

export default withIntlayer(nextConfig);
export default exampleContent;
```

### Step 3: Configure Middleware for Locale Detection
For more examples and information on content declaration, [see the documentation](https://github.com/aypineau/intlayer/blob/main/docs/content_declaration.md).

Set up middleware to detect the user's preferred locale:
### Integrating with Next.js

```typescript
// src/middleware.ts
export { intlayerMiddleware as middleware } from 'next-intlayer/middleware';
If you're using Next.js, Intlayer is designed to work seamlessly with it. The integration process involves setting up middleware, defining locale routes, and utilizing content dictionaries in your components.

export const config = {
matcher: '/((?!api|static|._\\.._|\_next).*),
};
```
Follow this guide to [set up Intlayer with Next.js](https://github.com/aypineau/intlayer/blob/main/docs/intlayer_with_nextjs.md).

### Step 4: Define Dynamic Locale Routes
### Integrating with React

Implement dynamic routing for localized content:
For Create React App users, Intlayer also provides an easy way to integrate internationalization. Learn how to [set up Intlayer with Create React App](https://github.com/aypineau/intlayer/blob/main/docs/intlayer_with_create_react_app.md).

Change `src/app/page.ts` to `src/app/[locale]/page.ts`
### Configuration

### Step 5: Manage Your Content
Intlayer's configuration is highly customizable. You can define internationalization settings, middleware behavior, and more. The configuration file supports various formats like TypeScript, JavaScript, and JSON.

Create and manage your content dictionaries:
Here's an example configuration:

```typescript
// src/app/[locale]/page.content.ts
import { t, type ContentModule } from "intlayer";
// intlayer.config.ts

import { Locales, type IntlayerConfig } from "intlayer";

const pageContent: ContentModule = {
id: "page",
getStarted: {
main: t({
en: "Get started by editing",
fr: "Commencez par éditer",
es: "Comience por editar",
}),
pageLink: "src/app/page.tsx",
const config: IntlayerConfig = {
internationalization: {
locales: [Locales.ENGLISH],
defaultLocale: Locales.ENGLISH,
},
};

export default pageContent;
export default config;
```

### Step 6: Utilize Content in Your Code

Access your content dictionaries throughout your application:

```tsx
// src/app/[locale]/page.ts

import { ClientComponentExample } from "@component/components/ClientComponentExample";
import { LocaleSwitcher } from "@component/components/LangSwitcherDropDown";
import { NestedServerComponentExample } from "@component/components/NestedServerComponentExample";
import { ServerComponentExample } from "@component/components/ServerComponentExample";
import { type NextPageIntlayer, IntlayerClientProvider } from "next-intlayer";
import { IntlayerServerProvider, useIntlayer } from "next-intlayer/server";

const Page: NextPageIntlayer = ({ params: { locale } }) => {
const content = useIntlayer("page", locale);

return (
<>
<p>
{content.getStarted.main}
<code>{content.getStarted.pageLink}</code>
</p>
{/**
* IntlayerServerProvider is used to provide the locale to the server children
* Don't work if set in the layout
*/}
<IntlayerServerProvider locale={locale}>
<ServerComponentExample />
</IntlayerServerProvider>
{/**
* IntlayerClientProvider is used to provide the locale to the client children
* Can be set in any parent component, including the layout
*/}
<IntlayerClientProvider locale={locale}>
<ClientComponentExample />
</IntlayerClientProvider>
</>
);
};

export default Page;
```
For more information on configuring Intlayer, [check the configuration documentation](https://github.com/aypineau/intlayer/blob/main/docs/configuration.md).

```tsx
// src/components/ClientComponentExample.tsx
## Using Intlayer with i18next

"use client";
Intlayer can also export i18next dictionaries for projects that still rely on i18next for certain functionalities. This integration allows you to generate i18next dictionaries while still benefiting from Intlayer's flexible content declaration. [Learn how to configure Intlayer for i18next](https://github.com/aypineau/intlayer/blob/main/docs/intlayer_with_i18n.md).

import { useIntlayer } from "next-intlayer";
## CLI Commands

export const ClientComponentExample = () => {
const content = useIntlayer("client-component-example"); // Create related content declaration
Intlayer comes with a CLI package that allows you to transpile your content declarations into dictionaries. You can run these commands to build your dictionaries:

return (
<div>
<h2>{content.title} </h2>
<p>{content.content}</p>
</div>
);
};
```bash
npx intlayer transpile
```

```tsx
// src/components/ServerComponentExample.tsx
To run in watch mode:

import { useIntlayer } from "next-intlayer/server";

export const ServerComponentExample = () => {
const content = useIntlayer("server-component-example"); // Create related content declaration

return (
<div>
<h2>{content.title} </h2>
<p>{content.content}</p>
</div>
);
};
```bash
npx intlayer watch
```

For more detailed usage of intlayer into Client, or Server component, see the [nextJS example here](https://github.com/aypineau/intlayer/blob/main/examples/nextjs-app/src/app/%5Blocale%5D/demo-usage-components/page.tsx).

## Configuration of your project

Create a config file to configure the languages of your application:

```typescript
// intlayer.config.ts

import { Locales, type IntlayerConfig } from "intlayer";

const config: IntlayerConfig = {
internationalization: {
locales: [
Locales.ENGLISH,
// Your other locales
],
defaultLocale: Locales.ENGLISH,
},
};

export default config;
```
For more information on the CLI and its usage, [refer to the CLI documentation](https://github.com/aypineau/intlayer/blob/main/docs/intlayer_cli.md).

To see all available parameters, refer to the [configuration documentation here](https://github.com/aypineau/intlayer/blob/main/docs/configuration.md).
## Conclusion

---
Intlayer offers a more flexible and modern approach to internationalization. Its seamless integration with Next.js and React, customizable configuration, and support for various content declaration formats make it a powerful choice for internationalization.

This version emphasizes ease of use, practical steps, and the professional application of Intlayer in a Next.js environment.
For additional resources, guides, and examples, explore the [Intlayer documentation](https://github.com/aypineau/intlayer/blob/main/docs/intlayer_with_nextjs.md).
49 changes: 49 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,49 +170,98 @@ Settings related to content handling within the application, including directory
- _Description_: The base directory for the project.
- _Example_: `'/path/to/project'`
- _Note_: This is used to resolve all Intlayer-related directories.
- **dictionaryOutput**:
- _Type_: `string[]`
- _Default_: `['intlayer']`
- _Description_: The type of dictionary output to use, e.g., `'intlayer'` or `'i18next'`.
- **contentDirName**:
- _Type_: `string`
- _Default_: `'src'`
- _Description_: The name of the directory where the content is stored.
- _Example_: `'data'`, `'content'`, `'locales'`
- _Note_: If not at the base directory level, update the `contentDir`.
- **contentDir**:

- _Type_: `string`
- _DerivedFrom_: `'baseDir'` / `'contentDirName'`
- _Description_: The directory path where content is stored.

- **resultDirName**:
- _Type_: `string`
- _Default_: `'.intlayer'`
- _Description_: The name of the directory where results are stored.
- _Example_: `'outputOFIntlayer'`
- _Note_: If this directory is not at the base level, update `resultDir`.
- **resultDir**:

- _Type_: `string`
- _DerivedFrom_: `'baseDir'` / `'resultDirName'`
- _Description_: The directory path for storing intermediate or output results.

- **moduleAugmentationDirName**:

- _Type_: `string`
- _Default_: `'types'`
- _Description_: Directory for module augmentation, allowing better IDE suggestions and type checking.
- _Example_: `'intlayer-types'`
- _Note_: Be sure to include this in `tsconfig.json`.

- **moduleAugmentationDir**:

- _Type_: `string`
- _DerivedFrom_: `'baseDir'` / `'moduleAugmentationDirName'`
- _Description_: The path for module augmentation and additional type definitions.

- **dictionariesDirName**:
- _Type_: `string`
- _Default_: `'dictionary'`
- _Description_: Directory for storing dictionaries.
- _Example_: `'translations'`
- _Note_: If not at the result directory level, update `dictionariesDir`.
- **dictionariesDir**:

- _Type_: `string`
- _DerivedFrom_: `'resultDir'` / `'dictionariesDirName'`
- _Description_: The directory for storing localization dictionaries.

- **i18nDictionariesDirName**:
- _Type_: `string`
- _Default_: `'i18_dictionary'`
- _Description_: Directory for storing i18n dictionaries.
- _Example_: `'translations'`
- _Note_: If not at the result directory level, update `i18nDictionariesDir`.
- _Note_: Ensure the i18n dictionaries output includes i18next to build the dictionaries for i18next
- **i18nDictionariesDir**:

- _Type_: `string`
- _DerivedFrom_: `'resultDir'` / `'i18nDictionariesDirName'`
- _Description_: The directory for storing i18n dictionaries.
- _Note_: Ensure this directory is configured for the i18next output type.

- **typeDirName**:

- _Type_: `string`
- _Default_: `'types'`
- _Description_: Directory for storing dictionary types.
- _Example_: `'intlayer-types'`
- _Note_: If not at the result directory level, update `typesDir`.

- **typesDir**:

- _Type_: `string`
- _DerivedFrom_: `'resultDir'` / `'typeDirName'`
- _Description_: The directory for storing dictionary types.

- **mainDirName**:
- _Type_: `string`
- _Default_: `'main'`
- _Description_: Directory for storing main files.
- _Example_: `'intlayer-main'`
- _Note_: If not at the result directory level, update `mainDir`.
- **mainDir**:
- _Type_: `string`
- _DerivedFrom_: `'resultDir'` / `'mainDirName'`
- _Description_: The directory where main application files are stored.
- **excludedPath**:
- _Type_: `string[]`
- _Default_: `['node_modules']`
Expand Down
Loading

0 comments on commit b416a68

Please sign in to comment.