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,60 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { translateEntityTitle, translateEntityTitlePlural, translateLabel, translatePropertyTitle, translateSelectOptionLabel } from "./translateLabel";
|
||||
|
||||
describe("translateLabel", () => {
|
||||
it("translates known keys, keeps literal labels, and hides missing key-shaped labels", () => {
|
||||
const t = vi.fn((key: string) => `translated:${key}`);
|
||||
const i18n = { exists: vi.fn((key: string) => key === "shared.name") };
|
||||
|
||||
expect(translateLabel(t as any, i18n as any, "shared.name")).toBe("translated:shared.name");
|
||||
expect(translateLabel(t as any, i18n as any, "Custom field")).toBe("Custom field");
|
||||
expect(translateLabel(t as any, i18n as any, "E-Mail")).toBe("E-Mail");
|
||||
expect(translateLabel(t as any, i18n as any, "analytics.title")).toBe("translated:shared.invalid");
|
||||
expect(translateLabel(t as any, i18n as any, undefined)).toBe("");
|
||||
});
|
||||
|
||||
it("translates persisted English seed labels through stable model fallbacks", () => {
|
||||
const knownKeys = new Set([
|
||||
"models.contact.object",
|
||||
"models.contact.plural",
|
||||
"models.contact.firstName",
|
||||
"models.contact.status.options.customer",
|
||||
"models.company.sourceOptions.website",
|
||||
]);
|
||||
const t = vi.fn((key: string) => `translated:${key}`);
|
||||
const i18n = { exists: vi.fn((key: string) => knownKeys.has(key)) };
|
||||
|
||||
expect(translateEntityTitle(t as any, i18n as any, { name: "contact", title: "Contact" })).toBe("translated:models.contact.object");
|
||||
expect(translateEntityTitlePlural(t as any, i18n as any, { name: "contact", titlePlural: "Contacts" })).toBe("translated:models.contact.plural");
|
||||
expect(translatePropertyTitle(t as any, i18n as any, "contact", { name: "firstName", title: "First name" })).toBe("translated:models.contact.firstName");
|
||||
expect(translateSelectOptionLabel(t as any, i18n as any, "contact", "status", { value: "customer", name: "Customer" })).toBe(
|
||||
"translated:models.contact.status.options.customer"
|
||||
);
|
||||
expect(translateSelectOptionLabel(t as any, i18n as any, "company", "source", { value: "website", name: "Website" })).toBe(
|
||||
"translated:models.company.sourceOptions.website"
|
||||
);
|
||||
expect(translatePropertyTitle(t as any, i18n as any, "contact", { name: "custom", title: "Custom field" })).toBe("Custom field");
|
||||
});
|
||||
|
||||
it("translates entity-specific persisted labels for CRM and tenant settings", () => {
|
||||
const knownKeys = new Set([
|
||||
"models.tenantSettings.object",
|
||||
"models.tenantSettings.plural",
|
||||
"models.tenantSettings.website",
|
||||
"models.opportunity.expectedCloseDate",
|
||||
]);
|
||||
const t = vi.fn((key: string) => `translated:${key}`);
|
||||
const i18n = { exists: vi.fn((key: string) => knownKeys.has(key)) };
|
||||
|
||||
expect(translateEntityTitle(t as any, i18n as any, { name: "tenantSettings", title: "Tenant settings" })).toBe("translated:models.tenantSettings.object");
|
||||
expect(translateEntityTitlePlural(t as any, i18n as any, { name: "tenantSettings", titlePlural: "Tenant settings" })).toBe(
|
||||
"translated:models.tenantSettings.plural"
|
||||
);
|
||||
expect(translatePropertyTitle(t as any, i18n as any, "tenantSettings", { name: "website", title: "Website" })).toBe(
|
||||
"translated:models.tenantSettings.website"
|
||||
);
|
||||
expect(translatePropertyTitle(t as any, i18n as any, "opportunity", { name: "expectedCloseDate", title: "Expected close date" })).toBe(
|
||||
"translated:models.opportunity.expectedCloseDate"
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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