Skip to content
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

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

Open
alpennec opened this issue Mar 22, 2024 · 1 comment
Assignees
Labels
API Change Any changes to Foundation's public API surface

Comments

@alpennec
Copy link

alpennec commented Mar 22, 2024

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?

@itingliu
Copy link
Contributor

itingliu commented Apr 4, 2024

It's currently impossible to extend localization support to custom Dimension, unfortunately. Currently we rely exclusively on ICU to fetch the localization. I imagine we'd need to add a hook in Measurement.FormatStyle to take localization via, say, our string localization infrastructure. Such "hook" will probably be public to allow extension in your own type, which means new API as well.

@itingliu itingliu self-assigned this Apr 4, 2024
@itingliu itingliu added the API Change Any changes to Foundation's public API surface label Apr 4, 2024
@alpennec alpennec changed the title Custom Dimesion: how to support internationalisation in the symbols and UnitLength? Custom Dimension: how to support internationalisation in the symbols and UnitLength? Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Change Any changes to Foundation's public API surface
Projects
None yet
Development

No branches or pull requests

2 participants