@snack-uikit/locale
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -6,2 +6,13 @@ # Change Log | ||
## 0.1.1 (2024-02-07) | ||
### Bug Fixes | ||
* **FF-4166:** fallback locales for wrong lang, change typings, transform lang prop to correct format ([d581233](https://github.com/cloud-ru-tech/snack-uikit/commit/d581233bf551e7582f78b412187bd8cabcb72adb)) | ||
# 0.1.0 (2024-02-06) | ||
@@ -8,0 +19,0 @@ |
import { useContext, useMemo } from 'react'; | ||
import { LocaleContext } from './LocaleProvider'; | ||
import { DEFAULT_LANG, LocaleContext } from './LocaleProvider'; | ||
export function useLocale(componentName) { | ||
const { locales: ctxLocales, lang } = useContext(LocaleContext); | ||
const locales = useMemo(() => { | ||
const localesObj = ctxLocales[lang] || {}; | ||
let localesObj = ctxLocales[lang]; | ||
if (!localesObj) { | ||
console.warn(`Snack-uikit: localization for lang ${lang} was not found. Make sure you are using correct lang or passed proper locales to LocaleProvider. For now default language (${DEFAULT_LANG}) will be used`); | ||
localesObj = ctxLocales[DEFAULT_LANG]; | ||
} | ||
if (!componentName) { | ||
@@ -8,0 +12,0 @@ return localesObj; |
import { ReactNode } from 'react'; | ||
import { LocaleLang, OverrideLocales } from '../../types'; | ||
export declare const DEFAULT_LANG = "en_GB"; | ||
export type LocaleContextType = { | ||
@@ -13,2 +14,2 @@ lang: LocaleLang; | ||
}; | ||
export declare function LocaleProvider({ lang, locales: localesProp, children }: LocaleProviderProps): import("react/jsx-runtime").JSX.Element; | ||
export declare function LocaleProvider({ lang: langProp, locales: localesProp, children }: LocaleProviderProps): import("react/jsx-runtime").JSX.Element; |
@@ -5,7 +5,8 @@ import { jsx as _jsx } from "react/jsx-runtime"; | ||
import { LOCALES } from '../../locales'; | ||
export const DEFAULT_LANG = 'en_GB'; | ||
export const LocaleContext = createContext({ | ||
lang: 'en_US', | ||
lang: DEFAULT_LANG, | ||
locales: LOCALES, | ||
}); | ||
export function LocaleProvider({ lang = 'en_US', locales: localesProp, children }) { | ||
export function LocaleProvider({ lang: langProp = DEFAULT_LANG, locales: localesProp, children }) { | ||
const locales = useMemo(() => { | ||
@@ -17,3 +18,4 @@ if (localesProp) { | ||
}, [localesProp]); | ||
const lang = useMemo(() => langProp.replace('-', '_'), [langProp]); | ||
return _jsx(LocaleContext.Provider, { value: { lang, locales }, children: children }); | ||
} |
export declare const LOCALES: { | ||
readonly en_US: { | ||
readonly en_GB: { | ||
Table: { | ||
@@ -4,0 +4,0 @@ searchPlaceholder: string; |
@@ -1,6 +0,6 @@ | ||
import { en_US } from './en_US'; | ||
import { en_GB } from './en_GB'; | ||
import { ru_RU } from './ru_RU'; | ||
export const LOCALES = { | ||
en_US, | ||
en_GB, | ||
ru_RU, | ||
}; |
import { LOCALES } from './locales'; | ||
import { PartialDeep } from './typeUtils'; | ||
export type KnownLocaleLang = keyof typeof LOCALES; | ||
type CustomLang = `${string}_${string}`; | ||
export type LocaleLang = KnownLocaleLang | CustomLang; | ||
export type LocaleDictionary = typeof LOCALES.en_US; | ||
export type OverrideLocales = PartialDeep<Record<KnownLocaleLang, LocaleDictionary>> | Record<CustomLang, LocaleDictionary>; | ||
export {}; | ||
export type LocaleLang = KnownLocaleLang | string; | ||
export type LocaleDictionary = typeof LOCALES.en_GB; | ||
export type OverrideLocales = PartialDeep<Record<KnownLocaleLang, LocaleDictionary>> | Record<string, LocaleDictionary>; |
@@ -7,3 +7,3 @@ { | ||
"title": "Locale", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"sideEffects": [ | ||
@@ -41,3 +41,3 @@ "*.css", | ||
}, | ||
"gitHead": "e38849932e4595cfeaf19733fcd296ffcfc7322e" | ||
"gitHead": "0e3f0ca15d2f0903a25bfa72bd95da9a414bddc2" | ||
} |
@@ -10,3 +10,3 @@ # Locale | ||
If you don't do it then **default** (en_US) translations will be applied if applicable. | ||
If you don't do it then **default** (en_GB) translations will be applied if applicable. | ||
@@ -26,3 +26,3 @@ Also you can pass custom translations or replace/modify existing. | ||
const customTraslations: OverrideLocales = { | ||
en_US: { | ||
en_GB: { | ||
Table: { | ||
@@ -40,3 +40,3 @@ searchPlaceholder: 'custom', | ||
const lang = 'en_US'; // or 'ru_RU' or 'custom_LANG' | ||
const lang = 'en_GB'; // or 'ru_RU' or 'custom_LANG' | ||
@@ -59,3 +59,3 @@ // ... | ||
|------|------|---------------|-------------| | ||
| lang | ``${string}_${string}`` | en_US | | | ||
| lang* | `string` | - | | | ||
| locales | `OverrideLocales` | - | | | ||
@@ -62,0 +62,0 @@ ## useLocale |
import { useContext, useMemo } from 'react'; | ||
import { KnownLocaleLang, LocaleDictionary, LocaleLang } from '../../types'; | ||
import { LocaleContext, LocaleContextType } from './LocaleProvider'; | ||
import { DEFAULT_LANG, LocaleContext, LocaleContextType } from './LocaleProvider'; | ||
@@ -21,4 +21,12 @@ type LocaleComponentName = keyof LocaleDictionary; | ||
const locales = useMemo(() => { | ||
const localesObj = ctxLocales[lang as KnownLocaleLang] || {}; | ||
let localesObj = ctxLocales[lang as KnownLocaleLang]; | ||
if (!localesObj) { | ||
console.warn( | ||
`Snack-uikit: localization for lang ${lang} was not found. Make sure you are using correct lang or passed proper locales to LocaleProvider. For now default language (${DEFAULT_LANG}) will be used`, | ||
); | ||
localesObj = ctxLocales[DEFAULT_LANG] as LocaleDictionary; | ||
} | ||
if (!componentName) { | ||
@@ -25,0 +33,0 @@ return localesObj as LocaleDictionary; |
@@ -1,7 +0,7 @@ | ||
import { en_US } from './en_US'; | ||
import { en_GB } from './en_GB'; | ||
import { ru_RU } from './ru_RU'; | ||
export const LOCALES = { | ||
en_US, | ||
en_GB, | ||
ru_RU, | ||
} as const; |
@@ -6,10 +6,6 @@ import { LOCALES } from './locales'; | ||
type CustomLang = `${string}_${string}`; | ||
export type LocaleLang = KnownLocaleLang | string; | ||
export type LocaleLang = KnownLocaleLang | CustomLang; | ||
export type LocaleDictionary = typeof LOCALES.en_GB; | ||
export type LocaleDictionary = typeof LOCALES.en_US; | ||
export type OverrideLocales = | ||
| PartialDeep<Record<KnownLocaleLang, LocaleDictionary>> | ||
| Record<CustomLang, LocaleDictionary>; | ||
export type OverrideLocales = PartialDeep<Record<KnownLocaleLang, LocaleDictionary>> | Record<string, LocaleDictionary>; |
Sorry, the diff of this file is not supported yet
28115
369