Giter Club home page Giter Club logo

acrool-react-locale's Introduction

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 {BlockWrapper} from '@acrool/react-block';
import LocaleProvider from '@acrool/react-locale';
import dayjs from 'dayjs';
import React from 'react';
import styled from 'styled-components';

import {DEFAULT_LOCALE, serverDictionaries} from '@/config/locale';
import {persistKey} from '@/config/app';

interface IProps {
    children: JSX.Element
}


/**
 * 多語系提供者
 * @param children
 */
const ReactLocaleProvider = ({
     children
 }: IProps) => {
    const handleChangeLocale = (newLocale: string) => {
        dayjs.locale(newLocale);
    };

    return <LocaleProvider
        localeDictionaries={serverDictionaries}
        defaultLocale={DEFAULT_LOCALE}
        onChangeLocale={handleChangeLocale}
        renderLoading={() => <Loader/>}
        persistKey={`${persistKey}-locale`}
    >
        {children}
    </LocaleProvider>;
};

export default ReactLocaleProvider;

then in your src/app.tsx

const App = () => {
    return (
        <Provider store={setup.store}>
            <ReactLocaleProvider>
                <AppRoute/>
            </ReactLocaleProvider>
        </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 {OriginLocaleProvider} 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 ReactLocaleProvider = ({
    children
}: IProps) => {
    const dispatch = useDispatch();
    const locale = useSelector(selector.selectLanguage);

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

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

export default ReactLocaleProvider;

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

Play react-editext-example

License

MIT © Acrool & Imagine

acrool-react-locale's People

Contributors

imagine10255 avatar

Stargazers

 avatar BurgessChen avatar  avatar kr8851 avatar Ralf avatar Barry Allen avatar Chiu avatar Yale Codes avatar  avatar

Watchers

 avatar  avatar

acrool-react-locale's Issues

Array locale

The description field needs to be hard-coded. When doing multi-lingual at the front end, the dictionary file can only be processed in the way of 1, 2, and 3. I don’t know if it can be taken out as an array

<DescUl>
    {Array.from({length: 3}).map((row, index) => {
        return <li>
            {
                i18n(`viewModal.checkIn.desc${index}`, {args: {
                        checkInGiftDepositMaxDays: setting?.checkInGiftDepositMaxDays ?? 0,
                        checkInGiftMinValidBet: setting?.checkInGiftMinValidBet ?? 0,
                    }})
            }
        </li>
    })}
</DescUl>

If this method is used, the parameter will be invalid

{
'viewModal.checkIn.desc': `
       <li>需連續簽到七天,只要未簽到,隔天重新算起</li>
        <li>{:checkInGiftMinValidBet}天內須儲值一次</li>
        <li>7天需累計14,000</li>`,
}

 <DescUl dangerouslySetInnerHTML={{__html: i18n('viewModal.checkIn.desc', {args: {
          checkInGiftDepositMaxDays: setting?.checkInGiftDepositMaxDays ?? 0,
          checkInGiftMinValidBet: setting?.checkInGiftMinValidBet ?? 0,
  }})}}/>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.