Group Group Group Group Group Group Group Group Group

How to change localization in the app?

Hello it is really necessary to change localization in the app… Right now I have created custom lozalized extension, but the thing is that I can’t change all user controllers and also languages inside of added frameworks… How can I completely change locale to user selected without restarting APP? ViewController could be started, no problem about that. I have searched many solutions, still I only learned about creating custom lozalized, but it does not change locales in the added frameworks, because I can’t control their locales…

Hi @wellbranding,
if you want to hot load the localization resources, then rather than let the system load the resource from the locale, write a function that loads the resources for a particular locale as created in code.

cheers,

Jayant

@jayantvarma I changed localization with UserDefaults. It works well…however problem is that Close title in the navbar is not localized…How can I localize it manually? I don’t control the presented Viewcontroller because it is opened from the added framework (country selection)

iOS App Localization

iOS app localization, the term is almost self explanatory to most of the mobile developers. If you are not aware of app localization then with app localization, it means that an app ability to support multiple languages. Since the majority of people in this world speaks and understand their country native language and English is not their primary language. So in order to reach more audience we need to localize the app to different languages and this can be achieved using app localization. In this
tutorial, we will earn how to localize iOS app.

Two ways to change app language
There are two ways with which we can allow app user’s to change app language

  1. App language will be same as the device language(This is the default system on which iOS works). The main limitation of this approach is that if someone is not interested to change device language but he wants to change a particular app language.

  2. Change app language from inside of the app. With this technique, we can allow users of our app to switch to different languages without changing or interfering with device language.

Steps to change app language and user interface design of the app from inside of the app, without changing device language
Step 1: Open Xcode, and create a single view application project, name it “Localization-Tutorial”.

Step 2: Adding another language, English language is default development language. Follow steps as shown in images below to add other language support for the iOS app

At this point we have localized our app for Arabic and English languages.

Step 3: We will design a user interface for the app, our user interface for the app will look like as shown in below image.

Step 4: Open ViewController.swift and create IBOutlet for UILabel’s, UIButton we added to main.storyboard and an IBAction that will trigger language change.

class ViewController: UIViewController {

@IBOutlet weak var lblHeader: UILabel!
@IBOutlet weak var btnChangeLangauge: UIButton!
@IBOutlet weak var lblCurrentLanguage: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func changeLanguage(_ sender: Any) {

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Changing app language from inside of the app
In order to change app language only without changing the device language, or language will changed from inside the app via providing an user language options. We have to override the default language preferences for our app and then load the app bundle as per the language in app prefrences and thus loading the correct localized values.

For changing the app language from inside of the app and reflecting the language change we will use
“LocalizationSystem.swift” file. In this file we have different methods that set language preference for our app and load the bundle as per our language preference. Also we will get new methods for fetching the localized text from our localizable.strings file.

Step 6: Open ViewController.swift file, and add the below code

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var lblHeader: UILabel!
@IBOutlet weak var btnChangeLangauge: UIButton!
@IBOutlet weak var lblCurrentLanguage: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    lblHeader.text = LocalizationSystem.sharedInstance.localizedStringForKey(key: "header_text", comment: "")
    lblCurrentLanguage.text = LocalizationSystem.sharedInstance.getLanguage()
    btnChangeLangauge.setTitle(LocalizationSystem.sharedInstance.localizedStringForKey(key: "change_language", comment: ""), for: .normal)
}


@IBAction func changeLanguage(_ sender: Any) {
    
    if LocalizationSystem.sharedInstance.getLanguage() == "ar" {
        LocalizationSystem.sharedInstance.setLanguage(languageCode: "en")
        UIView.appearance().semanticContentAttribute = .forceLeftToRight
    } else {
        LocalizationSystem.sharedInstance.setLanguage(languageCode: "ar")
        UIView.appearance().semanticContentAttribute = .forceRightToLeft
    }
    
    let vc = self.storyboard?.instantiateViewController(withIdentifier: "vc") as! ViewController
    let appDlg = UIApplication.shared.delegate as? AppDelegate
    appDlg?.window?.rootViewController = vc
    
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@ericrawn, thank you for sharing this!

  1. Set Device Language
  2. Sign into Apple ID
  3. Change Country or Region
  4. Accept Apple’s Terms and Conditions
  5. Accept Apple’s Terms and Conditions
  6. Launch App Store

This topic was automatically closed after 166 days. New replies are no longer allowed.