mirror of
https://github.com/mastodon/mastodon-ios.git
synced 2024-12-15 10:24:32 +01:00
0a9689c67f
* Basic fake language picker support * Recognize languages from post text * Exclude suggested languages from recents * Load recent languages from Settings object * Send the language to the API * Persist the used language to settings * Always show the currently selected language in the list * Fix crash * Add support for picking arbitrary lanuages * Fix display of 3 letter language codes * Improve label to include endonym too * Limit to 3 recent languages * Reduce lower bound for displaying language suggestions * Fix saving recent language when publishing * Fix tint color of language picker button * Add a badge to prompt users to change language * Dismiss the badge even if you pick the same language * Read language names in the language if possible * Use a compressed font for 3-letter codes Also use `minimumScaleFactor` to shrink troublesome codes to fit Co-Authored-By: samhenrigold <49251320+samhenrigold@users.noreply.github.com> * Remove .vscode/launch.json * Add message to fatalError() Co-authored-by: samhenrigold <49251320+samhenrigold@users.noreply.github.com>
42 lines
1.2 KiB
Swift
42 lines
1.2 KiB
Swift
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
import Foundation
|
|
|
|
// Consider replacing this with Locale.Language when dropping iOS 15
|
|
struct Language: Identifiable {
|
|
let endonym: String
|
|
let exonym: String
|
|
let id: String
|
|
let localeId: String?
|
|
|
|
init(endonym: String, exonym: String, id: String, localeId: String?) {
|
|
self.endonym = endonym
|
|
self.exonym = exonym
|
|
self.id = id
|
|
self.localeId = localeId
|
|
}
|
|
|
|
init?(id: String) {
|
|
guard let endonym = Locale(identifier: id).localizedString(forLanguageCode: id),
|
|
let exonym = Locale.current.localizedString(forLanguageCode: id)
|
|
else { return nil }
|
|
self.endonym = endonym
|
|
self.exonym = exonym
|
|
self.id = id
|
|
self.localeId = nil
|
|
}
|
|
|
|
func contains(_ query: String) -> Bool {
|
|
"\(endonym) \(exonym) \(id)".localizedCaseInsensitiveContains(query)
|
|
}
|
|
|
|
var exonymIsDifferent: Bool {
|
|
endonym.caseInsensitiveCompare(exonym) != .orderedSame
|
|
}
|
|
|
|
var label: AttributedString {
|
|
AttributedString(endonym, attributes: AttributeContainer([.languageIdentifier: id]))
|
|
+ AttributedString(exonymIsDifferent ? " (\(exonym))" : "")
|
|
}
|
|
}
|