0

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

Surafel
  • 675
  • 4
  • 11
  • 24
  • Possible duplicate of [Changing language on the fly in swift](https://stackoverflow.com/questions/31840176/changing-language-on-the-fly-in-swift) – Yoel Jimenez del valle Sep 17 '19 at 13:44
  • check this https://stackoverflow.com/questions/31840176/changing-language-on-the-fly-in-swift – Yoel Jimenez del valle Sep 17 '19 at 13:44
  • @kjoe please see my edited question – Surafel Sep 17 '19 at 14:07
  • @kjoe please see my edited question – Surafel Sep 17 '19 at 14:07
  • i used a few weeks ago the same link, also all my UI were in storyBoard. you have to use base and localize in all other languaje you are supposed to use in your app. i have a button to choose between spanish and english and with the code in the app i was able to resolve it – Yoel Jimenez del valle Sep 17 '19 at 14:13
  • @kjoe do i have to extend BaseViewController on all my view controllers or only on the ViewController where i have the Change Language button? – Surafel Sep 17 '19 at 15:01
  • is not necesary unless you do the language change deep inside app herarchy, since my app have a two buttons at begining of the app flow for me was not necesary – Yoel Jimenez del valle Sep 17 '19 at 17:06
  • @kjoe ok i have managed to make it work without using the BaseViewController and now it works fine but i still have to restart the app for the language to change and my languageDidChange method is empty what can do in that method to change it on the fly? – Surafel Sep 18 '19 at 06:54
  • can you explain the flow of your app mine is empty since in that view i dont have a text to change but you could reload all interface data, or call a method that reload it – Yoel Jimenez del valle Sep 18 '19 at 11:37
  • @kjoe i am trying to change the language on the login screen before the user logs in i also have another change language button on profile view controller which is presented after the user logs in. once the user changes the language the contents of the login screen also have to be changed on the fly – Surafel Sep 18 '19 at 11:51
  • my guess where you need to change language on the fly where is already loaded the view and need to change it then inherit from baseViewController – Yoel Jimenez del valle Sep 18 '19 at 12:01
  • ok but after i inherit from BaseViewController it gives me a function to overload right? languageChanged function what can i do in that function to reload the view and display the selected language? – Surafel Sep 18 '19 at 12:03

0 Answers0