Localization fixes

This commit is contained in:
Balazs Perlaki-Horvath 2023-12-21 23:21:44 +01:00
parent d486d9336a
commit 409be22b29
3 changed files with 31 additions and 27 deletions

View File

@ -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")
}
}
}

View File

@ -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
)
}
}

View File

@ -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
}