Kurisu
How would you recommend providing all countries collection to a phoenix form select?
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.
Marked As Solved
danschultzer
You should use CLDR for that: GitHub - elixir-cldr/cldr_territories: Territory formatting functions for the Common Locale Data Repository (CLDR) package https://github.com/elixir-cldr/cldr · GitHub
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.
Also Liked
kip
I’ve added a territory select helper to ex_cldr_html, there’s an explanation in this forum thread: https://forum.elixirforum.com/t/select-field-with-all-country-codes
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.
kip
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.
Last Post!
kip
I’ve added a territory select helper to ex_cldr_html, there’s an explanation in this forum thread: https://forum.elixirforum.com/t/select-field-with-all-country-codes
Popular in Questions
Other popular 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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










