How do you make use of gettext in a hex package?

I cannot seem to make it work. I made a simple hex package, it uses gettext library, place a locale (with its own translations), after I published it, I created another mix app to test it, but the package seems to not doing any translation.

The weird thing is, when I use the package locally, the translation works, but when I depend from the remote hex package, the translation won’t work.

Is there any example I can follow?

The weird thing is, when I use the package locally, the translation works, but when I depend from the remote hex package, the translation won’t work.

Sounds like either a missing configuration or mis-matched OTP configuration. Do you have a full reproducible example that we can git clone that shows the issue with steps the reproduce (like a failing testcase in that project so we can just mix deps.get && mix test)?

The mix test is green. (The gitlab pipeline also says so.)
Here it is:


git@gitlab.com:dwahyudi/say_number.git
https://gitlab.com/dwahyudi/say_number.git

I don’t have an answer to your original question (I will take a look) but if spelling numbers is what you are after, my lib ex_cldr_numbers might be enough for you:

iex> TestBackend.Cldr.Number.to_string 2018, format: :spellout_year, locale: "id"
{:ok, "dua ribu delapan belas"}
iex> TestBackend.Cldr.Number.to_string 18, format: :spellout, locale: "id"       
{:ok, "delapan belas"}
iex> TestBackend.Cldr.Number.to_string 23, format: :spellout_ordinal, locale: "id"
{:ok, "kedua puluh tiga"}

Neat. How do you make the gettext work?

It seems to work for me:

Kips-MacBook:Development kip$ mix new say
* creating README.md
...
Kips-MacBook:say kip$ mix deps.get
* Getting say_number (https://gitlab.com/dwahyudi/say_number)
...
New:
  gettext 0.16.1
* Getting gettext (Hex package)
Kips-MacBook:say kip$ iex -S mix
Erlang/OTP 21 [erts-10.1.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]

==> gettext
Compiling 1 file (.yrl)
Compiling 1 file (.erl)
Compiling 20 files (.ex)
Generated gettext app
==> say_number
Compiling 3 files (.ex)
Generated say_number app
==> say
Compiling 1 file (.ex)
Generated say app
Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> SayNumber.spell 18, "id"
"Delapan belas"
iex(2)> SayNumber.spell 18      
"Eighteen"

This is strange.

When I use gitlab as deps, it works.

          defp deps do
            [
              { :say_number, git: "git@gitlab.com:dwahyudi/say_number.git" }
            ]
          end

But not, if I use it from hex package.

  defp deps do
    [
      { :say_number, "~> 0.0.5" }
    ]
  end

Already mix deps.clean --all, mix deps.get over and over again.

In your package description you have:

files: ["lib", "mix.exs", "README*", "LICENSE*"],

I believe it should be:

files: ["lib", "priv", "mix.exs", "README*", "LICENSE*"],

ie, the priv directory needs to be included since that’s where the translations are.

1 Like

So this means, the files list is the published files into hex package?

Got it. :+1:

Update: it works now, many thanks @kip

1 Like