Kurisu

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

danschultzer

Pow Core Team

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.

11
Post #2

Also Liked

kip

kip

ex_cldr Core Team

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

kip

ex_cldr Core Team

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

kip

ex_cldr Core Team

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

kip

ex_cldr Core Team

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

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement