Hi!
I have defined the following app intent. It returns a result with a dialog to confirm that the intent has been executed.
Naturally, that dialog needs to be localized properly. But the String interpolation with the provided format doesn't do that.
- I specified
widefor thewidthparameter and expect spelled-out unit names. However, in the textual output, Siri always uses the abbreviated unit (e.g. "min" or "s"), in all languages I tested. In the audio output, Siri says "minutes" in English where the textual representation is "min". In German, Siri says "min", so it basically reads the textual representation aloud and that's not quite understandable to the user.
struct StartTimerIntent: AppIntent {
static let title: LocalizedStringResource = "Start New Timer"
static var description = IntentDescription("Starts a timer with a custom duration.")
@Parameter(title: "Duration", description: "The duration of the timer.") var duration: Measurement<UnitDuration>
func perform() async throws -> some IntentResult & ProvidesDialog {
// [code to execute intent goes here]
return .result(
dialog: .init(
full: "\(duration, format: .measurement(width: .wide, usage: .asProvided)) timer started.",
systemImageName: "timer"
)
)
}
}
As this SwiftUI-style formatter doesn't seem to work with localization, I tried a different approach with a MeasurementFormatter:
extension Measurement where UnitType == UnitDuration {
func localized() -> String {
let formatter = MeasurementFormatter()
formatter.locale = .autoupdatingCurrent
formatter.unitOptions = .providedUnit
formatter.unitStyle = .long
return formatter.string(from: self)
}
}
Usage with String interpolation:
"\(duration.localized()) timer started."
This works great as long as these two languages are set to the same language on the user's device:
- [UI language] Settings → General → Language & Region → Preferred Language
- [Siri langauge] Settings → Apple Intelligence & Siri → Language
However, when they differ, even this method doesn't yield correct results.
- For example, I have my general (UI) language set to English, but my Siri language set to German. Then Siri replies in German, but the unit is formatted in English and Siri speaks it in English, so the result is a messed up sentence that's half German, half English.
What is the proper way to localize parameters in dialogs for Siri?
How can I make sure that parameters are localized to match Siri's language?