Support translations in Fluxon components

This commit is contained in:
Claudio Ortolina
2025-07-05 13:40:45 +01:00
parent 6f4aca671b
commit 40bb95e52d
2 changed files with 20 additions and 0 deletions
+2
View File
@@ -97,6 +97,8 @@ config :music_library, MusicLibrary.ErrorRepo, priv: "priv/error_repo"
config :music_library, MusicLibrary.BackgroundRepo, priv: "priv/background_repo" config :music_library, MusicLibrary.BackgroundRepo, priv: "priv/background_repo"
config :fluxon, :translate_function, &MusicLibraryWeb.CoreComponents.translate_error/1
# Import environment specific config. This must remain at the bottom # Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above. # of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs" import_config "#{config_env()}.exs"
@@ -118,4 +118,22 @@ defmodule MusicLibraryWeb.CoreComponents do
"opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"} "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"}
) )
end end
def translate_error({msg, opts}) do
# When using gettext, we typically pass the strings we want
# to translate as a static argument:
#
# # Translate the number of files with plural rules
# dngettext("errors", "1 file", "%{count} files", count)
#
# However the error messages in our forms and APIs are generated
# dynamically, so we need to translate them by calling Gettext
# with our gettext backend as first argument. Translations are
# available in the errors.po file (as we use the "errors" domain).
if count = opts[:count] do
Gettext.dngettext(MusicLibraryWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(MusicLibraryWeb.Gettext, "errors", msg, opts)
end
end
end end