feature image NSLocalizedString

Using the NSLocalizedString macro: Swift iOS

Thulani Mtetwa
2 min readAug 14, 2022

--

Well according to official documentation from Apple here is what the Marco is all about.

It returns a localized version of a string from the default table, which Xcode autogenerates when exporting localizations.

Here is how to use this macro for key values stored in the string files aka table in a project. This helps keep things organized and helps you have cleaner code.

In your project resources add a strings file. File -> New -> File -> Select strings file. Give it a suitable name.

I have named mine default, this is the table we will be pulling our string values from.

I then create an enum to help keep the value of my tables. This is in case I have multiple tables for my project handling different scenarios for my strings in a project.

In your project add a new swift file. File -> New -> File -> Select swift file. I have named mine Localizable+Tables.

And then I write up an extension to handle the returning of the value from the created table. Create another new swift file. File -> New -> File -> Select swift file. I have named mine NSLocalizedString+Localizable.

The only thing now left is for us to now use this in our code. In one of my controllers(views), I will demonstrate this. But this can be used anywhere, where you need to reference a string stored in a table. See the following code usage.

Thank you if you’ve made it this far. Please clap and comment for any other topic you’d like for me to cover in my next post.

Take care.

--

--

Thulani Mtetwa

I am an iOS App Developer. And worked on multiple apps in the finance and insurance industry.