Materially React
  • Welcome
  • Prerequisites
  • Getting Started
  • Installation
  • Axios API Calls
  • Localization
  • File Structure
  • Routing
  • Template Config
  • Layout Option
  • Default Theme
  • Color Management
  • State Management
  • Dependencies
  • Support
  • Changelog
Powered by GitBook
On this page
  • IntlProvider
  • locale, formats, and messages
  • How does it work?

Was this helpful?

Localization

Localization

PreviousAxios API CallsNextFile Structure

Last updated 2 days ago

Was this helpful?

Materially supports four types of international languages ('en' - English, 'fr' - French, 'ro' - Romanian, 'zh' - Chinese). You can switch language from the header bar. We internationalize the main menu for all four languages, When you change it from the header, you will see the effect there. If you want to configure one more language or set a default language then continue reading below...

IntlProvider

React Intl uses the provider pattern to scope an i18n context to a tree of components. This allows configurations like the current locale and set of translated strings/messages to be provided at the root of a component tree and made available to the <FormattedMessage> components.

locale, formats, and messages

The user's current locale and what the app should be rendered in. While defaultLocale and defaultFormats are for fallbacks or during development and represent the app's default. Notice how there is no, defaultMessagesthat's because each provides a defaultMessage

How does it work?

Add node modules react-intl emo

yarn add react-intl / npm install --save react-intl

Data for locale files exist at src\utils\locales

{
  "dashboard": "Dashboard",
  "default": "Default",

  "widget": "Widget",
  "widgets": "Widgets",
  "statistic": "Statistic",
...
... 
}

Open the file config and set the language.

src/config.ts
const config: ConfigProps = {
  mode: ThemeMode.LIGHT,
  i18n: ThemeI18n.EN,
  themeDirection: ThemeDirection.LTR
};
src/config.js
const config = {
  mode: ThemeMode.LIGHT,
  i18n: ThemeI18n.EN,
  themeDirection: ThemeDirection.LTR
};

Open file Locales and apply IntlProvider

src/components/Locales.tsx
import { ReactElement, useEffect, useState } from 'react';

// third party
import { IntlProvider, MessageFormatElement } from 'react-intl';

// project imports
import { ThemeI18n } from 'config';
import useConfig from 'hooks/useConfig';
import { ChildrenProps } from 'types/root';

// locales
const loadLocaleData = (locale: ThemeI18n) => {
  switch (locale) {
    case ThemeI18n.FR:
      return import('utils/locales/fr.json');
    case ThemeI18n.RO:
      return import('utils/locales/ro.json');
    case ThemeI18n.ZH:
      return import('utils/locales/zh.json');
    case ThemeI18n.EN:
    default:
      return import('utils/locales/en.json');
  }
};

// ==============================|| LOCALIZATION ||============================== //

export default function Locales({ children }: ChildrenProps) {
  const { i18n } = useConfig();

  const [messages, setMessages] = useState<Record<string, string> | Record<string, MessageFormatElement[]> | undefined>();

  useEffect(() => {
    loadLocaleData(i18n).then((d: { default: Record<string, string> | Record<string, MessageFormatElement[]> | undefined }) => {
      setMessages(d.default);
    });
  }, [i18n]);

  return (
    <>
      {messages && (
        <IntlProvider locale={i18n} defaultLocale="en" messages={messages}>
          {children as ReactElement}
        </IntlProvider>
      )}
    </>
  );
}
src/components/Locales.jsx
import PropTypes from 'prop-types';
import { useEffect, useState } from 'react';

// third party
import { IntlProvider } from 'react-intl';

// project imports
import { ThemeI18n } from 'config';
import useConfig from 'hooks/useConfig';

// locales
const loadLocaleData = (locale) => {
  switch (locale) {
    case ThemeI18n.FR:
      return import('utils/locales/fr.json');
    case ThemeI18n.RO:
      return import('utils/locales/ro.json');
    case ThemeI18n.ZH:
      return import('utils/locales/zh.json');
    case ThemeI18n.EN:
    default:
      return import('utils/locales/en.json');
  }
};

// ==============================|| LOCALIZATION ||============================== //

export default function Locales({ children }) {
  const { i18n } = useConfig();

  const [messages, setMessages] = useState();

  useEffect(() => {
    loadLocaleData(i18n).then((d) => {
      setMessages(d.default);
    });
  }, [i18n]);

  return (
    <>
      {messages && (
        <IntlProvider locale={i18n} defaultLocale="en" messages={messages}>
          {children}
        </IntlProvider>
      )}
    </>
  );
}

Locales.propTypes = { children: PropTypes.any };

Message Descriptor