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

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement