1

I am trying to change my app language setting "AppleLanguages". Everything changed except one of them. UITextField menu items don't change. It always holds the first-time app launch language.

I have already followed this link example: Setting "AppleLanguages" doesn't change app language

Here I used this code:

UserDefaults.standard.set(appleLanguages, forKey: "AppleLanguages")
UserDefaults.standard.synchronize()

if let languageDirectoryPath = Bundle.main.path(forResource: languageCode, ofType: "lproj"){
    bundle = Bundle.init(path: languageDirectoryPath)
}

Although I have changed UIsearchbar cancel button text in English, other items can not.

if let searchBtn = searchBar.value(forKey: "cancelButton") as? UIButton{
    searchBtn.setTitle(Messages.shared.CANCEL, for: .normal)
}

enter image description here

But when I implemented this code example, menu text language always English

Step 1. Create an extension of Bundle

import Foundation

class L012Localizer: NSObject {
    class func DoTheSwizzling() {
        MethodSwizzleGivenClassName(cls: Bundle.self, originalSelector: #selector(Bundle.localizedString(forKey:value:table:)), overrideSelector:
            #selector(Bundle.specialLocalizedString(key:value:table:)))
    }
}

extension Bundle {
    @objc func specialLocalizedString(key: String, value: String?, table tableName: String?) -> String {
        let currentLanguage = Utils.currentLanguage().rawValue
        var bundle = Bundle();
        if currentLanguage != "" , let _path = Bundle.main.path(forResource: currentLanguage, ofType: "lproj") {
            bundle = Bundle(path: _path)!
        } else {
            let _path = Bundle.main.path(forResource: "Base", ofType: "lproj")!
            bundle = Bundle(path: _path)!
        }
        return (bundle.specialLocalizedString(key: key, value: value, table: tableName))
    }
}

func MethodSwizzleGivenClassName(cls: AnyClass, originalSelector: Selector, overrideSelector: Selector){

    let origMethod: Method = class_getInstanceMethod(cls, originalSelector)!;
    let overrideMethod: Method = class_getInstanceMethod(cls, overrideSelector)!;
    if (class_addMethod(cls, originalSelector, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
        class_replaceMethod(cls, overrideSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    } else {
        method_exchangeImplementations(origMethod, overrideMethod);
    }
}
  1. Call DoTheSwizzling in AppDelegate

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        L012Localizer.DoTheSwizzling()
        return true
    }
    
  2. Create Language Utils

   class Utils: NSObject {
        class func setLanguage(_ lang: LanguageType) {
            UserDefaults.standard.set([lang.rawValue], forKey: "AppleLanguages")
        }
        class func currentLanguage() -> LanguageType {
            if let langs = UserDefaults.standard.object(forKey: "AppleLanguages") as? [String], let currentLang = langs.first {
                return LanguageType(rawValue: currentLang) ?? .english
            }

            return .english
        }
    }
  1. Create Language Type

    enum LanguageType: String {
        case english = "en"
        case korea = "ko"
        case vietnamese = "vi-VN"

        func toString() -> String {
            switch self {
            case .korea:
                return "Korea".localized
            case .vietnamese:
                return "Vietnamese".localized
            default:
                return "English".localized
            }
        }
       }
  1. Remember that you have to config Application Language in Scheme to SystemLanguage

  2. Then Every time you need to localize your app, you only need to call.

    Utils.setLanguage({LanguageType})

enter image description here

Here is my info.plist

enter image description here

AMIT
  • 906
  • 1
  • 8
  • 20
  • Can you show the code for adding the MenuItems in MenuControllers. My guess is they are added only once and hence they pick the default language., make sure you update the menu items once you know the language is changed – AjinkyaSharma Feb 02 '20 at 05:39
  • I don't add any MenuItems manually. It is shown by default when I select a text or want to cut, copy and paste any text. – AMIT Feb 02 '20 at 05:43
  • The way you are setting the language in user defaults, doesn't change the device's language, and the menu controller is using device's language. – AjinkyaSharma Feb 02 '20 at 06:00
  • I don't know which changed the effect on the app menu item language. If it is responsible for system language change please guide me right and proper direction. – AMIT Feb 02 '20 at 06:04
  • You have to change your device language – Chris Feb 02 '20 at 06:31
  • I have already spent more time but didn't found any solution. Please help me to find any example to change the device language programmatically. – AMIT Feb 02 '20 at 06:36
  • Hi Amit, you can't change the device language programmatically. From what I see, the way you are doing is having a language selector in the app itself, and don't depend on the device language. Hence you see a localized language selector. You will have to check how to handle the menu items in a search field, or maybe have a custom search field and handle this. – AjinkyaSharma Feb 02 '20 at 10:44
  • After a long time searching and trying I can't any solution. Please, anyone, give me a reference. – AMIT Feb 02 '20 at 13:46

0 Answers0