iOS Tutorials: Localize your app with a button click

OVERVIEW

Localization expands your app coverage to people from different part of the world. Although English is an international language, there are still some part of the countries that prefer to speak in their native country and in order to break that language barrier, we use Localization to deliver the content of the app in the native language. Today, we will be doing the basic of changing language when we click on the language we desired.

Image from Apptamin

“Unless you try to do something beyond what you have already mastered, you will never grow.”
― Ronald E. Osborn

CONFIGURING THE STORYBOARD

Remember, there are many different way to do this. You could localize using Storyboard that generates the Object-ID for you. But this time, we are using Localizable.string way of localizing our app. The layout for this is really simple as we intend to only show different languages so we will only be using Label and SegmentedControl here.

This is where you are given the option to add languages. This is a pretty CRUCIAL step I would say.

CREATING LOCALIZATION.STRING

For this, you will have to create a new File > Resources > String File and in my case, I’ll name it Localization.string. Once you have created the file, you could Localize it by clicking on the Localize button.

And it will show you the available Localization and once you select it, it will create a Localizable file for you.

Since I am using English, Thai and Korean, I went to the respective files that were just created and created the key and value. The key here is “hello_world” and the value is “Hello World”. We are storing the different languages in different files and we call the Korean file, it will return us the Korean language.

// Localizable.string (English)
"hello_world" = "Hello World";
// Localizable.string (Thai) 
"hello_world" = "สวัสดีชาวโลก";
// Localizable.string (Korean)
"hello_world" = "안녕하세요 세계";

 

WRITING THE CODE TO LOCALIZE YOUR APP

We will creating this extension in the ViewController class. This is the code to read the bundle class and it will return the necessary bundle that we need.

extension String {
    func localizableString(_ name: String) -> String {
        let path = Bundle.main.path(forResource: name, ofType: "lproj")
        let bundle = Bundle(path: path!)
        return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
    }
}

I would assume that you have set up your own @IBOutlet. Here, we will be using an @IBAction for the SegmentedControl.

    @IBAction func changeLanguage(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            label.text = "hello_world".localizableString("en")
        case 1:
            label.text = "hello_world".localizableString("ko")
        case 2:
            label.text = "hello_world".localizableString("th")
        default:
            break
        }
    }

This is the tricky part. You need to call the correct localizableString or else it wont work. To do that, go to your Project and you’ll see the lproj created when you add the language in the first step. 

Once you are done, your program should work just like ours. Check out the project uploaded to GitHub.

 

 

 

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*