From 409be22b29a321376b978e4e7dc72d7fdbbfa333 Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Thu, 21 Dec 2023 23:21:44 +0100 Subject: [PATCH] Localization fixes --- Model/Entities/Errors.swift | 6 ++-- Model/Utilities/String+Extension.swift | 50 ++++++++++++++------------ Views/BuildingBlocks/Favicon.swift | 2 +- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/Model/Entities/Errors.swift b/Model/Entities/Errors.swift index 96c517e0..b600a43f 100644 --- a/Model/Entities/Errors.swift +++ b/Model/Entities/Errors.swift @@ -16,12 +16,12 @@ public enum LibraryRefreshError: LocalizedError { public var errorDescription: String? { switch self { case .retrieve(let description): - let prefix = "library_refresh_error.retrieve.description".localized(withComment: "Library Refresh Error") + let prefix = "library_refresh_error.retrieve.description".localizedWith(comment: "Library Refresh Error") return [prefix, description].compactMap({ $0 }).joined(separator: " ") case .parse: - return "library_refresh_error.parse.description".localized(withComment: "Library Refresh Error") + return "library_refresh_error.parse.description".localizedWith(comment: "Library Refresh Error") case .process: - return "library_refresh_error.process.description".localized(withComment: "Library Refresh Error") + return "library_refresh_error.process.description".localizedWith(comment: "Library Refresh Error") } } } diff --git a/Model/Utilities/String+Extension.swift b/Model/Utilities/String+Extension.swift index 7c07e21c..65e6ad7a 100644 --- a/Model/Utilities/String+Extension.swift +++ b/Model/Utilities/String+Extension.swift @@ -11,38 +11,42 @@ import Foundation extension String { var localized: String { - NSLocalizedString( - self, - tableName: nil, - bundle: DefaultLanguages.currentBundle, - value: "", - comment: "" - ) + localizedWithFallback() } - func localized(withComment: String) -> String { - return NSLocalizedString( - self, - tableName: nil, - bundle: DefaultLanguages.currentBundle, - value: "", - comment: withComment - ) + func localizedWith(comment: String) -> String { + localizedWithFallback(comment: comment) } func localizedWithFormat(withArgs: CVarArg...) -> String { - let format = NSLocalizedString( - self, - tableName: nil, - bundle: DefaultLanguages.currentBundle, - value: "", - comment: "" - ) - + let format = localizedWithFallback() switch withArgs.count { case 1: return String.localizedStringWithFormat(format, withArgs[0]) case 2: return String.localizedStringWithFormat(format, withArgs[0], withArgs[1]) default: return String.localizedStringWithFormat(format, withArgs) } } + + private func localizedWithFallback( + bundle: Bundle = DefaultLanguages.currentBundle, + comment: String = "" + ) -> String { + let englishValue: String + if let path = Bundle.main.path(forResource: "en", ofType: "lproj"), + let bundle = Bundle(path: path) { + englishValue = NSLocalizedString(self, bundle: bundle, comment: comment) + if NSLocale.preferredLanguages.first == "en" { + return englishValue + } + } else { + englishValue = "" + } + return NSLocalizedString( + self, + tableName: nil, + bundle: bundle, + value: englishValue, // fall back to this, if translation not found + comment: comment + ) + } } diff --git a/Views/BuildingBlocks/Favicon.swift b/Views/BuildingBlocks/Favicon.swift index 268d432a..36a3538f 100644 --- a/Views/BuildingBlocks/Favicon.swift +++ b/Views/BuildingBlocks/Favicon.swift @@ -46,7 +46,7 @@ struct Favicon: View { if let data = imageData, let image = UIImage(data: data) { Image(uiImage: image).resizable() } else { - Image(category.name).resizable() + Image(category.icon).resizable() } #endif }