This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppEnvironment: ObservableObject { | |
@Published private var localizationManagerWillChange: Void = () | |
let localizationManager: LocalizationManager = .init() | |
init() { | |
localizationManager.objectWillChange.assign(to: &$localizationManagerWillChange) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LocalizationManager: ObservableObject { | |
@Published var language: Language = .english | |
func localize(_ key: String) -> String { | |
guard | |
let bundlePath = Bundle.main.path(forResource: language.rawValue, | |
ofType: "lproj"), | |
let bundle = Bundle(path: bundlePath) | |
else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum MainError: Error { | |
case general | |
case unknownError | |
var userFriendlyDescription: String { | |
"MainError.\(self)".localizeSafely(safe: "MainError.general".localized) | |
} | |
} | |
extension String { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NetworkError: String, Error { | |
case userNotFound = "USER_NOT_FOUND" | |
var userFriendlyDescription: String { | |
"NetworkError.\(self)".localized | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NetworkError.userNotFound = "The user is not exist"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
var localized: String { | |
NSLocalizedString(self, comment: "") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum MainError: Error { | |
case general | |
case networkError(NetworkError) | |
var userFriendlyDescription: String { | |
switch self { | |
case .networkError(let error): | |
return error.userFriendlyDescription | |
default: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NetworkError: Error { | |
case userNotFound | |
var userFriendlyDescription: String { | |
"NetworkError.\(self)".localized | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MainError.general = "An error occurred. Please try again later."; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum MainError: Error { | |
case general | |
var userFriendlyDescription: String { | |
"MainError.\(self)".localized | |
} | |
} |