Snake-Sanders

Snake-Sanders

How to add custom measurement units with `Cldr.Unit.Additional`

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

Marked As Solved

kip

kip

ex_cldr Core Team

@Snake-Sanders, I don’t see anything immediately to explain why the new units aren’t being honoured. Additional units are merged at compile time - there is nothing you need to do to make this happen other than configure them as you have done.

I’ve created an issue on the repo and will dig into this today.

Where Next?

Popular in Questions Top

Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
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 49522 488
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement