Today we'll follow up with another Bite in our localization series (sort of). Units of measurement are important to get right when localizing our apps for different cultures.
Today we'll look at a library from Khoa Pham called Scale that can help us convert between different units of measurement (for either localization or just-for-fun purposes). Let's begin.
Scale makes it incredible simple to work with different units of measurement:
let length = 5.kilometer + 7.meter // 5,007 meters
let weight = 10.0.kilogram * 5.gram // 50,000 grams
Everything is strongly-typed, and conversion is a breeze:
2.week.to(unit: .hour) // 336 hours
Scale even provides nicely formatted debug output. For example, print
'ing that last statement logs:
Time(value: 336.0, unit: Scale.TimeUnit.hour)
Scale supports a wide variety of units:
let angle = 5.degree + 2.radian
let area = 5.acre + 2.hectare
let metric = 5.base + 2.kilo
let volume = 5.liter + 2.gallon
// + many more...
One more example to show off how readable Scale's API is. Here we'll add two typed units together, then convert them:
let time = 4.day + 1.week
print(time.to(unit: .week))
Time(value: 1.57142857142857, unit: Scale.TimeUnit.week)
More info about Scale can be found at git.io/scale