chore: better path getter func
This commit is contained in:
parent
26c21c2de8
commit
8c4aed699d
|
@ -116,16 +116,10 @@ async function getTranslation(node: Node): Promise<string | null> {
|
|||
await readFile(`./translations/en/${namespace}.json`, "utf-8")
|
||||
);
|
||||
|
||||
function getByPath(key: string, object: any) {
|
||||
try {
|
||||
return key.split(".").reduce((obj, key) => obj[key], object);
|
||||
} catch {
|
||||
// errors if the object doesn't contain the key
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
const dotProp = (key: string, object: any) =>
|
||||
key.split(".").reduce((obj, key) => obj?.[key], object);
|
||||
|
||||
return getByPath(path, bundle);
|
||||
return dotProp(path, bundle);
|
||||
}
|
||||
|
||||
async function parseFile(fileName: string) {
|
||||
|
|
|
@ -74,20 +74,14 @@ function format(source: string, variables: Record<string, any>) {
|
|||
}
|
||||
|
||||
// converts a dot-notation path to an object value
|
||||
function getByPath(key: string, object: any) {
|
||||
try {
|
||||
return key.split(".").reduce((obj, key) => obj[key], object);
|
||||
} catch {
|
||||
// errors if the object doesn't contain the key
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
const dotProp = (key: string, object: any) =>
|
||||
key.split(".").reduce((obj, key) => obj?.[key], object);
|
||||
|
||||
type Translation = string | ({ [rule in Intl.LDMLPluralRule]?: string } & { other: string; });
|
||||
|
||||
// translation retrieval function
|
||||
function _t(key: string, bundle: any): Translation {
|
||||
const translation = getByPath(key, bundle);
|
||||
const translation = dotProp(key, bundle);
|
||||
|
||||
if (!translation) {
|
||||
if (bundle !== translations.en) {
|
||||
|
|
Loading…
Reference in a new issue