Translation

    Defining Translations

    The t function in intlayer allows you to declare content in multiple languages. This function ensures type safety, raising an error if any translations are missing, which is particularly useful in TypeScript environments.

    Using TypeScript

    Here's an example of how to declare content with translations in a TypeScript file:

    import { t, type DeclarationContent } from "intlayer"; const multiLangContent = { key: "multi_lang", content: { welcomeMessage: t({ en: "Welcome to our application", fr: "Bienvenue dans notre application", es: "Bienvenido a nuestra aplicación", }), }, } satisfies DeclarationContent; export default multiLangContent;

    Using ECMAScript Modules

    If you're using ECMAScript modules, the declaration looks like this:

    import { t } from "intlayer"; const multiLangContent = { id: "multi_lang", welcomeMessage: t({ en: "Welcome to our application", fr: "Bienvenue dans notre application", es: "Bienvenido a nuestra aplicación", }), }; export default multiLangContent;

    Using CommonJS Modules

    In a CommonJS setup, you can declare translations like this:

    const { t } = require("intlayer"); const multiLangContent = { id: "multi_lang", welcomeMessage: t({ en: "Welcome to our application", fr: "Bienvenue dans notre application", es: "Bienvenido a nuestra aplicación", }), }; module.exports = multiLangContent;

    Using JSON

    For JSON-based declarations, you can define translations as follows:

    { "id": "multi_lang", "welcomeMessage": { "nodeType": "translation", "en": "Welcome to our application", "fr": "Bienvenue dans notre application", "es": "Bienvenido a nuestra aplicación" } }

    Configuration for Locales

    To ensure proper translation handling, you can configure the accepted locales in intlayer.config.ts. This configuration allows you to define the languages that your application supports:

    import { Locales, type IntlayerConfig } from "intlayer"; const config: IntlayerConfig = { internationalization: { locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH], }, }; export default config;

    Using Translations in React Components

    With react-intlayer, you can use translations in React components. Here's an example:

    import { useIntlayer } from "react-intlayer"; const MyComponent = () => { const content = useIntlayer("multi_lang"); return ( <div> <p>{content.welcomeMessage}</p> </div> ); }; export default MyComponent;

    This component fetches the corresponding translation based on the current locale set in your application.

    Custom Content Objects

    intlayer supports custom content objects for translation, allowing you to define more complex structures while ensuring type safety. Here's an example with a custom object:

    import { t, type DeclarationContent } from "intlayer"; interface ICustomContent { title: string; content: string; } const customContent = { key: "custom_content", content: { profileText: t<ICustomContent>({ en: { title: "Page Title", content: "Page Content", }, fr: { title: "Titre de la Page", content: "Contenu de la Page", }, es: { title: "Título de la Página", content: "Contenido de la Página", }, }), }, } satisfies DeclarationContent; export default customContent;

    In this page