From 1675d66b67be3225949892ba0caf9d089389875b Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 11 Dec 2025 00:51:26 +0100 Subject: [PATCH] translate field names for visibility dropdown --- .../field_visibility_dropdown_component.ex | 20 ++++++++- lib/mv_web/translations/member_fields.ex | 41 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 lib/mv_web/translations/member_fields.ex diff --git a/lib/mv_web/live/components/field_visibility_dropdown_component.ex b/lib/mv_web/live/components/field_visibility_dropdown_component.ex index 642273c..5fc0abf 100644 --- a/lib/mv_web/live/components/field_visibility_dropdown_component.ex +++ b/lib/mv_web/live/components/field_visibility_dropdown_component.ex @@ -152,9 +152,25 @@ defmodule MvWeb.Components.FieldVisibilityDropdownComponent do defp field_to_string(field) when is_atom(field), do: Atom.to_string(field) defp field_to_string(field) when is_binary(field), do: field - defp format_field_label(field) do + defp format_field_label(field) when is_atom(field) do + MvWeb.Translations.MemberFields.label(field) + end + + defp format_field_label(field) when is_binary(field) do + case safe_to_existing_atom(field) do + {:ok, atom} -> MvWeb.Translations.MemberFields.label(atom) + :error -> fallback_label(field) + end + end + + defp safe_to_existing_atom(string) do + {:ok, String.to_existing_atom(string)} + rescue + ArgumentError -> :error + end + + defp fallback_label(field) do field - |> field_to_string() |> String.replace("_", " ") |> String.split() |> Enum.map_join(" ", &String.capitalize/1) diff --git a/lib/mv_web/translations/member_fields.ex b/lib/mv_web/translations/member_fields.ex new file mode 100644 index 0000000..3750bcb --- /dev/null +++ b/lib/mv_web/translations/member_fields.ex @@ -0,0 +1,41 @@ +defmodule MvWeb.Translations.MemberFields do + @moduledoc """ + Helper module to dynamically translate member field names. + + ## Features + - Translates technical field names (atoms) to human-friendly localized text + - Used primarily in the field visibility dropdown component + + ## Example + + iex> MvWeb.Translations.MemberFields.label(:first_name) + "Vorname" # when locale is "de" + + iex> MvWeb.Translations.MemberFields.label(:first_name) + "First Name" # when locale is "en" + """ + use Gettext, backend: MvWeb.Gettext + + @spec label(atom()) :: String.t() + def label(:first_name), do: gettext("First Name") + def label(:last_name), do: gettext("Last Name") + def label(:email), do: gettext("Email") + def label(:paid), do: gettext("Paid") + def label(:phone_number), do: gettext("Phone") + def label(:join_date), do: gettext("Join Date") + def label(:exit_date), do: gettext("Exit Date") + def label(:notes), do: gettext("Notes") + def label(:city), do: gettext("City") + def label(:street), do: gettext("Street") + def label(:house_number), do: gettext("House Number") + def label(:postal_code), do: gettext("Postal Code") + + # Fallback for unknown fields + def label(field) do + field + |> to_string() + |> String.replace("_", " ") + |> String.split() + |> Enum.map_join(" ", &String.capitalize/1) + end +end