iOS Localization Advices (Swift 5)

Photo by Deanna Ritchie on Unsplash

Intro

If you want to make worldwide-known app available in dozens of countries you should adapt your app to different languages. It is much more convenient for user to have app in his/her native language.

I want to share how to improve your iOS localization process.

Part I: Add Internationalization to your project

Enabling Base Internationalization

First off all, you should enable project localization.

  1. In theProject Navigator, select the project
  2. Click Info tab
  3. Check on Use Base Internationalization
Enabling Base Internationalization

After that you can add new languages by tapping "+" icon (above “Use Base Internationalization”)

For more details check this step-by-step guide or documentation.

Create Localizable.string

  1. Create New String File named Localizable (New FileString File).
  2. In File Inspector (right panel) click on Localize.. button.

It’s your Localizable.strings file for Base language.

The syntax of this file is:

So now it’s a time to improve our localization handling!

Part II: Localizable Management

For using a localized string programmatically, you should use the NSLocalizedString function:

In brief, by calling the key programmatically NSLocalizedString func returns the localized value.

But for managing all localized strings we need more type-safe approach that helps to avoid calling hardcoded strings.

First of all,

Split all localized strings by pages or by meaning, and create an enum for each part

Secondly, create LocalizableDelegate protocol which we will use in enums:

So LocalizableDelegate has 3 variables. table is the name of table where to search specific key. localized returns a value by rawValue located in the table. By default table is nil, it means that the localizedString method attempts to use the table Localizable.strings.

The next step, is to adopt LocalizableDelegate in enum

You can use specific file for each View or ViewController. In this case you have to declare table variable:

Example of usage:

I have created Localizable enum containing all localization enums

Advantages

  1. Type-safe approach that helps to avoid calling hardcoded strings;
  2. Manage all localized strings from one place;
  3. Simplified change of localizable value;
  4. You can refer to one localized string from more than one enum.
iOS Localization using Swift

Thanks for reading.
I hope these iOS localization tips can help you improve your workflow.

Check my articles: how to create own custom xCode Template or Countdown Timer in Swift 5 for iOS.


You may check my apps.


If you have any questions or found any errors, please drop me a line!

Written with ❤️ by Rita