Автор:
    Создание:2024-08-11Последнее обновление:2025-06-29

    Перевод

    Определение переводов

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

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const MyComponent: FC = () => {
    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.

    Конфигурация локалей

    Для обеспечения правильной обработки переводов вы можете настроить поддерживаемые локали в файле intlayer.config.ts. Эта конфигурация позволяет определить языки, которые поддерживает ваше приложение:

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

    Использование переводов в React-компонентах

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

    **/*.tsx
    import type { FC } from "react";
    import { useIntlayer } from "react-intlayer";
    
    const MyComponent: FC = () => {
    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.

    Пользовательские объекты контента

    intlayer поддерживает пользовательские объекты контента для перевода, позволяя определять более сложные структуры при обеспечении типовой безопасности. Вот пример с пользовательским объектом:

    **/*.content.ts
    import { t, type Dictionary } 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 Dictionary;
    
    export default customContent;