translate field names for visibility dropdown
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Simon 2025-12-11 00:51:26 +01:00
parent 8512be0282
commit 1675d66b67
Signed by: simon
GPG key ID: 40E7A58C4AA1EDB2
2 changed files with 59 additions and 2 deletions

View file

@ -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_atom(field), do: Atom.to_string(field)
defp field_to_string(field) when is_binary(field), do: 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
|> field_to_string()
|> String.replace("_", " ") |> String.replace("_", " ")
|> String.split() |> String.split()
|> Enum.map_join(" ", &String.capitalize/1) |> Enum.map_join(" ", &String.capitalize/1)

View file

@ -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