Localization in iOS with Property Wrapper

A different approach to implement Localization using Property Wrapper and also automate the code generation required for project

Mukesh Mandora
3 min readMay 25, 2020

NSLocalizedString

I am sure most of us have used or implemented custom wrapper over NSLocalizedString to access to localized strings in their code.

NSLocalizedString("key_name", comment: "comment")

Most of us centralized the NSLocalizedString keys in single file as it is not good idea spread raw strings across app and there are high chances of error, But that still doesn’t solve our problem to write the above function again and again every time we set the strings to our UI components.

Also imagine if you are working across the Teams Example: (Content, Tech) where Content team share localization files to Tech team.

So Definitely we need some developer friendly syntax to ease the process of exporting the localization code and also accessing the localized strings in our code.

Let’s Start

I am not covering the part about how to add multiple Localization in your app, It’s pretty straight forward from the below image, Click on +(plus) icon and add the localization you want and it will create all resource files in the project.

Screenshot from sample app supporting Localization

Now, We will first create Localizable propertyWrapper to handle the logic of getting the localized string from the particular key.

Then we will create Strings enum where we will store all localized keys. You can also distribute it across many features in your app. But for simplicity we will store all in single enum.

enum Strings {
@Localizable static var featureOneTitle = "featureOneTitle"
@Localizable static var featureTwoTitle = "featureTwoTitle"
}

Now with single line of code we can access our localized string and set to any UI Components or Variables, For Example

label.text = Strings.featureOneTitle

Automate the Enum(Strings) code generation

Okay now Let’s automate this, We don’t want to generate Strings keys every time when changes are made by Content team to improve the copy or when new strings are added for any feature.

In this example, Consider Content team are using Google sheet where all keys added, Like below screenshot

Screenshot of Sample Google Sheet example

Now we will create custom script to generate our code, I took this inspiration from this git repo. I tweak the script for my own need to create code for my case, You can check the script from my git repo link and use it.

The script will help you with generating all code required for Localization. Like for our current example it will give you the enum and all Localizable strings required for your project.

  • Strings Enum
enum Strings {
@Localizable static var home_title = "home_title"
}
  • Localizable.strings (English)
“home_title” = “Home”
  • Localizable.strings (Thai)
“home_title” = “หน้าหลัก”

In this way we automate the code generation for Localization and build a custom Localization wrapper to use across our app.

Thank You 😊

I hope this article helps you in implementing Localization in your app, Do share your valuable feedback.

References

--

--