How to Translate Your iOS App into Different Languages
In this article, I will talk about localizing and translating your iOS application into different languages. If your app supports more languages it’s better for you as a developer because more people can use it. It means your app can have more downloads and make more revenue.
So in this article, you will see how you can set this up for your app and by the end of it your app will be available in other languages.
Let’s get into it together!
So it’s actually pretty easy to add different translations for your strings in the application.
We are going to divide the process on 3 steps.
1. Create an extension
The first step is to create an extension for a String.
We need to create a simple function and it’s a best practice to call it “localized”.
So in this function we simply return NSLocalizedString and construct it with following values:
- We pass self for the key. This key represents a string in the default table.
- The table name will be the Strings File in which your translations will exist. And it’s typically called «Localizable»
- For the bundle we will use main. If you do not know what that main bundle is Apple gives us a good explanation:
The main bundle represents the bundle directory that contains the currently executing code. So for an app, the main bundle object gives you access to the resources that shipped with your app.
- The value will be self
- And the comment will be self as well
2. Use “localizable” function
So to use it, it super easy, you just add it to the any string you are using in your app like this:
3. Add Translations into the app
If you go ahead and run your app nothing will change. It’s because we have specified in our function where it should take translation but we haven’t yet created them. So there is no translation for this specific key we provide and that’s why it defaults to the value we wrote down before. So, let’s create our translations.
Go ahead and select your project. And what you will see there is the heading Localizations. Click plus and select the language you want your app to be translated to. Now it will ask you which interface files you are going to localize.
But what if we don’t use interface files (storyboard, .xib)? Then we will add it another way!
Click create New File. Search for “Strings” and you will see two files, first called Strings File and second called Stringsdict File.
We are going to use the first one and make sure to give it a name how you have called it in your extension function. I’ve called it “Localizable”, so I provide this name for my file.
So now if your inspectors tab is closed then open it up and under File Inspector hit the Localize Button. It will ask you what is your base language. My base language is English so that what I’m going to use.
After that in the same place you will see another language you specified, so just check that box too. And now you will see that Localizable.strings file has two other files with the name of the language in the end of it.
Now let’s create our first value in English file.
We are going to write «Hello» = «Hello»;
The first value is the key and we specified the same value as the strings is because if you remember in our extension function we provide self as the key.
And now let’s go to the other file with different language and let’s provide a translation for this key.
«Hello» = «Привет»;
So the key is the same, but the value this time will be an actual translation to a language you have chosen. In my case it is Russian.
And now if we run our app it will be translated to this language, but just make sure your phone language is of the language you wanted your app to be translated to, otherwise you will see your string in the base language which is English for me.
But I’m also going to tell you how you can change the language of a simulator to test it without an actual phone.
Select your target and click Edit Scheme. It will open a window where you will see a specified App language. Change it to another language and run a simulator!
That’s it for this article! Now you can translate your app into different languages. Congratulations!!!
Stay tuned for other articles I’m about to share with you!