Fix locale issues

This commit is contained in:
Justin Mazzocchi 2021-01-17 18:42:03 -08:00
parent 8706fdab78
commit 493ea98f13
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
5 changed files with 67 additions and 21 deletions

View File

@ -21,11 +21,13 @@ public extension AccessTokenEndpoint {
public var username = ""
public var email = ""
public var password = ""
public var locale = "en"
public var locale: String
public var reason = ""
public var agreement = false
public init() {}
public init(locale: String) {
self.locale = locale
}
}
}

View File

@ -6,7 +6,6 @@ import Foundation
import Mastodon
public enum EmojiPickerError: Error {
case invalidLocaleLanguageCode
case emojisFileMissing
case invalidSystemEmojiGroup
case annotationsAndTagsFileMissing
@ -87,23 +86,9 @@ public extension EmojiPickerService {
.eraseToAnyPublisher()
}
func systemEmojiAnnotationsAndTagsPublisher(locale: Locale) -> AnyPublisher<[String: String], Error> {
func systemEmojiAnnotationsAndTagsPublisher(languageCode: String) -> AnyPublisher<[String: String], Error> {
Future { promise in
guard let languageCode = locale.languageCode else {
promise(.failure(EmojiPickerError.invalidLocaleLanguageCode))
return
}
let language: String
if languageCode == "zh" && locale.scriptCode == "Hant" {
language = "zh_Hant"
} else {
language = languageCode
}
guard let url = Bundle.module.url(forResource: language, withExtension: "json") else {
guard let url = Bundle.module.url(forResource: languageCode, withExtension: "json") else {
promise(.failure(EmojiPickerError.annotationsAndTagsFileMissing))
return
@ -118,6 +103,7 @@ public extension EmojiPickerService {
promise(.failure(error))
}
}
.print()
.eraseToAnyPublisher()
}

View File

@ -19,6 +19,7 @@ final public class EmojiPickerViewModel: ObservableObject {
@Published private var systemEmojiAnnotationsAndTags = [String: String]()
private var cancellables = Set<AnyCancellable>()
// swiftlint:disable:next function_body_length
public init(identification: Identification) {
self.identification = identification
emojiPickerService = identification.service.emojiPickerService()
@ -38,7 +39,9 @@ final public class EmojiPickerViewModel: ObservableObject {
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$emojiUses)
$locale.removeDuplicates().flatMap(emojiPickerService.systemEmojiAnnotationsAndTagsPublisher(locale:))
$locale.map { $0.languageCodeWithScriptIfNecessary ?? Locale.fallbackLanguageCode }
.removeDuplicates()
.flatMap(emojiPickerService.systemEmojiAnnotationsAndTagsPublisher(languageCode:))
.replaceError(with: [:])
.assign(to: &$systemEmojiAnnotationsAndTags)

View File

@ -0,0 +1,53 @@
// Copyright © 2021 Metabolist. All rights reserved.
import Foundation
extension Locale {
static let fallbackLanguageCode = "en"
static var preferred: Locale? {
guard let identifier = preferredLanguages.first else { return nil }
return Self(identifier: identifier)
}
var languageCodeWithScriptIfNecessary: String? {
guard let languageCode = languageCode else { return nil }
if scriptCode == "Hant" {
return "zh_Hant"
} else {
return languageCode
}
}
var languageCodeWithCoercedRegionCodeIfNecessary: String? {
guard let languageCode = languageCode else { return nil }
switch languageCode {
case "es":
if regionCode == "AR" {
return "es-AR"
} else {
return "es"
}
case "pt":
if regionCode == "PT" {
return "pt-PT"
} else {
return "pt-BR"
}
case "zh":
if let regionCode = regionCode,
regionCode == "CN" || regionCode == "HK" || regionCode == "TW" {
return "zh-".appending(regionCode)
} else if scriptCode == "Hant" {
return "zh-TW"
} else {
return "zh-CN"
}
default:
return languageCode
}
}
}

View File

@ -14,7 +14,9 @@ public final class RegistrationViewModel: ObservableObject {
public let serverRulesURL: URL
public let termsOfServiceURL: URL
@Published public var alertItem: AlertItem?
@Published public var registration = Registration()
@Published public var registration = Registration(
locale: (Locale.preferred ?? Locale.current).languageCodeWithCoercedRegionCodeIfNecessary
?? Locale.fallbackLanguageCode)
@Published public var passwordConfirmation = ""
@Published public private(set) var registerDisabled = true
@Published public private(set) var registering = false