revite/src/lib/i18n.tsx

63 lines
1.6 KiB
TypeScript
Raw Normal View History

import { IntlContext, translate } from "preact-i18n";
import { useContext } from "preact/hooks";
2021-07-05 06:23:23 -04:00
import { Children } from "../types/Preact";
interface Fields {
2021-07-05 06:25:20 -04:00
[key: string]: Children;
}
interface Props {
2021-07-05 06:25:20 -04:00
id: string;
fields: Fields;
}
export interface IntlType {
2021-07-05 06:25:20 -04:00
intl: {
dictionary: {
[key: string]: Object | string;
};
};
}
// This will exhibit O(2^n) behaviour.
function recursiveReplaceFields(input: string, fields: Fields) {
2021-07-05 06:25:20 -04:00
const key = Object.keys(fields)[0];
if (key) {
const { [key]: field, ...restOfFields } = fields;
if (typeof field === "undefined") return [input];
const values: (Children | string[])[] = input
.split(`{{${key}}}`)
.map((v) => recursiveReplaceFields(v, restOfFields));
for (let i = values.length - 1; i > 0; i -= 2) {
values.splice(i, 0, field);
}
return values.flat();
} else {
// base case
return [input];
}
}
export function TextReact({ id, fields }: Props) {
2021-07-05 06:25:20 -04:00
const { intl } = useContext(IntlContext) as unknown as IntlType;
2021-07-05 06:25:20 -04:00
const path = id.split(".");
let entry = intl.dictionary[path.shift()!];
for (let key of path) {
// @ts-expect-error
entry = entry[key];
}
2021-07-05 06:25:20 -04:00
return <>{recursiveReplaceFields(entry as string, fields)}</>;
}
export function useTranslation() {
2021-07-05 06:25:20 -04:00
const { intl } = useContext(IntlContext) as unknown as IntlType;
return (id: string, fields?: Object, plural?: number, fallback?: string) =>
translate(id, "", intl.dictionary, fields, plural, fallback);
}