diff --git a/config/config.exs b/config/config.exs index f86f7d22..f307c4df 100644 --- a/config/config.exs +++ b/config/config.exs @@ -97,6 +97,8 @@ config :music_library, MusicLibrary.ErrorRepo, priv: "priv/error_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 # of this file so it overrides the configuration defined above. import_config "#{config_env()}.exs" diff --git a/lib/music_library_web/components/core_components.ex b/lib/music_library_web/components/core_components.ex index 5bb5a27e..75bffb5d 100644 --- a/lib/music_library_web/components/core_components.ex +++ b/lib/music_library_web/components/core_components.ex @@ -118,4 +118,22 @@ defmodule MusicLibraryWeb.CoreComponents do "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"} ) 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