-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Are there any plans to support ranges in units? #33
Comments
@ribanez7, thanks for the suggestion. I'm not sure I completely understand what you're suggesting. I think what you are saying is something like: iex> Cldr.Unit.new(:kilogram, 100..1000) Is that the idea? I'm interested but I can see some challenges.
I'm curious about the use case you have in mind. Can you share an example? |
Sure. I am using a custom unit (person), to show company sizes. The japanese case gives us some juice since it also changes the way a range is formatted (using tilde instead of dash). So given this implementation: unit_localization(:person, "ja", :long,
nominative: %{
one: "{0} 人",
other: "{0} 人"
},
display_name: "人"
) Let's say I want something like: I would like something like:
But that implies calling The japanese case I was talking about would result in: MyApp.Cldr.Unit.to_string!(MyApp.Cldr.Unit.new!(:person 1..2), [style: :long, locale: :ja])
"1〜2人" |
I can't come up with examples of it using math operations. |
Thanks for the example, I understand better now. I think there is a way to do a unit version analogous to I am working on a series of updates for CLDR 43 data that will launch in about 3 weeks. Is that manageable timing for you? |
I can also implement |
Yep, that will work perfectly. For the meantime I have a helper function to do it "the kind-of-hardcoded-way", by calculating both things separately (the number.to_range_string and the unit.to_string) and placing them together just with a concat. |
* Adds Cldr.Unit.Range module and type * Adds Enumeration implementation for Cldr.Unit.Range.t * Adds Inspect implementation for Cldr.Unit.Range.t Unit Range formatting will be implemented next in this module. I can't be implemented in Cldr.Number because that would create a circular dependency. This commit is targeted for ex_cldr_units version 3.16.0 to coincide with CLDR 43 in April.
I've closed this issue for now. Still on track for shipping in a couple of weeks. When everything across all the cldr projects is merged it'll be easier for you to test for your own use cases. For now, here's an example: iex> {:ok, range} = Cldr.Unit.Range.new(Cldr.Unit.new!(:gram, 1), Cldr.Unit.new!(:gram, 5))
iex> Cldr.Unit.to_string(range, locale: :ja)
{:ok, "1~5 グラム"} |
Many thanks @kipcole9 , that's awesome. |
You're welcome. As a bonus, iex> range = Cldr.Unit.Range.new!(Cldr.Unit.new!(:gram, 1), Cldr.Unit.new!(:gram, 5))
iex> Enum.to_list(range)
[Cldr.Unit.new!(:gram, 1), Cldr.Unit.new!(:gram, 2), Cldr.Unit.new!(:gram, 3),
Cldr.Unit.new!(:gram, 4), Cldr.Unit.new!(:gram, 5)] |
Yess, I've seen that going through the code, brilliant 👍🏼 |
Currently it is possible to pass float, integer, Decimal or Ratio, but would it be a possibility to pass a range? Or maybe this should be responsibility of
Cldr.Number.to_range_string
?If we include it in
Cldr.Number.to_range_string
, maybe it can be passed as an option, like the currency one. Why not allow an option for unit, if there is an option for currency?And in case that should be responsibility of Cldr.Unit, I could try to do a POC.
The text was updated successfully, but these errors were encountered: