elixir-cldr / cldr_units

Unit formatting (volume, area, length, ...) functions for the Common Locale Data Repository (CLDR)
Other
16 stars 13 forks source link

Additional units aren't accessible #40

Closed kipcole9 closed 10 months ago

kipcole9 commented 10 months ago

I was checking how to add new units to Cldr following the documentation but I failed.

I added two new units alarm and person, (the settings are listed below).

In iex I tried to get the new alarm unit, but it says it does not exist. the unit gallon works fine, as in the example.

MyApp.Cldr.Unit.to_string Cldr.Unit.new!(:alarm, 1) ** (Cldr.UnknownUnitError) Unknown unit was detected at "alarm" (ex_cldr_units 3.15.0) lib/cldr/unit.ex:1021: Cldr.Unit.new!/3 Exploring with iex, I manage to see that the new units are there:

iex> Citymonitor.Cldr.Unit.Additional.additional_units [:alarm, :person]

iex> {:ok, dic_de} = Citymonitor.Cldr.Unit.unit_strings_for :de iex> dic_de %{ {"alarme", [:alarm]}, {"alarmen", [:alarm]}, ... }

iex> {:ok, dic_en} = Citymonitor.Cldr.Unit.unit_strings_for :en iex> dic_en %{ {"al.", [:alarm]}, {"alarm", [:alarm]}, ... } I was wondering if the custom units should be merged into the core units, but I don’t know how or where to do this:

Cldr.Unit.known_units() |> merge_base_units()

I would appreciate any hints or tips.

Here is the rest of the configuration.

iex> Citymonitor.Cldr.Unit.Additional.additional_units [:alarm, :person]

File: mix.exs

{:ex_cldr, "~> 2.34"}, {:ex_cldr_numbers, "~> 2.29"}, {:ex_cldr_units, "~> 3.15"}, {:ex_cldr_plugs, "~> 1.2"}, {:ex_cldr_dates_times, "~> 2.13.1"}, ...

File: config/config.exs

localization

config :ex_cldr, default_backend: MyApp.Cldr

...

Cldr Units

config :ex_cldr_units, default_backend: MyApp.Cldr, additional_units: [ alarm: [base_unit: :unit, factor: 1], person: [base_unit: :unit, factor: 1] ]

File: lib/myapp/cldr/cldr.ex

defmodule Citymonitor.Cldr do use Cldr.Unit.Additional

use Cldr, locales: ["en", "de"], default_locale: "de", fuzzy_threshold: 1, gettext: MyApp.Gettext, providers: [Cldr.Number, Cldr.Unit, Cldr.List, Cldr.DateTime, Cldr.Calendar]

Alarm

unit_localization(:alarm, "en", :long, nominative: %{ one: "{0} alarm", other: "{0} alarm" }, display_name: "alarm" )

unit_localization(:alarm, "en", :short, nominative: %{ one: "{0} al", other: "{0} al" }, display_name: "al" )

unit_localization(:alarm, "en", :narrow, nominative: %{ one: "{0} a", other: "{0} a" }, display_name: "a" )

unit_localization(:alarm, "de", :long, nominative: %{ one: "{0} Alarme", other: "{0} Alarmen" }, display_name: "Alarme" )

unit_localization(:alarm, "de", :short, nominative: %{ one: "{0} Al.", other: "{0} Al." }, display_name: "Al." )

unit_localization(:alarm, "de", :narrow, nominative: %{ one: "{0} a", other: "{0} a" }, display_name: "a" )

Person

unit_localization(:person, "en", :long, nominative: %{ one: "{0} person", other: "{0} people" }, display_name: "people" )

unit_localization(:person, "en", :short, nominative: %{ one: "{0} per", other: "{0} ppl" }, display_name: "ppl" )

unit_localization(:person, "en", :narrow, nominative: %{ one: "{0} p", other: "{0} p" }, display_name: "p" )

unit_localization(:person, "de", :long, nominative: %{ one: "{0} Person", other: "{0} Personen" }, display_name: "Personen" )

unit_localization(:person, "de", :short, nominative: %{ one: "{0} Per", other: "{0} Prn" }, display_name: "Prn" )

unit_localization(:person, "de", :narrow, nominative: %{ one: "{0} p", other: "{0} p" }, display_name: "p" ) end

kipcole9 commented 10 months ago

Seems this is a result of a small bug in ex_cldr_units which I've now fixed. Would you please try configuring ex_cldr_units from GitHub to validate. You should only need to update your deps:

def deps do
  [
    {:ex_cldr_units, github: "elixir-cldr/cldr_units"},
    ...
  ]
end
Snake-Sanders commented 10 months ago

I tested it and It works! Thank you so much.