Dynamic Localization in iOS

Bharathi Kumar
4 min readJul 18, 2020

Localization or Internationalization is the way of presenting your apps to the global audiance. In other terms, its the ability of your app to present itself in multiple languages. Its the sole responsibity of the app developer to provide the required translation content in an already defined format, while iOS will handle the rest based on the language set in the iOS device.

Apps without localization:

By default, apps willn’t support Localization. In other words, the apps content will be displayed in only one language. Irrespectivve of the device language settings, your app will be loaded with that one language when the app is shipped.

Apps with localization:

In order to support localization, the first step is Select the XCode project -> Project-> under Info tab -> Localization.

By default, a base language will be provided, i.e, english. This base language will act as a fallback language if any localizable content is not found. I will describe about this later in this section.

Click on + button and proceed to add the language.

You can see the file structure gets updated after you add a new language.

Click on New -> File -> Strings File -> and name it as Localizable.strings

Select “Localize” from the popUp, you could see the file structure gets updated and the Localizable files gets created for the added language.

Open localizable.strings(english) file and add the content as you can see in the image.

Follow the same for localizable.strings(hindi)

Now open your view controller, and refer to the localizable string as follow.
self.title = NSLocalizedString(“key1”, comment: “”)
and change the device language.

As you can see when you select the device language to english or hindi, the localizable key will read value from the respective file.

Try change the device language to a language other than english and hindi. This time the value will read from the base file. Earlier i told about fallback language.

For more info about Localization in iOS, please refer the below tutorial.

Advantages:

  • The app can be used by people across the globe.

Disadvantages:

In case if the developer needs to modify any content of the language file, the changes needs to be updated in the project and the app needs re-submission.

Solution:

The solution for this is to make the language file content dynamic, i.e, during the runtime of the program the developer should be able to make changes of the language file content.

iOS wont allow us to modify the Main Bundle once the app is shipped. The trick is to create a custom bundle and write our language file content dynamically, and then read the proper bundle based on the language selection.

Just read the following guidelines:

To put down into points:

1. Don’t depend on the default language file content in the app bundle, instead create a web service which will feed the app with the language file content.

2. When the app starts, fetch the language content from the web service and write it in the documents directory.

3. Change the bundle based on the user selection language.

4. Update the views on ViewWillAppear method to change the content language dynamically.

5. This can be done in the viewWillAppear method of the UIViewController class, or through a custom method which will be invoked using a NSNotification., all depends on your needs.

6. This tutorial is to provide a demo for those who are in need of dynamically changing the language content.

7. Using this method, if the developer wants to change the value of a word, he can make the changes in the backend and the same will be reflected in the app, without the need for resubmitting the app to the appstore.

A fully working Xcode project is available in the below link. Please feel free to try it and comment in case of doubts:

--

--