From 0a113bb0bf753e1f9a18a08c6176c562ccda8d0c Mon Sep 17 00:00:00 2001 From: Lakr Aream Date: Sat, 29 Jul 2023 10:46:07 +0800 Subject: [PATCH] Update Worker.swift --- Source/Worker.swift | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Source/Worker.swift b/Source/Worker.swift index 10c9a0f..de15684 100644 --- a/Source/Worker.swift +++ b/Source/Worker.swift @@ -71,10 +71,37 @@ class Worker: ObservableObject { } } + func lookupPossibleLocale(fromBuildDetails buildDetails: UUPDetails, withPreferredLanguage language: String? = nil) -> String { + // There is a naming conversation required for UUP API and macOS API + // see https://github.com/TuringSoftware/CrystalFetch/issues/2 for details + let localeMapper = [ + "zh-hans-cn": "zh-cn" + ] + + var decisionLocale = language ?? defaultLocale + if !buildDetails.langList.contains(decisionLocale), + let convertor = localeMapper[decisionLocale] + { + // unable to find this locale, check naming conversation if possible + decisionLocale = convertor + } + if !buildDetails.langList.contains(decisionLocale) { + // unable to find this locale, use the English if possible + decisionLocale = "en-us" + } + if !buildDetails.langList.contains(decisionLocale) { + // unable to find the en-us, use the first value supported if possible + decisionLocale = buildDetails.langList.first ?? "" + } + // if still not possible, throw the error UNSUPPORTED_LANG from server side + return decisionLocale + } + func refreshDetails(uuid: String, language: String? = nil, _ onCompletion: @escaping (BuildDetails, BuildEditions) -> Void) { withBusyIndication { [self] in let detailsResponse = try await api.fetchDetails(for: uuid) - let editionsResponse = try await api.fetchEditions(for: uuid, language: language ?? defaultLocale) + let language = lookupPossibleLocale(fromBuildDetails: detailsResponse, withPreferredLanguage: language) + let editionsResponse = try await api.fetchEditions(for: uuid, language: language) onCompletion(BuildDetails(from: detailsResponse), BuildEditions(from: editionsResponse)) } }