Skip to content

Commit

Permalink
#65 Improve Intl utilities & extend intlDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Beceic committed Apr 24, 2023
1 parent 7a4bedd commit 9014432
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
28 changes: 16 additions & 12 deletions libs/intl/src/Intl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ interface IntlProps {
}

export default function Intl({ name, params, children }: IntlProps) {
const { lang, dictionary, intlUtil } = useIntlContext();

if (!dictionary?.translations?.[lang]?.[name] && !dictionary?.messages?.[name]) {
return <>{name}</>;
const intlContext = useIntlContext();

if (intlContext) {
const { lang, dictionary, intlUtil } = intlContext;
if (!dictionary?.translations?.[lang]?.[name] && !dictionary?.messages?.[name]) {
return <>{name}</>;
}

return (
<>
{intlUtil.translate({ name, params, render: children }).map((it, i) => (
<React.Fragment key={i}>{it}</React.Fragment>
))}
</>
);
}

return (
<>
{intlUtil.translate({ name, params, render: children }).map((it, i) => (
<React.Fragment key={i}>{it}</React.Fragment>
))}
</>
);
return <></>;
}
27 changes: 16 additions & 11 deletions libs/intl/src/IntlProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import * as React from "react";

import _ from "lodash";
import { IntlContext as ReactIntlContext, IntlProvider as ReactIntlProvider, IntlShape } from "react-intl";

import { createNamedContext } from "@tiller-ds/util";
Expand All @@ -32,6 +33,7 @@ export type CommonKeys = {
autocompleteNoResults?: string;
selectNoResults?: string;
paginationSummary?: string;
pageResizerSummary?: string;
};

type IntlProviderProps = {
Expand All @@ -47,7 +49,7 @@ type IntlProviderProps = {
dictionary?: Dictionary;

/**
* Similar to dictionary prop, but serves as a function which generally receives your dictionary as a return value.
* Similar to dictionary prop, but represents a promise which returns your dictionary.
*/
loadDictionary?: ((lang: string) => Promise<Dictionary>) | undefined;

Expand Down Expand Up @@ -80,20 +82,23 @@ export type IntlContextType = IntlValue & {
intl: IntlShape;
};

const defaultKeyConfig: CommonKeys = {
required: "required",
autocompleteNoTags: "autocomplete.noTags",
autocompleteAddTag: "autocomplete.addTag",
autocompleteNoResults: "autocomplete.noResults",
selectNoResults: "select.noResults",
paginationSummary: "pagination.summary",
pageResizerSummary: "pageResizer.summary",
};

export const IntlContext = createNamedContext<IntlContextType>("IntlContext");

export function useIntl(
language: string,
dict: Dictionary | undefined = intlDictionary,
loadDictionary: ((lang: string) => Promise<Dictionary>) | undefined,
keyConfig: CommonKeys = {
required: "required",
autocompleteNoTags: "autocomplete.noTags",
autocompleteAddTag: "autocomplete.addTag",
autocompleteNoResults: "autocomplete.noResults",
selectNoResults: "select.noResults",
paginationSummary: "pagination.summary",
},
keyConfig: CommonKeys = defaultKeyConfig,
): IntlValue {
const [lang, setLang] = React.useState<string>(language);
const [dictionary, setDictionary] = React.useState<Dictionary>(dict || ({} as Dictionary));
Expand Down Expand Up @@ -133,9 +138,9 @@ export function useIntl(
changeLang,
dictionary,
intlUtil: new IntlUtil(dictionary, lang),
commonKeys: keyConfig,
commonKeys: _.merge(defaultKeyConfig, keyConfig),
}),
[lang, dictionary, changeLang],
[lang, changeLang, dictionary, keyConfig],
);
}

Expand Down
3 changes: 3 additions & 0 deletions libs/intl/src/intlDictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
"autocomplete.noResults": "No results for:",
"select.noResults": "No results",
"pagination.summary": "Showing {from} to {to} of {total} results",
"pageResizer.summary": "Show {pageSizeSelect} results per page",
},
translations: {
hr: {
Expand All @@ -17,6 +18,7 @@ export default {
"autocomplete.noResults": "Nema rezultata za:",
"select.noResults": "Nema rezultata",
"pagination.summary": "Prikazuje se {from} do {to} od {total} rezultata",
"pageResizer.summary": "Prikaži {pageSizeSelect} rezultata po stranici",
},
en: {
required: "Required field",
Expand All @@ -26,6 +28,7 @@ export default {
"autocomplete.noResults": "No results for:",
"select.noResults": "No results",
"pagination.summary": "Showing {from} to {to} of {total} results",
"pageResizer.summary": "Show {pageSizeSelect} results per page",
},
},
};
10 changes: 7 additions & 3 deletions libs/intl/src/useIntlContext.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import * as React from "react";

import { IntlContext } from "./IntlProvider";
import { IntlContext, IntlContextType } from "./IntlProvider";

export function useIntlContext() {
export function useIntlContext(returnDefault?: boolean) {
const context = React.useContext(IntlContext);

if (!context) {
if (!context && !returnDefault) {
return undefined;
}

if (!context && returnDefault) {
return { lang: "en" } as IntlContextType;
}

return context;
}

0 comments on commit 9014432

Please sign in to comment.