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)
}
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);
}
}
Call DoTheSwizzling in AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { L012Localizer.DoTheSwizzling() return true }
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
}
}
- 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
}
}
}
Remember that you have to config Application Language in Scheme to SystemLanguage
Then Every time you need to localize your app, you only need to call.
Utils.setLanguage({LanguageType})
Here is my info.plist