npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@codestacks/react-locale

v4.2.3

Published

Internationalize Elegant intl library based for Reactjs

Readme

Codestacks React Locale

NPM npm npm

npm downloads npm

^3.2.1 support react >=18.0.0 <20.0.0

Features

  • Use react-intl v7 for packaging
  • Support Hooks
  • Support Nextjs 15+

Install

yarn add @codestacks/react-locale

Usage

create src/config/locale.ts

import {TLocaleDictionaries} from '@codestacks/react-locale';
import {enUS} from '@/locales/en-US';
import {zhTW} from '@/locales/zh-TW';
import {jaJP} from '@/locales/ja-JP';

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

export const localeDictionaries: TLocaleDictionaries = {
    [ELocales.enUS]: enUS,
    [ELocales.zhTW]: zhTW,
    [ELocales.jaJP]: jaJP,
};

create src/locales/en-US.ts (Default Locales)

/*-------------------------
*      Default Locales
---------------------------*/
const translations = {
    page: {
        'page.home.field.title': 'Title',
        'page.home.desc': `This is a carousel developed directly using React + Flexbox (non-js secondary development package into React),
    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',
    },
    formatError:{
        'formatError.email': 'Email format error',
        'formatError.max': 'The maximum value is {max}',
        'formatError.maxFraction': 'The floating-point number can only have a maximum of {maxFraction} digits',
        'formatError.maxLength': 'Up to {maxLength} characters',
        'formatError.min': 'The minimum value is {min}',
        'formatError.minLength': 'At least {minLength} characters',
        'formatError.pattern': 'Format error',
        'formatError.required': 'Required Column'
    }
};

export default translations;
export type TDefaultTranslations = typeof translations;

create src/locales/zh-TW.ts (Options Locales)

import {TDefaultTranslations} from './en-US'; // Import default locales

const translations: TDefaultTranslations = {
    page:{
        'page.home.field.title': '標題',
        'page.home.desc': `Bear Carousel 是一個直接使用React + Flexbox開發的輪播套件 (非js二次開發包成React),
     並且只包含你需要的功能,沒有太多很酷的效果,因為那些可能會讓你變得不容易使用並且產生其他奇怪的問題`,
        'page.home.feature.title': '特性',
    },
    formatError: {
        'formatError.email': '電子郵件格式錯誤',
        'formatError.max': '最大值為 {max}',
        'formatError.maxFraction': '浮點數最多僅能為 {maxFraction} 位數',
        'formatError.maxLength': '最多 {maxLength} 個字元',
        'formatError.min': '最小值為 {min}',
        'formatError.minLength': '至少 {minLength} 個字元',
        'formatError.pattern': '格式錯誤',
        'formatError.required': '必填欄位'
    }
} as const;

export default translations;

as const only other language

create src/library/react-locale/index.tsx

import {BlockWrapper} from '@codestacks/react-block';
import {LocaleProvider} from '@codestacks/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
}


/**
 * Custom Locale Provider
 * @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`}
        ignoreMissingLocaleMessage={false}
    >
        {children}
    </LocaleProvider>;
};

export default ReactLocaleProvider;

then in your src/app.tsx


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

If you need async load dictionaries

in src/config/locale.ts

${languageCode}-${countryCode}

languageCode is a two-letter lowercase code as defined by ISO-639. https://zh.wikipedia.org/zh-tw/ISO_639-1

countryCode is a two-letter uppercase code as defined by ISO-3166. https://zh.wikipedia.org/zh-tw/ISO_3166-1

For example, en-US is the language value for United States English.

import {TLocaleDictionariesAsync} from '@codestacks/react-locale';

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

export const localeDictionaries: TLocaleDictionariesAsync = {
    [ELocales.enUS]: () => import('@/locales/en-US').then((module) => module.default),
    [ELocales.zhTW]: () => import('@/locales/zh-TW').then((module) => module.default),
    [ELocales.jaJP]: () => import('@/locales/ja-JP').then((module) => module.default),
};

in src/library/react-locale/index.tsx

import {LocaleAsyncProvider} from '@codestacks/react-locale';
// ...ignore same

const ReactLocaleProvider = ({
     children
 }: IProps) => {
    const handleChangeLocale = (newLocale: string) => {
        dayjs.locale(newLocale);
    };

    return <LocaleAsyncProvider
        //...ignore same
    >
        {children}
    </LocaleAsyncProvider>;
};

export default ReactLocaleProvider;

How to use

function component hook

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


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

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

import {translateI18n} from '@codestacks/react-locale';
translateI18n('page.promotion.title', {def: '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 '@codestacks/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)}
        ignoreMissingLocaleMessage={false}
    >
        {Children.only(children)}
    </OriginLocaleProvider>;
};

export default ReactLocaleProvider;

Storybook use (Not async load locale dictionaries)

import {PartialStoryFn, Renderer} from '@storybook/types';
import {DEFAULT_LOCALE, ELocales} from "@/config/locale";
import {persistKey} from "@/config/app";
import LocaleProvider, {TLocaleDictionaries} from "@codestacks/react-locale";
import localeEnUS from "@/locales/en-US";


const localeDictionaries: TLocaleDictionaries = {
    [ELocales.enUS]: localeEnUS,
};
const withI18next = (StoryFn: PartialStoryFn<Renderer>) => {

    return (
        <LocaleProvider
            localeDictionaries={localeDictionaries}
            defaultLocale={DEFAULT_LOCALE}
            persistKey={`${persistKey}-locale`}
            ignoreMissingLocaleMessage={false}
        >
            <StoryFn/>
        </LocaleProvider>
    );
};



// storybook preview.tsx
const preview: Preview = {
    parameters: {
        controls: {
            matchers: {
                color: /(background|color)$/i,
                date: /Date$/i,
            },
        },
    },
    decorators: [
        withI18next,
        (Story) => (
            <GridThemeProvider>
                <Story/>
            </GridThemeProvider>
        ),
    ],
};