Skip to content

Commit

Permalink
#65 Refactor useLabel to exclude Intl if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Beceic committed Apr 24, 2023
1 parent 57ae1ff commit c05cedd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
15 changes: 10 additions & 5 deletions libs/intl/src/useLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ import { CommonKeys } from "./IntlProvider";
import { useIntlContext } from "./useIntlContext";

export function useLabel(name: keyof CommonKeys): string {
const { dictionary, lang, commonKeys } = useIntlContext();
const intlContext = useIntlContext();

const key = commonKeys[name] || name;
if (intlContext) {
const { lang, dictionary, commonKeys } = intlContext;
const key = commonKeys[name] || name;

if (!dictionary?.translations?.[lang]?.[key] && !dictionary?.messages?.[key]) {
return defaultDictionary.translations?.[lang]?.[key] || defaultDictionary.messages[name];
if (!dictionary?.translations?.[lang]?.[key] && !dictionary?.messages?.[key]) {
return defaultDictionary.translations?.[lang]?.[key] || defaultDictionary.messages[name];
}

return (dictionary?.translations?.[lang]?.[key] || dictionary?.messages?.[key]) as string;
}

return (dictionary?.translations?.[lang]?.[key] || dictionary?.messages?.[key]) as string;
return "Required field";
}
7 changes: 0 additions & 7 deletions storybook/src/intl/Intl.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,17 @@ display components which depend on your location.
**The list of components which require your app to be wrapped inside `Intl Provider`**:

- Amount
- CheckboxGroup(field)
- Date
- DateInput(field)
- DatePicker
- DateRangeInput(field)
- DateTimeInput(field)
- TimeInput(field)
- DragZone(field)
- Input(field)
- Login
- Number
- NumberInput(field)
- RadioGroup(field)
- Select(field)
- Slider(field)
- Textarea(field)
- Toggle(field)
- TreeSelect(field)

## Default Intl Dictionary

Expand Down

0 comments on commit c05cedd

Please sign in to comment.