@snack-uikit/locale 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 0.1.1 (2024-02-07)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **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))
12
+
13
+
14
+
15
+
16
+
6
17
  # 0.1.0 (2024-02-06)
7
18
 
8
19
 
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  You need to wrap your app into `<LocaleProvider>` to make translations work.
9
9
 
10
- If you don't do it then **default** (en_US) translations will be applied if applicable.
10
+ If you don't do it then **default** (en_GB) translations will be applied if applicable.
11
11
 
12
12
  Also you can pass custom translations or replace/modify existing.
13
13
 
@@ -22,7 +22,7 @@ But when passing a custom language, you will have to specify all translations.
22
22
  import { LocaleProvider, OverrideLocales } from '@snack-uikit/locale';
23
23
 
24
24
  const customTraslations: OverrideLocales = {
25
- en_US: {
25
+ en_GB: {
26
26
  Table: {
27
27
  searchPlaceholder: 'custom',
28
28
  },
@@ -35,7 +35,7 @@ const customTraslations: OverrideLocales = {
35
35
  },
36
36
  };
37
37
 
38
- const lang = 'en_US'; // or 'ru_RU' or 'custom_LANG'
38
+ const lang = 'en_GB'; // or 'ru_RU' or 'custom_LANG'
39
39
 
40
40
  // ...
41
41
  <LocaleProvider lang={lang} locales={customTranslations}>
@@ -53,7 +53,7 @@ const lang = 'en_US'; // or 'ru_RU' or 'custom_LANG'
53
53
  ### Props
54
54
  | name | type | default value | description |
55
55
  |------|------|---------------|-------------|
56
- | lang | ``${string}_${string}`` | en_US | |
56
+ | lang* | `string` | - | |
57
57
  | locales | `OverrideLocales` | - | |
58
58
  ## useLocale
59
59
  `helper`
@@ -1,5 +1,6 @@
1
1
  import { ReactNode } from 'react';
2
2
  import { LocaleLang, OverrideLocales } from '../../types';
3
+ export declare const DEFAULT_LANG = "en_GB";
3
4
  export type LocaleContextType = {
4
5
  lang: LocaleLang;
5
6
  locales: OverrideLocales;
@@ -10,4 +11,4 @@ export type LocaleProviderProps = {
10
11
  locales?: OverrideLocales;
11
12
  children: ReactNode;
12
13
  };
13
- export declare function LocaleProvider({ lang, locales: localesProp, children }: LocaleProviderProps): import("react/jsx-runtime").JSX.Element;
14
+ export declare function LocaleProvider({ lang: langProp, locales: localesProp, children }: LocaleProviderProps): import("react/jsx-runtime").JSX.Element;
@@ -2,16 +2,18 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import merge from 'lodash.merge';
3
3
  import { createContext, useMemo } from 'react';
4
4
  import { LOCALES } from '../../locales';
5
+ export const DEFAULT_LANG = 'en_GB';
5
6
  export const LocaleContext = createContext({
6
- lang: 'en_US',
7
+ lang: DEFAULT_LANG,
7
8
  locales: LOCALES,
8
9
  });
9
- export function LocaleProvider({ lang = 'en_US', locales: localesProp, children }) {
10
+ export function LocaleProvider({ lang: langProp = DEFAULT_LANG, locales: localesProp, children }) {
10
11
  const locales = useMemo(() => {
11
12
  if (localesProp) {
12
13
  return merge({}, LOCALES, localesProp);
13
14
  }
14
15
  return LOCALES;
15
16
  }, [localesProp]);
17
+ const lang = useMemo(() => langProp.replace('-', '_'), [langProp]);
16
18
  return _jsx(LocaleContext.Provider, { value: { lang, locales }, children: children });
17
19
  }
@@ -1,9 +1,13 @@
1
1
  import { useContext, useMemo } from 'react';
2
- import { LocaleContext } from './LocaleProvider';
2
+ import { DEFAULT_LANG, LocaleContext } from './LocaleProvider';
3
3
  export function useLocale(componentName) {
4
4
  const { locales: ctxLocales, lang } = useContext(LocaleContext);
5
5
  const locales = useMemo(() => {
6
- const localesObj = ctxLocales[lang] || {};
6
+ let localesObj = ctxLocales[lang];
7
+ if (!localesObj) {
8
+ 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`);
9
+ localesObj = ctxLocales[DEFAULT_LANG];
10
+ }
7
11
  if (!componentName) {
8
12
  return localesObj;
9
13
  }
@@ -1,4 +1,4 @@
1
- export declare const en_US: {
1
+ export declare const en_GB: {
2
2
  Table: {
3
3
  searchPlaceholder: string;
4
4
  noData: {
@@ -1,4 +1,4 @@
1
- export const en_US = {
1
+ export const en_GB = {
2
2
  Table: {
3
3
  searchPlaceholder: 'Search',
4
4
  noData: {
@@ -1,5 +1,5 @@
1
1
  export declare const LOCALES: {
2
- readonly en_US: {
2
+ readonly en_GB: {
3
3
  Table: {
4
4
  searchPlaceholder: string;
5
5
  noData: {
@@ -1,6 +1,6 @@
1
- import { en_US } from './en_US';
1
+ import { en_GB } from './en_GB';
2
2
  import { ru_RU } from './ru_RU';
3
3
  export const LOCALES = {
4
- en_US,
4
+ en_GB,
5
5
  ru_RU,
6
6
  };
package/dist/types.d.ts CHANGED
@@ -1,8 +1,6 @@
1
1
  import { LOCALES } from './locales';
2
2
  import { PartialDeep } from './typeUtils';
3
3
  export type KnownLocaleLang = keyof typeof LOCALES;
4
- type CustomLang = `${string}_${string}`;
5
- export type LocaleLang = KnownLocaleLang | CustomLang;
6
- export type LocaleDictionary = typeof LOCALES.en_US;
7
- export type OverrideLocales = PartialDeep<Record<KnownLocaleLang, LocaleDictionary>> | Record<CustomLang, LocaleDictionary>;
8
- export {};
4
+ export type LocaleLang = KnownLocaleLang | string;
5
+ export type LocaleDictionary = typeof LOCALES.en_GB;
6
+ export type OverrideLocales = PartialDeep<Record<KnownLocaleLang, LocaleDictionary>> | Record<string, LocaleDictionary>;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "Locale",
7
- "version": "0.1.0",
7
+ "version": "0.1.1",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -37,5 +37,5 @@
37
37
  "devDependencies": {
38
38
  "@types/lodash.merge": "4.6.9"
39
39
  },
40
- "gitHead": "e38849932e4595cfeaf19733fcd296ffcfc7322e"
40
+ "gitHead": "0e3f0ca15d2f0903a25bfa72bd95da9a414bddc2"
41
41
  }
@@ -4,13 +4,15 @@ import { createContext, ReactNode, useMemo } from 'react';
4
4
  import { LOCALES } from '../../locales';
5
5
  import { LocaleLang, OverrideLocales } from '../../types';
6
6
 
7
+ export const DEFAULT_LANG = 'en_GB';
8
+
7
9
  export type LocaleContextType = {
8
10
  lang: LocaleLang;
9
11
  locales: OverrideLocales;
10
12
  };
11
13
 
12
14
  export const LocaleContext = createContext<LocaleContextType>({
13
- lang: 'en_US',
15
+ lang: DEFAULT_LANG,
14
16
  locales: LOCALES,
15
17
  });
16
18
 
@@ -20,7 +22,7 @@ export type LocaleProviderProps = {
20
22
  children: ReactNode;
21
23
  };
22
24
 
23
- export function LocaleProvider({ lang = 'en_US', locales: localesProp, children }: LocaleProviderProps) {
25
+ export function LocaleProvider({ lang: langProp = DEFAULT_LANG, locales: localesProp, children }: LocaleProviderProps) {
24
26
  const locales = useMemo(() => {
25
27
  if (localesProp) {
26
28
  return merge({}, LOCALES, localesProp);
@@ -29,5 +31,7 @@ export function LocaleProvider({ lang = 'en_US', locales: localesProp, children
29
31
  return LOCALES;
30
32
  }, [localesProp]);
31
33
 
34
+ const lang = useMemo(() => langProp.replace('-', '_') as LocaleLang, [langProp]);
35
+
32
36
  return <LocaleContext.Provider value={{ lang, locales }}>{children}</LocaleContext.Provider>;
33
37
  }
@@ -1,7 +1,7 @@
1
1
  import { useContext, useMemo } from 'react';
2
2
 
3
3
  import { KnownLocaleLang, LocaleDictionary, LocaleLang } from '../../types';
4
- import { LocaleContext, LocaleContextType } from './LocaleProvider';
4
+ import { DEFAULT_LANG, LocaleContext, LocaleContextType } from './LocaleProvider';
5
5
 
6
6
  type LocaleComponentName = keyof LocaleDictionary;
7
7
 
@@ -18,7 +18,15 @@ export function useLocale<C extends LocaleComponentName = LocaleComponentName>(c
18
18
  const { locales: ctxLocales, lang } = useContext<LocaleContextType>(LocaleContext);
19
19
 
20
20
  const locales = useMemo(() => {
21
- const localesObj = ctxLocales[lang as KnownLocaleLang] || {};
21
+ let localesObj = ctxLocales[lang as KnownLocaleLang];
22
+
23
+ if (!localesObj) {
24
+ console.warn(
25
+ `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`,
26
+ );
27
+
28
+ localesObj = ctxLocales[DEFAULT_LANG] as LocaleDictionary;
29
+ }
22
30
 
23
31
  if (!componentName) {
24
32
  return localesObj as LocaleDictionary;
@@ -1,4 +1,4 @@
1
- export const en_US = {
1
+ export const en_GB = {
2
2
  Table: {
3
3
  searchPlaceholder: 'Search',
4
4
  noData: {
@@ -1,7 +1,7 @@
1
- import { en_US } from './en_US';
1
+ import { en_GB } from './en_GB';
2
2
  import { ru_RU } from './ru_RU';
3
3
 
4
4
  export const LOCALES = {
5
- en_US,
5
+ en_GB,
6
6
  ru_RU,
7
7
  } as const;
package/src/types.ts CHANGED
@@ -3,12 +3,8 @@ import { PartialDeep } from './typeUtils';
3
3
 
4
4
  export type KnownLocaleLang = keyof typeof LOCALES;
5
5
 
6
- type CustomLang = `${string}_${string}`;
6
+ export type LocaleLang = KnownLocaleLang | string;
7
7
 
8
- export type LocaleLang = KnownLocaleLang | CustomLang;
8
+ export type LocaleDictionary = typeof LOCALES.en_GB;
9
9
 
10
- export type LocaleDictionary = typeof LOCALES.en_US;
11
-
12
- export type OverrideLocales =
13
- | PartialDeep<Record<KnownLocaleLang, LocaleDictionary>>
14
- | Record<CustomLang, LocaleDictionary>;
10
+ export type OverrideLocales = PartialDeep<Record<KnownLocaleLang, LocaleDictionary>> | Record<string, LocaleDictionary>;