Added default locale fallback when loading supported locales on the ContentViewModel view model in the Sample app target.
This commit is contained in:
@@ -48,11 +48,18 @@ extension ContentView {
|
||||
// MARK: Methods
|
||||
|
||||
/// Loads the locales the transcriber supports into ``locales``, sorted by their localized names, and aligns ``locale`` with the
|
||||
/// supported equivalent of its current value so the locale picker starts with a valid selection.
|
||||
/// supported equivalent of its current value — falling back to the supported equivalent of a default locale when none exists —
|
||||
/// so the locale picker starts with a valid selection.
|
||||
func load() async {
|
||||
locales = await SpeechTranscriber.supportedLocales.sorted {
|
||||
name(for: $0).localizedStandardCompare(name(for: $1)) == .orderedAscending
|
||||
}
|
||||
|
||||
if let equivalent = await SpeechTranscriber.supportedLocale(equivalentTo: locale) {
|
||||
locale = equivalent
|
||||
} else if let fallback = await SpeechTranscriber.supportedLocale(equivalentTo: .byDefault) {
|
||||
locale = fallback
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the localized name of a locale for the locale picker.
|
||||
@@ -62,7 +69,8 @@ extension ContentView {
|
||||
func name(
|
||||
for locale: Locale
|
||||
) -> String {
|
||||
Locale.current
|
||||
Locale
|
||||
.current
|
||||
.localizedString(
|
||||
forIdentifier: locale.identifier
|
||||
)?.localizedCapitalized
|
||||
@@ -86,3 +94,10 @@ extension ContentView {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Constants
|
||||
|
||||
private extension Locale {
|
||||
/// The locale to fall back to when the transcriber supports no equivalent of the user's current locale.
|
||||
static let byDefault: Locale = .init(identifier: "en_US")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user