How to get Gettext in a hex package

Hello, I crated a hex package for Auth system (MsihkaAuth) that I want to make it a multi languages support, then I selected Gettext lib but I don’t know how to get all translations which use Gettext on my all installed plugins in my phoenix project especially my new lib.

then I changed my project in a Fork and use Gettext with custom module and use gettext config on it.
but after installing this lib in my new Phoenix project and use this command mix gettext.extract. it works just into phoenix not my plugin.

then Please help me to migrate and merge my translations in my plugin to main default gettext .pot

what is your suggestion?

The project is Forked which used gettext(just lib without using in Phoenix project):

I think if you really prefer your package to use its own gettext backend, it should come with the translations already extracted in its own priv folder.

But since different projects will likely need different locales you will probably end up with a lot of unused translations files. So in my opinion it’s better to let the phoenix project that uses the package handle those translations through a gettext domain. auth for example in your case. That is how Phoenix itself is handling the default errors messages for now. It generates an errors.pot file in any new project so that we can translate it in desired languages…

1 Like

if i understood correctly, you suggest me I should use client Phoenix project Gettext module!! yes? if yes how can I get name of his module name and import in my files that I need?
you think my way is good ?!! if I create a .pot file in my local and upload for my client then after downloading they can copy and paste it on their project default .pot? I didn’t test it

if I let the user uses his Gettext module, I should get module name on config file and import on my all file I needed but I couldn’t find a way to do this

invalid argument for import, expected a compile time atom or alias, got:...

Ah sorry, In fact I didn’t test the method that generates a pot file when setting up the package…

But what I do is injecting the auth app messages in some module that must be provided to the auth app config. Please see below a sample of my auth backend messages definition:

defmodule MyAuth.Messages do
  @moduledoc """
  Module that handles messages.

  ## Usage

      defmodule MyApp.Auth.Messages do
        use MyAuth.Messages, [gettext: MyApp.Gettext]

  Remember to add `messages_backend: MyApp.Auth.Messages` to your

  defmacro __using__([gettext: backend] = _opts) do
    quote do
      use Pow.Phoenix.Messages

      use Pow.Extension.Phoenix.Messages,
        extensions: [PowResetPassword, PowEmailConfirmation, PowInvitation]

      # import Gettext backend
      import unquote(backend)

      # Override pow messages here

      def user_not_authenticated(_conn),
        do: dgettext("auth", "You need to sign in to access this page.")

      def signed_in(_conn), do: dgettext("auth", "You have been logged in.")

      def signed_out(_conn), do: dgettext("auth", "You have been logged out.")

      def user_could_not_be_deleted(_conn),
        do: dgettext("auth", "Your account could not be deleted.")

      def auth_unauthorized_access(_conn),
        do: dgettext("auth", "You are not authorized to access the requested page.")

In my case I just write some auth library on top of Pow auth package so that i can reuse it my projects. Basically The Pow app will fetch in its config the messages_backend which is defined in the Phoenix app, to display specific messages. So in my projects I just define this module which need of course the phoenix app gettext backend to work as expected:

      defmodule MyApp.Auth.Messages do
        use MyAuth.Messages, [gettext: MyApp.Gettext]

Then I added to my Pow config:

# Pow config
config :my_app, :pow,
  messages_backend: MyApp.Auth.Messages

Next step when I run gettext extract --merge task from my_app root the auth translations files are generated according to my_app gettext config. In some new projects I follow almost the same steps but I can just copey and paste translations files that already exist.

I hope this will be helpful to you.