I need to implement dynamic localization in my SwiftUI app. As an alternative of utilizing the built-in Localizable.strings information, I’ve saved all my localized strings in an Excel sheet (uploaded to Google Sheets).
There are a lot of extra strings however I simply giving eg
https://docs.google.com/spreadsheets/d/1bpl7AGvrRotwsyMjFRm-h10pB5WZ31PJ5Y0naCr4atk/edit?usp=sharing
How can I fetch localization information from an Excel sheet as an alternative of utilizing the built-in Localizable.strings in iOS?
Please information me and supply any documentation or code snippets for this.
I’ve used the next code for localization with Localizable.strings: However I would like this to occur from Excel sheet information.. how?
Textual content("schoolCode_title".localized())
Textual content("schoolCode_findyourSchool".localized())
extension String {
func localized() -> String {
LanguageManager.shared.localizedString(forKey: self)
}
}
class LanguageManager: ObservableObject {
static let shared = LanguageManager()
static func initializeManager() {
_ = LanguageManager.shared
}
@Printed var currentLanguage: String {
didSet {
UserDefaults.customary.set(currentLanguage, forKey: "AppLanguage")
updateBundle()
}
}
var bundle: Bundle = .important
non-public init() {
currentLanguage = UserDefaults.customary.string(forKey: "AppLanguage") ?? "en"
updateBundle()
}
func setLanguage(_ language: String) {
guard currentLanguage != language else { return }
currentLanguage = language
}
func toggleLanguage() {
currentLanguage = (currentLanguage == "en") ? "hello" : "en"
}
non-public func updateBundle() {
if let path = Bundle.important.path(forResource: currentLanguage, ofType: "lproj"),
let langBundle = Bundle(path: path) {
self.bundle = langBundle
} else {
self.bundle = .important
}
}
func localizedString(forKey key: String) -> String {
bundle.localizedString(forKey: key, worth: nil, desk: nil)
}
}