Hey i was trying to change the default app language in swift and used this code its an extension to Bundle
extension Bundle {
static func swizzleLocalization() {
print("print one")
let orginalSelector = #selector(localizedString(forKey:value:table:))
guard let orginalMethod = class_getInstanceMethod(self, orginalSelector) else { return }
let mySelector = #selector(myLocaLizedString(forKey:value:table:))
guard let myMethod = class_getInstanceMethod(self, mySelector) else { return }
if class_addMethod(self, orginalSelector, method_getImplementation(myMethod), method_getTypeEncoding(myMethod)) {
class_replaceMethod(self, mySelector, method_getImplementation(orginalMethod), method_getTypeEncoding(orginalMethod))
} else {
method_exchangeImplementations(orginalMethod, myMethod)
}
}
@objc private func myLocaLizedString(forKey key: String,value: String?, table: String?) -> String {
print("print two")
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let bundlePath = Bundle.main.path(forResource: UserDefaults.standard.string(forKey: "AppLanguage"), ofType: "lproj"),
let bundle = Bundle(path: bundlePath) else {
return Bundle.main.myLocaLizedString(forKey: key, value: value, table: table)
}
return bundle.myLocaLizedString(forKey: key, value: value, table: table)
}
}
now i stored this code at the bottom of my AppDelegate
Class and called it using this code in the didFinishLaunchingWithOptions
UserDefaults.standard.set("ar", forKey: "AppLanguage")
Bundle.swizzleLocalization()
this works just fine but when i try to change it while running inside click event of a 'changeLanguage' Button it doesnt work, how can i fix this to change it on runtime?
i am using storyboard items i translated the Main.Storyboard
and it gave me Main.strings
i looked at this question but i guess it doesnt work for because all my elements are on a storyboard and the accepted answers author on that question specifically said it doesnt work on storyboards