Localization in iOS using Swift 5

Vitalii Zaitcev
3 min readDec 2, 2020

There are many articles on how to localize your application and the goal of every developer is to choose the right one for the project. In this article, I use the power of enums to achieve that.

So let’s jump in and create 3 Strings file which we are going to use: Common, Error, Main. The idea here is to granulate strings as much as you want, feel free to add more files for each feature: Profile, Settings, etc.

After that, we want to add some keys (otherwise, what’s the point of it all?).

Common.strings
Common.strings
Error.strings
Error.strings
Main.Strings
Main.strings

Halfway there! Nice. Let’s write some code.

Traditionally, the implementation looks like this:

But we are not here for that. Let’s create Localization User Story for each .string file instead. It’s enum time!

Very easy, right? But how does it help us, you probably want to ask me. Well, let’s add another function to get a value from a story:

By the way, the raw value of an enum case should match the name of .string file in order to fetch string from the file.
To use this approach we just need to call:

Please, feel free to leave a comment if you want me to go over in more detail what this function does under the hood.

And that’s it. Now you can easily migrate shared .string files from project to project and avoid having huge files with all strings which is hard to maintain.

But from here we can make it even better! Simply using extension:

Or by having another enum for each key:

Thank you for reading my article! Let me know if you have any questions.

--

--