tearsinthegame/localeunitswift
A helper library that manages Unit, Measurement and Dimension related functions in Swift, respecting the locale settings of the user or the locale selected by the developer.
Setup
You can add LocaleUnitSwift to your project with Swift Package Manager. In Xcode, select File > Add Package Dependency. Then, enter this Github repository URL: https://github.com/tearsinthegame/LocaleUnitSwift
More About Features
It is possible to convert Measurement to the user's default locale value with correct units, with the option to select the Locale and MeasurementFormatUnitUsage:
let localeUnit = LocaleUnitSwift()
let measure = Measurement(value: 5, unit: UnitLength.meters)
let converted = localeUnit.convertedToLocaleValue(measure, locale: Locale(identifier: "en-US")) // returns converted Measurement, "16.404199475065617 ft"
let symbol = localeUnit.symbol(for: UnitLength.self, locale: Locale(identifier: "en-US"), usage: MeasurementFormatUnitUsage<UnitLength>.road) // returns miYou can use your custom Dimension classes by extending the LocaleUnitSwift's LocaleUnitSwiftCustom protocol for your own implementation. An example dimension, UnitCustom can be applied as follows:
extension UnitCustom: LocaleUnitSwiftCustom {
static func unit(for locale: Locale) -> Unit {
if locale.measurementSystem == .metric {
return Self.unitA
} else {
return Self.unitB
}
}
}
let symbol = LocaleUnitSwift().symbol(for: UnitCustom.self) // returns unitA symbol for locale metric system[!IMPORTANT] You need to add protocol stubs to conform to the package functions
LICENSE
This library is licensed with Apache 2.0.
Package Metadata
Repository: tearsinthegame/localeunitswift
Default branch: main
README: README.md