Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,81 @@
|
||||
import type { TFunction, i18n } from "i18next";
|
||||
|
||||
export function translateLabel(t: TFunction, i18n: Pick<i18n, "exists"> | null | undefined, value: string | undefined | null): string {
|
||||
if (!value) {
|
||||
return "";
|
||||
}
|
||||
if (i18n?.exists(value)) {
|
||||
return String(t(value));
|
||||
}
|
||||
return isKeyShapedLabel(value) ? String(t("shared.invalid")) : value;
|
||||
}
|
||||
|
||||
function translateLabelWithFallbacks(
|
||||
t: TFunction,
|
||||
i18n: Pick<i18n, "exists"> | null | undefined,
|
||||
value: string | undefined | null,
|
||||
fallbackKeys: string[]
|
||||
): string {
|
||||
if (!value) {
|
||||
return "";
|
||||
}
|
||||
if (i18n?.exists(value)) {
|
||||
return String(t(value));
|
||||
}
|
||||
const fallbackKey = fallbackKeys.find((key) => i18n?.exists(key));
|
||||
if (fallbackKey) {
|
||||
return String(t(fallbackKey));
|
||||
}
|
||||
return isKeyShapedLabel(value) ? String(t("shared.invalid")) : value;
|
||||
}
|
||||
|
||||
export function translateEntityTitle(
|
||||
t: TFunction,
|
||||
i18n: Pick<i18n, "exists"> | null | undefined,
|
||||
entity: { name: string; title?: string | null }
|
||||
): string {
|
||||
return translateLabelWithFallbacks(t, i18n, entity.title, [`models.${entity.name}.object`]);
|
||||
}
|
||||
|
||||
export function translateEntityTitlePlural(
|
||||
t: TFunction,
|
||||
i18n: Pick<i18n, "exists"> | null | undefined,
|
||||
entity: { name: string; titlePlural?: string | null }
|
||||
): string {
|
||||
return translateLabelWithFallbacks(t, i18n, entity.titlePlural, [`models.${entity.name}.plural`]);
|
||||
}
|
||||
|
||||
export function translatePropertyTitle(
|
||||
t: TFunction,
|
||||
i18n: Pick<i18n, "exists"> | null | undefined,
|
||||
entityName: string,
|
||||
property: { name: string; title?: string | null }
|
||||
): string {
|
||||
return translateLabelWithFallbacks(t, i18n, property.title, [`models.${entityName}.${property.name}`]);
|
||||
}
|
||||
|
||||
export function translateEntityViewTitle(
|
||||
t: TFunction,
|
||||
i18n: Pick<i18n, "exists"> | null | undefined,
|
||||
entityName: string,
|
||||
view: { name: string; title?: string | null }
|
||||
): string {
|
||||
return translateLabelWithFallbacks(t, i18n, view.title, [`models.${entityName}.views.${view.name}`]);
|
||||
}
|
||||
|
||||
export function translateSelectOptionLabel(
|
||||
t: TFunction,
|
||||
i18n: Pick<i18n, "exists"> | null | undefined,
|
||||
entityName: string,
|
||||
propertyName: string,
|
||||
option: { value: string; name?: string | null }
|
||||
): string {
|
||||
return translateLabelWithFallbacks(t, i18n, option.name ?? option.value, [
|
||||
`models.${entityName}.${propertyName}.options.${option.value}`,
|
||||
`models.${entityName}.${propertyName}Options.${option.value}`,
|
||||
]);
|
||||
}
|
||||
|
||||
export function isKeyShapedLabel(value: string) {
|
||||
return /^[a-z][A-Za-z0-9]*(?:[._][A-Za-z0-9-]+)+$/.test(value);
|
||||
}
|
||||
Reference in new issue
Block a user