Make a new class NSBundle+Language.h, NSBundle+Language.m. And don't forget to add bridging header.
NSBundle+Language.h
#import <Foundation/Foundation.h>
@interface NSBundle (Language)
+(void)setLanguage:(NSString*)language;
@end
NSBundle+Language.m
#import "NSBundle+Language.h"
#import <objc/runtime.h>
static const char _bundle=0;
@interface BundleEx: NSBundle
@end
@implementation BundleEx
-(NSString*)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
NSBundle* bundle=objc_getAssociatedObject(self, &_bundle);
return bundle ? [bundle localizedStringForKey:key value:value table:tableName]: [super localizedStringForKey:key value:value table:tableName];
}
@end
@implementation NSBundle (Language)
+(void)setLanguage:(NSString*)language
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^
{
object_setClass([NSBundle mainBundle],[BundleEx class]);
});
objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]]: nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
After this use this language manager to change the language:-
class LanguageManager: NSObject {
//MARK: Set language preference
class func setSelectedLanguage(dictionary: [String: String]) {
let userDefaults = UserDefaults.standard
userDefaults.set(dictionary, forKey: kSelectedLanguageDetails)
userDefaults.synchronize()
}
class func getSelectedLanguage() -> [String: String]? {
let userDefaults = UserDefaults.standard
return userDefaults.value(forKey: kSelectedLanguageDetails) as? [String: String]
}
class func getSelectedLangaugeCode() -> String{
if let selectedLanguageDetails = LanguageManager.getSelectedLanguage() {
if let languageCode = selectedLanguageDetails[LocalizationKeys.kLanguageCode.rawValue] {
Bundle.setLanguage(LanguageManager.getAppleLanguageCode(languageCode: languageCode.lowercased()))
return languageCode.lowercased()
}
else {
let languageDetails = [LocalizationKeys.kLanguageCode.rawValue:"EN", LocalizationKeys.kLanguageName.rawValue:"English", LocalizationKeys.kLanguageID.rawValue:"1"]
LanguageManager.setSelectedLanguage(dictionary: languageDetails)
if let languageCode = languageDetails[LocalizationKeys.kLanguageCode.rawValue] {
Bundle.setLanguage(LanguageManager.getAppleLanguageCode(languageCode: languageCode.lowercased()))
return languageCode.lowercased()
}
}
}
else {
let languageDetails = [LocalizationKeys.kLanguageCode.rawValue:"EN", LocalizationKeys.kLanguageName.rawValue:"English", LocalizationKeys.kLanguageID.rawValue:"1"]
LanguageManager.setSelectedLanguage(dictionary: languageDetails )
if let languageCode = languageDetails[LocalizationKeys.kLanguageCode.rawValue] {
Bundle.setLanguage(LanguageManager.getAppleLanguageCode(languageCode: languageCode.lowercased()))
return languageCode.lowercased()
}
}
return "en"
}
/// Checks for the Language preferences selected by user
class func checkLanguagePreferenceAndSetToDefaults() {
if let selectedLanguageDetails = LanguageManager.getSelectedLanguage() {
if let languageCode = selectedLanguageDetails[LocalizationKeys.kLanguageCode.rawValue] {
Bundle.setLanguage(LanguageManager.getAppleLanguageCode(languageCode: languageCode.lowercased()))
}
else {
let languageDetails = [LocalizationKeys.kLanguageCode.rawValue:"EN", LocalizationKeys.kLanguageName.rawValue:"English", LocalizationKeys.kLanguageID.rawValue:"1"]
LanguageManager.setSelectedLanguage(dictionary: languageDetails)
if let languageCode = languageDetails[LocalizationKeys.kLanguageCode.rawValue] {
Bundle.setLanguage(LanguageManager.getAppleLanguageCode(languageCode: languageCode.lowercased()))
}
}
}
else {
let languageDetails = [LocalizationKeys.kLanguageCode.rawValue:"EN", LocalizationKeys.kLanguageName.rawValue:"English", LocalizationKeys.kLanguageID.rawValue:"1"]
LanguageManager.setSelectedLanguage(dictionary: languageDetails )
if let languageCode = languageDetails[LocalizationKeys.kLanguageCode.rawValue] {
Bundle.setLanguage(LanguageManager.getAppleLanguageCode(languageCode: languageCode.lowercased()))
}
}
}
//MARK: Get Language Code
/// Return apple specific language code
///
/// - parameter languageCode: language code string
///
/// - returns: apples language code
static func getAppleLanguageCode(languageCode:String) -> String{
switch languageCode.lowercased() {
case "en":
return "en"
case "es":
return "es"
case "fr":
return "fr"
case "de":
return "de"
case "it":
return "it"
default:
return languageCode
}
}
}
Define the language options as bellow:-
let KlanguageOptionArray: [[String:String]] = [
[
"LanguageCode": "EN",
"Name": "English",
"LanguageID": "1"
],
[
"LanguageCode": "ES",
"Name": "Español",
"LanguageID": "2"
],
[
"LanguageCode": "FR",
"Name": "Français",
"LanguageID": "3"
],
[
"LanguageCode": "DE",
"Name": "Deutsch",
"LanguageID": "4"
],
[
"LanguageCode": "IT",
"Name": "Italiano",
"LanguageID": "5"
]
]
Change the selected language with bellow code
let selectedLanguageDetails = self.languageOptionArray[indexPath.row]
LanguageManager.setSelectedLanguage(dictionary: selectedLanguageDetails)
if let languageCode = selectedLanguageDetails[LocalizationKeys.kLanguageCode.rawValue] {
Bundle.setLanguage(LanguageManager.getAppleLanguageCode(languageCode: languageCode.lowercased()))
NotificationCenter.default.post(name: NSNotification.Name(rawValue: NotificationCenterTypes.ReloadTableOnLanguageChangeNotification), object: nil)
}
And don't forget to add this on every launch
if let selectedLanguageDetails = LanguageManager.getSelectedLanguage() {
if let languageCode = selectedLanguageDetails[LocalizationKeys.kLanguageCode.rawValue] {
Bundle.setLanguage(LanguageManager.getAppleLanguageCode(languageCode: languageCode.lowercased()))
}
else {
}
}
Hope this helps you