Michael Charland

Swift Developer

Home

How I added localization to Brooklyn

Published Oct 15, 2019

Overview

Brooklyn is a fantastic screen open source MacOS app written by Pedro Carrasco. It allows you to set an Apple theme as your screen saver. During Hacktoberfest Brooklyn had an open feature request to add localization to the app. Hacktoberfest is a once a year month long open source coding challenge, where contributors are challenged to open at least 5 pull requests. I really wanted to contribute to open source, and get the free shirt and stickers. Also I’ve done localization work in the past in my BlackBerry, Jaybird, and TextNow days. But I had never done it for a MacOS app. So this was my chance.

The Beginning

I did the typical DuckDuckGo search finding out how others have done it. Read some Apple Docs on what they recommend. And thankfully it ended up being pretty straightforward and similar to how you do it on an iOS App. The big difference was you had to deal with menus. I really liked how in this article Mario created a localizable protocol with a localized variable and one for XIBLocalizable also. This would make adding the ability for the app to be localized require less code changes and read cleaner.

Forking

Next up, I needed to start changing the Brooklyn code and I couldn’t just change the code in the Pedro’s repo as I’m not a collaborator on that repo. So I had to fork my own copy. I love this feature on GitHub. It’s so easy to do and so powerful. What it does is copy all the files into a new repo on your account. Then you can do what ever you want with the code, if you want to contribute back you can open up a Pull Request back to the repo you forked from.

Discovering how Brooklyn did localization

Now I started digging through the Brooklyn code to discover how the strings in the app where handled. I found some hardcoded strings, some strings in XIBs, and some localized strings. Nothing too crazy or hard to reason about how it would need to change.

Writing Code

This provided a great platform to try out using the Localizable and XIBLocalizable protocols. Basically it was adding the following to extensions for NSTextField, NSButtonCell, NSWindows, NSMenu, and NSMenuItem.

@IBInspectable var xibLocKey: String? {
        get { return nil }
        set(key) {
            if let localized = key?.localized, key != localized  {
                self.title = localized
            }
        }
    }

This went pretty smooth and was super easy to to see the changes in the xib about what. There were many menu items that had to be replaced to using these protocols. Here is an example of setting the key for the save menu item.

Pull Request Time

I cleaned up the code I changed trying to remove any junk from the commit and trying to make it as small and focused as possible. This is what I ended up with: https://github.com/pedrommcarrasco/Brooklyn/pull/60

Pull Request Feedback

The author of the repo wanted changes such as he didn’t like the naming convention I picked not being specific enough. This was a good point as I leaned too heavily on context. There some other comments I addressed too. Thankfully I hadn’t missed anything large scale and was inline with what he was looking for.

Pull Requested Accepted

After I addressed the feedback from Pedro, he accepted and the app now supports multiple languages. This was a fun learning experience in which I learned how to localize a MacOS app and use the Localizable and XIBLocalizable protocols. Thank you Pedro for letting me add localization it was a great learning experience.