elixir-cldr / cldr_territories

Territory formatting functions for the Common Locale Data Repository (CLDR) package https://github.com/elixir-cldr/cldr
Other
23 stars 11 forks source link

Cldr.UnknownStyleError raised when setting territory style to :short #20

Closed DaTrader closed 2 years ago

DaTrader commented 2 years ago

The following fails:

MyApp.Cldr.Territory.from_territory_code!( :de, style: :short)

Schultzer commented 2 years ago

If a style does not exist, an error is raised.

The same as from_territory_code/2, but raises an exception if it fails.

Example

 iex> Cldr.Territory.from_territory_code!(:GB, TestBackend.Cldr)
 "United Kingdom"

 iex> Cldr.Territory.from_territory_code!(:GB, TestBackend.Cldr, [style: :short])
 "UK"

 iex> Cldr.Territory.from_territory_code!(:GB, TestBackend.Cldr, [locale: "pt"])
 "Reino Unido"

This is an expected behavior and outline in the docs: https://hexdocs.pm/ex_cldr_territories/Cldr.Territory.html#from_territory_code!/3.

If you believe that this is a mistake then I believe the proper channel for this would be upstream in Unicode CLDR.

https://cldr.unicode.org/#h.vw32p8sealpj https://cldr.unicode.org/index/bug-reports https://github.com/unicode-org/cldr

If this has already been fixed upstream, then this would require a new version of ex_cldr.

DaTrader commented 2 years ago

Ok, but how does one verify if a style does not exist for a particular territory?

DaTrader commented 2 years ago

I see, the errors are not just errors, but legit alternative scenarios, so I should invoke the from_territory_code/3 and match against { :error, ..} .

Schultzer commented 2 years ago

Ok, but how does one verify if a style does not exist for a particular territory?

You can use the known_territories/1 to verify what style each territory has.

I see, the errors are not just errors, but legit alternative scenarios, so I should invoke the from_territory_code/3 and match against { :error, ..} .

Yeah, depending on your use-case then you could build a function in your MyWeb.Cldr, that would try to translate into the short version or fallback to standard.

DaTrader commented 2 years ago

Ok. Thanks!