Fix crash if language code is less than 3 chars

Closes #391
This commit is contained in:
Shadowfacts 2023-05-27 15:22:53 -07:00
parent d0a1aec1c0
commit 822e3f91c4
1 changed files with 3 additions and 2 deletions

View File

@ -22,10 +22,11 @@ struct LanguagePicker: View {
}
static func codeFromInputMode(_ mode: UITextInputMode) -> Locale.LanguageCode? {
guard let bcp47Lang = mode.primaryLanguage else {
guard let bcp47Lang = mode.primaryLanguage,
!bcp47Lang.isEmpty else {
return nil
}
var maybeIso639Code = bcp47Lang[..<bcp47Lang.index(bcp47Lang.startIndex, offsetBy: 3)]
var maybeIso639Code = bcp47Lang[..<bcp47Lang.index(bcp47Lang.startIndex, offsetBy: min(3, bcp47Lang.count))]
if maybeIso639Code.last == "-" {
maybeIso639Code = maybeIso639Code[..<maybeIso639Code.index(before: maybeIso639Code.endIndex)]
}