lenards
I did search on the forums here to see if there was a previous answer and didn’t come up with a clear match.
I’m working on a Phoenix app that is using gettext for text translations. I user will have a preferred language, so we’ll be serving up pages using that. However, for unauthenticated pages, we’ll need to read and respond to the Accept-Language headers.
I’ve been evaluating using the Plug from ex_cldr_plug and it’s remarkably robust. However it seems like the locale naming for Gettext isn’t aligned with those in CLDR.
ling 893 files (.ex)
warning: The locales ["de-DE", "en-US"] are configured in the Elixir.MyPhoenixApp.Gettext gettext backend but are unknown to CLDR. They will be ignored by CLDR.
Generating MyPhoenixApp.Cldr for 3 locales named [:de, :en, :und] with a default locale named :en
config/config.ex
# Configure for Internationalization (i18n)
config :myphxapp, MyPhoenixApp.Gettext,
default_locale: "en_US",
locales: ["en_US", "de_DE"]
config :ex_cldr,
default_locale: "en",
default_backend: MyPhoenixApp.Cldr,
json_library: Jason
lib/myphxapp/cldr.ex
defmodule MyPhoenixApp.Cldr do
use Cldr,
locales: ["en", "de"],
default_locale: "en",
add_fallback_locales: false,
gettext: MyPhoenixApp.Gettext,
data_dir: "./priv/cldr",
otp_app: :myphxapp,
precompile_number_formats: ["¤¤#,##0.##"],
precompile_transliterations: [{:latn, :arab}, {:thai, :latn}],
# we could include providers for Numbers, etc. For now: empty
providers: [],
generate_docs: true,
force_locale_download: false
end
lib/myphxapp/gettext.ex
defmodule MyPhoenixApp.Gettext do
use Gettext, otp_app: :myphxapp
end
I believe that Gettext is using POSIX locale name from what I can see in an earlier version of the ex_cldr readme:
Since
Gettextuses the Posix locale name format (locales with an ‘_‘ in them) andCldruses the Unicode format (a ‘-‘ as the subtag separator),Cldrwill transliterate locale names fromGettextinto theCldrcanonical form.
I imagine the warning is hinting at this being more than just a format definition though (so not just underscore _ vs dash -).
I realize that de_DE is not populated in CLDR from Kip’s response here:
Is there a way to address the warning and allow Gettext’s locales to coexist with CLDR’s definitions?
Or, I’m not sure if the correct move to is change from en_US & de_DE to simply en and de?
I know that the application will need to support en_GB and en_CA soon, so that’s why I was attempting so use the format for language-code / country code.
Hopefully I’ve provided enough configuration details (and not too much).
Thanks in advance
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
@lenards, thanks for the detailed message. This is a poor error message. It’s correct as far as it goes, but it doesn’t explain well enough.
TLDR; While
en-USisn’t known to CLDR, setting your CLDR toen-USwill correctly link it to the Gettext localeen_US.During the configuration phase (ie at compilation time),
ex_cldrtries to resolve the required locale names by combining the requestedex_cldrlocale names with the configured Gettext locale names (as you correctly identified).At configuration time the key thing of interest is “Does CLDR have a data repository for this locale”. For
en_USandde_DEit does not since by the rules of CLDR, the data inenis the data foren-USand the data fordeis the data forde-DE.At runtime, however, when validating a locale derived from the
Accept-Languageheader or elsewhere,ex_cldrwill still try to find aGettextlocale to match with the requested locale. Using your example configuration you could try this:If for some reason thats not what you see (I tested it in my dev environment and it appears to be working as expected) please do open an issue.
lenards
Thanks Kip!
All that information I included and I forgot to mention that I was seeing
#Cldr.LanguageTag<de-DE [validated]>when I was sending in a header valueAccept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7when using the Plug.I’ll try changing the CLDR locales quick.
kip
Yes, thats exactly what you would expect to see!
Requesting a locale
de-DE(which has a 1.0 weighting in the header and therefore the highest priority to match) will match theex_cldrconfigured locale ofde. It will use the extra provided information of the territoryDEto note that thats the users preferred territory for localisation.In short, configuration is about “what data do I have available in CLDR to use for localisation”. Runtime is about “what’s the best fit data available to match with what the user requested”
kip
One way to explore what is being resolved is to look at some of the data in the language tag struct:
lenards
I might have misunderstood about how to eliminate the warning.
Leaving the
MyPhoenixApp.Gettextconfigured the same.If I change the CLDR configuration to:
And then alter
lib/myphxapp/cldr.exto the following:I’m failing a compile time check:
So I’m still not sure I see how to tackle the warning here.
lenards
Exploring through
iexis a great reminder. I definitely need to lean more on that as an approachkip
There isn’t a way to eliminate the warning in this case. During compilation I try to be as explicit as possible - locale matching is complicated enough as is. But if you’re running with “warnings as errors” this would be problematic so sounds like this needs to be revisited since it is just a notification - there is nothing “wrong”.
Open to suggestions - what would you suggest is the correct behaviour? Emit the message, but not as a warning? Something else?
kip
The locales configuration must be an exact match to the locales available in CLDR. There’s no clever matching in this part.
lenards
I was seeking to understand the warning, so I’m not sure I have a great suggestion in mind right now.
The concern for me was the phrase “are unknown to CLDR” in the warning:
Because the locales appears in the
-format, which is the convention forex_cldr, and not forgettextI was even more curious what was up (because that seemed to imply some “smart, or savvy, evaluation” of the configuration by CLDR).Let me ponder a bit and see there is a more helpful or “wiser” response inside me that isn’t available at the moment.
Thank you again for all the effort on the
ex_cldr*libraries and your prompt, thorough responses!kip
Thanks for the encouragement!
I have changed both the error message and the way its printed. It no longer uses
IO.warn/2so it won’t trigger compilation errors if you usewarnings_as_errors: true. The message now reads:BTW, you can also add
suppress_warnings: trueto your CLDR backend configuration and it won’t print warnings. I’ve just seen that its not honoured in all cases - but will be in this next release.I’ll push a release in a few hours with this change.