Skip to content

Custom Dimension: how to support internationalisation in the symbols and UnitLength? #503

Open
@alpennec

Description

@alpennec

I've defined a custom Dimension to represent the pace.

public final class UnitPace: Dimension {
    public static let secondsPerMeter = UnitPace(
        symbol: "s/m",
        converter: UnitConverterLinear(coefficient: 1)
    )
}

extension Measurement where UnitType == UnitPace {
     public init(
        minutes: Int,
        seconds: Int,
        unit: UnitPace
    ) {
        let value = Double(minutes) + Double(seconds) / 60
        
        self.init(
            value: value,
            unit: unit
        )
    }
}

I want to format a Measurement<UnitPace> so it can be shown in the UI.

let paceMeasurement: Measurement<UnitPace> = Measurement<UnitPace>(
     minutes: 5, 
     seconds: 30, 
     unit: .secondsPerMeter
)
                                            
Text(paceMeasurement.formatted(.measurement(width: .narrow, numberFormatStyle: .number.precision(.fractionLength(0...1)))) ?? "-")

// "... s/m" is displayed

How can I specify the localised strings for the different UnitStyle (short, medium, long) / UnitWidth (wide, abbreviated, narrow) to use?

Metadata

Metadata

Assignees

Labels

API ChangeAny changes to Foundation's public API surface

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions