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 v0.24.0
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.
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
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:
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.
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?
I checked the .pot files @kip but no Plural-Forms: header.
I think you pinpointed the source of the exception though, as Application.get_env(:gettext, :plural_forms, Gettext.Plural) resolves to the custom module, and the exception occurs when nplurals/1 is called on it. Perhaps there’s something more I need to do for the mix task to be able to access it?
Task such as mix gettext.merge use the plural backend configured under the :gettext application, so in general the global configuration approach is preferred.
Because of that, use the global configuration:
# For example, in config/config.exs
config :gettext, :plural_forms, MyApp.Plural
Instead of the configuration for a specific backend module:
defmodule MyApp.Gettext do
use Gettext, otp_app: :my_app, plural_forms: MyApp.Plural
end