- Create domain-specific .pot template files (auth, equipment, admin, emails) - Create corresponding English .po files for all domains - Add GettextHelpers module with domain-specific helper functions: * t() for default domain (common UI) * t_auth() for authentication flows * t_equipment() for equipment/device management * t_admin() for admin features * t_email() for email templates - Import GettextHelpers in html_helpers() for automatic availability in LiveViews/components - Add mix populate_english task to auto-fill English translations (msgstr = msgid) - Task properly handles plural forms and multi-line strings Phase 1 complete - infrastructure ready for module-by-module migration.
157 lines
3.5 KiB
Elixir
157 lines
3.5 KiB
Elixir
defmodule ToweropsWeb.GettextHelpers do
|
|
@moduledoc """
|
|
Domain-specific helper functions for Gettext translations.
|
|
|
|
Provides shorter syntax for common translation patterns:
|
|
- `t/1` - Common UI elements (default domain)
|
|
- `t_auth/1` - Authentication flows
|
|
- `t_equipment/1` - Equipment/device management
|
|
- `t_admin/1` - Admin features
|
|
- `t_email/1` - Email templates
|
|
|
|
Error messages use the standard `gettext/1` function.
|
|
|
|
## Examples
|
|
|
|
# Common UI
|
|
t("Save")
|
|
t("Cancel")
|
|
|
|
# Authentication
|
|
t_auth("Log in")
|
|
t_auth("Sign up")
|
|
|
|
# Equipment
|
|
t_equipment("Add Device")
|
|
t_equipment("Last polled: %{time}", time: formatted_time)
|
|
|
|
# Admin
|
|
t_admin("Impersonate User")
|
|
|
|
# Emails
|
|
t_email("Reset password instructions")
|
|
|
|
# Errors (use standard gettext)
|
|
gettext("can't be blank")
|
|
"""
|
|
|
|
@doc """
|
|
Translates a string in the default domain.
|
|
|
|
Used for common UI elements, navigation, generic actions.
|
|
"""
|
|
defmacro t(msgid) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("default", unquote(msgid))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string with interpolation in the default domain.
|
|
"""
|
|
defmacro t(msgid, bindings) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("default", unquote(msgid), unquote(bindings))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string in the auth domain.
|
|
|
|
Used for authentication flows, user management, login/signup.
|
|
"""
|
|
defmacro t_auth(msgid) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("auth", unquote(msgid))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string with interpolation in the auth domain.
|
|
"""
|
|
defmacro t_auth(msgid, bindings) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("auth", unquote(msgid), unquote(bindings))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string in the equipment domain.
|
|
|
|
Used for equipment/device management, SNMP, monitoring, alerts.
|
|
"""
|
|
defmacro t_equipment(msgid) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("equipment", unquote(msgid))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string with interpolation in the equipment domain.
|
|
"""
|
|
defmacro t_equipment(msgid, bindings) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("equipment", unquote(msgid), unquote(bindings))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string in the admin domain.
|
|
|
|
Used for admin-specific features like impersonation, GeoIP, audit logs.
|
|
"""
|
|
defmacro t_admin(msgid) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("admin", unquote(msgid))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string with interpolation in the admin domain.
|
|
"""
|
|
defmacro t_admin(msgid, bindings) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("admin", unquote(msgid), unquote(bindings))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string in the emails domain.
|
|
|
|
Used for email templates and subjects.
|
|
"""
|
|
defmacro t_email(msgid) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("emails", unquote(msgid))
|
|
end
|
|
end
|
|
|
|
@doc """
|
|
Translates a string with interpolation in the emails domain.
|
|
"""
|
|
defmacro t_email(msgid, bindings) do
|
|
quote do
|
|
require ToweropsWeb.Gettext
|
|
|
|
ToweropsWeb.Gettext.dgettext("emails", unquote(msgid), unquote(bindings))
|
|
end
|
|
end
|
|
end
|