Postponing internationalization without feeling guilty

Being lazy isn’t shameful when you’re conscious about it

We all know that we should start to care about internationalization very early in the iOS development cycle. However, when your specification is changing rapidly or you’re experimenting a lot, switching between your code and Localizable.strings often can be quite distracting. On the other hand, if you don’t care about internationalization at all and just throw unlocalized strings straight to the UI, you’re going to encounter serious problems later.

So I propose a simple and elegant solution. Here it is:

extension String {
    
    #if DEBUG
        var unlocalized: String {
            return self
        }
    #else
        @available(*, deprecated, message: "You should not use unlocalized strings in release builds")
        var unlocalized: String {
            return self
        }
    #endif
    
}

And then, when we pass unlocalized strings to the UI, we simply mark them as such:

label.text = "The \(fruit.name) contains \(fruit.calories) calories".unlocalized

So what’s the deal with that? First, finding all unlocalized strings is a trivial task now (just search for “unlocalized” keyword). But more than that, now the compiler will warn us at every place where we use such strings when we switch to “Release” mode (of course you can adjust that logic however you want). The warnings looks like that:

Of course, you still have to remember to write those .unlocalized all over the place. So you still should care about internationalization early. And I never said you shouldn’t. But by writing .unlocalized , you’re making a conscious and intentional statement:

Yes, I know that I’m doing things wrong, and I will certainly fix them later — I just don’t want to lose my momentum right now.

And it’s great. You’re not just ignoring internationalization. You’re aware of it. You know how important that is. And you will do everything right, but just not right now. You’re postponing it.

So keep that momentum going.


Thanks for reading the post! Don’t hesitate to ask or suggest anything in the “responses” section below. You can also contact me on Twitter.

AnySuggestion

A Swift & Cocoa blog by Oleg Dreyman

Oleg Dreyman

Written by

Swift enthusiast. https://github.com/dreymonde

AnySuggestion

A Swift & Cocoa blog by Oleg Dreyman