1.2.6 • Published 3 years ago

use-localization v1.2.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

use-localization

Custom Hook to support localization when locale changes. The hook will accept the locale and return the localization utilities.

NPM npm

Install

npm install use-localization

Usage

import React, { useState, useMemo } from "react";
import useLocalization from 'use-localization';

function MyLocalizationComponent() {
  const [locale, setLocale] = useState('en-IN');
  const config = useMemo(() => ({ locale }), [locale]);
  const { formatNumber, formatCurrency, flag, isRTL } = useLocalization(config);
  return (
        <div>
            <div>Formatted Currency: {formatCurrency(783984.356)}</div>
            <div>Formatted Number: {formatNumber(783984.356)}</div>
            <div>Flag: {flag}</div>
            {isRTL && <div>{`${isRTL}`}</div>}
            <button type="button" onClick={() => setLocale(locale === 'en-IN' ? 'en-US' : 'en-IN')}>
                Switch to {locale === 'en-IN' ? 'US' : 'INDIA'}
            </button>
        </div>
  )
}

Examples

To play around with the hook, try these interactive sample apps

CodeSandbox

Parameters

ParamTypeOptionalDefaultDescription
configObjectNo-Must have the locale property present

Returns

An object containing below properties

PropReturn TypeDescription
formatCurrencyFunctionTakes any number as an argument and returns the formatted localized value with currency
formatNumberFunctionTakes any number as an argument and returns the formatted localized value
flagTextText representation of the country flag based on the locale. For example, 🇮🇳 for en-IN
isRTLBooleanTrue, if the passed locale is RTL. For example, Arabic, Hebrew, etc.

Supported Locales

sq-AL, ar-DZ, ar-BH, ar-EG, ar-IQ, ar-JO, ar-KW, ar-LB, ar-LY, ar-MA, 
ar-OM, ar-QA, ar-SA, ar-SD, ar-SY, ar-TN, ar-AE, ar-YE, be-BY, bg-BG, 
ca-ES, zh-CN, zh-HK, zh-SG, zh-TW, hr-HR, cs-CZ, da-DK, nl-BE, nl-NL, 
en-AU, en-CA, en-IN, en-IE, en-MT, en-NZ, en-PH, en-SG, en-ZA, en-GB, 
en-US, et-EE, fi-FI, fr-BE, fr-CA, fr-FR, fr-LU, fr-CH, de-AT, de-DE, 
de-LU, de-CH, el-CY, el-GR, iw-IL, hi-IN, hu-HU, is-IS, in-ID, ga-IE, 
it-IT, it-CH, ja-JP, ko-KR, lv-LV, lt-LT, mk-MK, ms-MY, mt-MT, no-NO, 
pl-PL, pt-BR, pt-PT, ro-RO, ru-RU, sr-BA, sr-ME, sr-CS, sr-RS, sk-SK, 
sl-SI, es-AR, es-BO, es-CL, es-CO, es-CR, es-DO, es-EC, es-SV, es-GT, 
es-HN, es-MX, es-NI, es-PA, es-PY, es-PE, es-PR, es-ES, es-US, es-UY, 
es-VE, sv-SE, th-TH, tr-TR, uk-UA, vi-VN

License

MIT © mayank8aug

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago