I need to support translations or at least show the countries in French.
All Hex packages I found so far don’t support i8n and will just show the names in English.
I’m hesitating between creating a table for countries in the database, or just use a long list without database. It seems to me that this is how the packages I found work though.
Is there maybe a package that you would recommend to me. I just need the countries names and maybe their ISO prefix.
As an example, we’ve used this to show a list of countries (with unicode flag):
defmodule MyAppWeb.RegistrationView do
use MyAppWeb, :view
alias Cldr.Territory
def country_select(form, opts \\ []) do
select(form, :address_country, country_options(), opts)
end
def country_options() do
Territory.country_codes()
|> Enum.map(&country_option/1)
|> Enum.sort(&(&1[:key] <= &2[:key]))
end
def country_option(country) do
[key: Territory.from_territory_code!(country),
value: country,
data_label: Territory.to_unicode_flag!(country)]
end
end
You could use the translate_territory/3 method or if you only support french then just set the locale for that.
Does anyone know where the translations come from? How they are generated and where they are stored? Unfortunately it doesn’t say in the docs
cc @kip ?
The localisation data comes from the CLDR project. The source is on github.
For ex_cldr and friends, the localisation data is baked into the code in a backend module. Using the example above, Territory.from_territory_code!(country, MyApp.Cldr), MyApp.Cldr is a backend module.
The serialised data can be explored, if you’re curious, by looking at the output from Cldr.Config.get_locale("en", %Cldr.Config{locales: :all}). Please note this is not a function to be used at runtime, its for compile time usage or exploration purposes only.
CLDR has deprecated telephone code data so it hasn’t been included in any ex_cldr or related library to my knowledge.
Depending on your requirements you might find ex_phone_number meets your needs. If you’re after a wrapper for Google’s libphonenumber which is the canonical library for parsing, formatting, and validating international phone numbers you might find elibphonenumber closer to your requirement although it is a NIF so has a more complex build process.
Lastly, if none of these meets your needs, let me know what the use case is - its an interesting topic (like postal codes) but just not currently top of my list of things to do.