RealVidy

RealVidy

Hi, using gettext, I’m trying to define a locale variant to handle formal vs informal french (so “fr” vs “fr@formal” in my case).

I tried to add a plural module to my app, as specified in the docs, but it seems to completely ignore it.

defmodule MyApp.Plural do
  @behaviour Gettext.Plural

  # Fallback to Gettext.Plural
  def nplurals("fr@formal"), do: 2
  def nplurals(locale), do: Gettext.Plural.nplurals(locale)

  def plural("fr@formal", 0), do: 0
  def plural("fr@formal", 1), do: 1

  def plural(locale, n) do
    Gettext.Plural.plural(locale, n)
  end
end

defmodule MyApp.Gettext do
  use Gettext, otp_app: :my_app, plural_forms: MyApp.Plural
end

Running mix gettext.merge priv/gettext --locale fr@formal gives me

** (Gettext.Plural.UnknownLocaleError) unknown locale “fr@formal”. If this is a locale you need to handle,
consider using a custom pluralizer module instead of the default
Gettext.Plural. You can read more about this on the Gettext docs at
Gettext.Plural — gettext v1.0.2

What am I missing?

Showing Posts 1 to 10

kip

kip

ex_cldr Core Team

I suspect you may need to run mix gettext.merge priv/gettext --locale fr@formal before you configure your Gettext backend with your plurals module.

RealVidy

RealVidy OP

Thanks for your answer Kip, it seems that this doesn’t help unfortunately.

I removed plural_forms: MyApp.Plural in use Gettext, otp_app: :my_app, plural_forms: MyApp.Plural and ran the mix gettext.merge priv/gettext --locale fr@formal command. I get the same error.

kip

kip

ex_cldr Core Team

Sorry to hear that. I just created a new app with a gettext backend and no plurals module. And then:

% mix gettext.merge priv/gettext --locale fr@formal
Created directory priv/gettext/fr@formal/LC_MESSAGES

It seemed to be fine creating the locale files. Afraid I’m out of ideas as this stage.

RealVidy

RealVidy OP

Something must be wrong on my end then. Can I ask how you create your new app? I ran into the same issue with a phoenix app I created with a simple mix phx.new test-app

kip

kip

ex_cldr Core Team

I just just mix new my_app (ie not Phoenix) although that really shouldn’t matter.

eriklindgren

eriklindgren

Did you ever find a solution to this @RealVidy? I have the exact same issue.

RealVidy

RealVidy OP

Unfortunately, no :man_shrugging: I gave up on the idea.

I have not retried recently though.

APB9785

APB9785

Creator of ECSx

In the docs I noticed this line:

Notice that tasks such as mix gettext.merge use the plural backend configured under the :gettext application, so generally speaking the first format is preferred.

where the “first format” is setting the Plural module in config.exs like such:

config :gettext, :plural_forms, MyApp.Plural

Have you tried this as well?

eriklindgren

eriklindgren

Thanks for the suggestion @APB9785, yes, I read the same thing and that’s how I configure my custom plural module. It works perfectly when running the app but the mix task still fails.

There’s an easy workaround, you can provide the plural forms as an argument like this mix gettext.merge priv/gettext/ --locale custom_locale --plural-forms 2 as the task only seems to need the plural module to find the number of plural forms, but it would still be nice to understand what I’m doing wrong. I can also go into iex (with -S mix) and call the functions in my module just fine, though I guess that doesn’t prove much of anything.

I may well be doing something wrong, but it seems to at least be something missing from the docs about how to get this working in a Phoenix context.

kip

kip

ex_cldr Core Team

In Gettext.Merger the code to resolve the n_plurals for a locale centres around the following code:

  defp put_plural_forms_opt(opts, headers, locale) do
    Keyword.put_new_lazy(opts, :plural_forms, fn ->
      read_plural_forms_from_headers(headers) ||
        Application.get_env(:gettext, :plural_forms, Gettext.Plural).nplurals(locale)
    end)
  end

  defp read_plural_forms_from_headers(headers) do
    Enum.find_value(headers, fn header ->
      with "Plural-Forms:" <> rest <- header,
           "nplurals=" <> rest <- String.trim(rest),
           {plural_forms, _rest} <- Integer.parse(rest) do
        plural_forms
      else
        _other -> nil
      end
    end)
  end

Notice that resolving the n-plurals during a merge is:

      read_plural_forms_from_headers(headers) ||
        Application.get_env(:gettext, :plural_forms, Gettext.Plural).nplurals(locale)

And read_plural_forms_from_headers/1 received headers from the “old” .po file. In the case of a new locale, the “old” file is actually the .pot file. This from the mix task .

Therefore I wonder if the .pot file has a Plural-Forms: header that is superceding the custom plurals module?

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New
adr
Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities. Sup...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews