fix(talk): honor configured speech locale

This commit is contained in:
Peter Steinberger
2026-04-25 21:05:15 +01:00
parent 8fb24ac3ce
commit 02f3e9cfa2
25 changed files with 297 additions and 11 deletions

View File

@@ -56,6 +56,46 @@ public enum TalkConfigParsing {
self.resolvedPositiveInt(talk?["silenceTimeoutMs"], fallback: fallback)
}
public static func normalizedSpeechLocaleID(_ value: String?) -> String? {
let trimmed = (value ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed.replacingOccurrences(of: "_", with: "-")
}
public static func resolvedSpeechLocaleID(
_ talk: [String: AnyCodable]?,
fallback: String? = nil
) -> String? {
self.normalizedSpeechLocaleID(talk?["speechLocale"]?.stringValue)
?? self.normalizedSpeechLocaleID(fallback)
}
public static func normalizedExplicitSpeechLocaleID(
_ value: String?,
automaticID: String = "auto"
) -> String? {
let normalized = self.normalizedSpeechLocaleID(value)
return normalized == automaticID ? nil : normalized
}
public static func resolvedSpeechRecognitionLocaleID(
preferredLocaleIDs: [String?],
fallbackLocaleID: String = "en-US",
supportedLocaleIDs: Set<String>
) -> String? {
let supported = Set(supportedLocaleIDs.compactMap(self.normalizedSpeechLocaleID))
var seen = Set<String>()
let candidates = (preferredLocaleIDs + [fallbackLocaleID])
.compactMap(self.normalizedSpeechLocaleID)
for candidate in candidates {
guard seen.insert(candidate).inserted else { continue }
if supported.isEmpty || supported.contains(candidate) {
return candidate
}
}
return nil
}
private static func normalizedTalkProviderID(_ raw: String?) -> String? {
let trimmed = (raw ?? "").trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
return trimmed.isEmpty ? nil : trimmed

View File

@@ -116,4 +116,21 @@ struct TalkConfigParsingTests {
#expect(TalkConfigParsing.resolvedPositiveInt(AnyCodable(true), fallback: 700) == 700)
#expect(TalkConfigParsing.resolvedPositiveInt(AnyCodable("1500"), fallback: 700) == 700)
}
@Test func resolvesSpeechLocaleID() {
#expect(TalkConfigParsing.resolvedSpeechLocaleID(["speechLocale": AnyCodable(" ru_RU ")]) == "ru-RU")
#expect(TalkConfigParsing.resolvedSpeechLocaleID(["speechLocale": AnyCodable("")], fallback: "en-US") == "en-US")
}
@Test func resolvesSpeechRecognitionLocaleFromSupportedFallbacks() {
let locale = TalkConfigParsing.resolvedSpeechRecognitionLocaleID(
preferredLocaleIDs: ["zz-ZZ", "fr-FR"],
supportedLocaleIDs: ["fr-FR", "en-US"])
let fallback = TalkConfigParsing.resolvedSpeechRecognitionLocaleID(
preferredLocaleIDs: ["zz-ZZ", "yy-YY"],
supportedLocaleIDs: ["en-US"])
#expect(locale == "fr-FR")
#expect(fallback == "en-US")
}
}