FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 regels
1.6 KiB

  1. "use client";
  2. import { useTranslation } from "react-i18next";
  3. import type { TFunction } from "i18next";
  4. import type { ReportDefinition, ReportField } from "@/config/reportConfig";
  5. /** FP-MTMS Version Checklist | Functions Ref. No. 69 | v1.0.2 | 2026-09-10 */
  6. export function useReportLabels() {
  7. const { t, i18n } = useTranslation("report");
  8. const reportTitle = (report: Pick<ReportDefinition, "id" | "title">) =>
  9. t(`reports.${report.id}.title`, { defaultValue: report.title });
  10. const fieldLabel = (
  11. reportId: string,
  12. field: Pick<ReportField, "name" | "label">,
  13. ) =>
  14. t(`reports.${reportId}.fields.${field.name}`, {
  15. defaultValue: field.label,
  16. });
  17. const fieldHint = (reportId: string, fieldName: string, fallback = "") => {
  18. const key = `reports.${reportId}.fieldHints.${fieldName}`;
  19. if (!i18n.exists(key, { ns: "report" })) return fallback;
  20. return String(t(key, { defaultValue: fallback }));
  21. };
  22. const optionLabel = (
  23. reportId: string,
  24. fieldName: string,
  25. opt: { label: string; value: string },
  26. ) =>
  27. t(
  28. [
  29. `reports.${reportId}.options.${fieldName}.${opt.value}`,
  30. `options.${opt.value}`,
  31. ],
  32. { defaultValue: opt.label },
  33. );
  34. const categoryTitle = (id: string, fallback: string) =>
  35. t(`categories.${id}`, { defaultValue: fallback });
  36. return { t, i18n, reportTitle, fieldLabel, fieldHint, optionLabel, categoryTitle };
  37. }
  38. export function reportExcelT(
  39. t: TFunction | undefined,
  40. key: string,
  41. fallback: string,
  42. ): string {
  43. if (!t) return fallback;
  44. return String(t(key, { defaultValue: fallback }));
  45. }