/acrool-react-locale

Internationalize Elegant intl library based for Reactjs

Primary LanguageTypeScriptMIT LicenseMIT

Acrool React Locale

Acrool React Locale Logo

Internationalize Elegant intl library based for Reactjs

NPM npm npm

npm downloads npm

Features

  • Use intl for packaging
  • Support Hooks
  • Support Nextjs 14+

Install

yarn add @acrool/react-locale

Usage

create src/config/locale.ts

import {TLocaleDictionaries} from '@acrool/react-locale';

export enum ELocales {
    enUS = 'en-US',
    zhTW = 'zh-TW',
    jaJP = 'ja-JP',
}

export const localeDictionaries: TLocaleDictionaries = {
    [ELocales.enUS]: require('locales/en-US').default,
    [ELocales.zhTW]: require('locales/zh-TW').default,
    [ELocales.jaJP]: require('locales/ja-JP').default,
};

create src/locales/en-US.ts

export default {
    'page.home.title': 'Home',
    'page.home.desc': `This is a carousel developed directly using React + Flexbox (non-js secondary development package into React),<br/>
    and only include the features you need, not too many cool effects that might complicate your usage or create other weird issues.`,
    'page.home.feature.title': 'Feature',
};

create src/locales/zh-TW.ts

export default {
    'page.home.title': '關於',
    'page.home.desc': `Bear Carousel 是一個直接使用React + Flexbox開發的輪播套件 (非js二次開發包成React),<br/>
     並且只包含你需要的功能,沒有太多很酷的效果,因為那些可能會讓你變得不容易使用並且產生其他奇怪的問題`,
    'page.home.feature.title': '特性',
};

in your src/app.tsx add (is not global state)

import {StateControlLocaleProvider} from '@acrool/react-locale';
import {DEFAULT_LOCALE, localeDictionaries} from './config/locale';

<StateControlLocaleProvider 
    localeDictionaries={localeDictionaries}
    defaultLocale={DEFAULT_LOCALE}
    persistKey="acrool-example"
    isReMountWithChangeLocale={true} // option: If you want to change the language, re-mount
>
    <AppRoute/>
</StateControlLocaleProvider>

then in your src/app.tsx

const App = () => {
    return (
        <Provider store={setup.store}>
            <LanguageProvider>
                <AppRoute/>
            </LanguageProvider>
        </Provider>
    );
};

How to use

function component hook

import {useLocale} from '@acrool/react-locale';


const Example = () => {
    const {i18n} = useLocale();
    return <div>{i18n('page.promotion.title', {defaultMessage: 'promotions', params: {country: 'Taiwan'}})}</div>
}

global function (in not function component) only in provider children component

import {translateI18n} from '@acrool/react-locale';
translateI18n('page.promotion.title', {defaultMessage: 'promotions', params: {country: 'taiwan'}})

Option

if you use redux(global state) link locale, your can create custom Provider in your project

import React, {Children} from 'react';
import {LocaleProvider} from '@acrool/react-locale';
import {useDispatch, useSelector} from 'react-redux';
import {localeDictionaries, DEFAULT_LOCALE, ELocales} from 'config/locale';

// Stores
import {selector, actions} from 'store/main/language';

interface IProps {
    children: JSX.Element
}

const LanguageProvider = ({
    children
}: IProps) => {
    const dispatch = useDispatch();
    const locale = useSelector(selector.selectLanguage);

    const handleChangeLocale = (locale: ELocales) => {
        dispatch(actions.setLocale({locale}));
    };

    return <LocaleProvider
        localeDictionaries={localeDictionaries}
        defaultLocale={DEFAULT_LOCALE}
        locale={locale}
        setLocale={(locale: string ) => handleChangeLocale(locale as ELocales)}
    >
        {Children.only(children)}
    </LocaleProvider>;
};

export default LanguageProvider;

There is also a example that you can play with it:

Play react-editext-example

License

MIT © Acrool & Imagine