Localization & CocoaPods

Localization requires you to add appropriate resources to your software to ensure that a given country, locale, language, or culture is supported. Nowadays, it’s not uncommon to make your mobile app localizable because it allows your app to access a global market.

The Problem

My colleague and I recently encountered a problem when making our app localizable. The reason is that our project is separated into many different components, such as UI and networking, and then we integrate those components together with CocoaPods. Therefore, there’re a lot of strings which aren’t located in our main project, for example, button’s title and text field’s placeholder.

Possible Solutions

The first solution comes into my head is we can make each component localizable at first and then copy all localizable strings into our main project. However, it will cause lots of string duplications and even conflict in our project, so it doesn’t seem to be an appropriate solution.

The second thought is we can move all strings from each component to our main project and then make our main project localizable. It seems to be the best solution because we will maintain only one Localizable.strings file in future. However, the effort is quite huge at this stage because we need not only to reimplement those setTitle and setTextmethods but also to change the existing interface of several components.

After discussion and survey, we decide to take the advantage of CocoaPods to achieve localization. We still make each component localizable at first and write resource_bundle in each Podspec file as following:

spec.resource_bundle = { "my_bundle_name" => ["my_component_directory/*.lproj/*.strings"] }

CocoaPods will copy those Localizable.strings files to the main project and generate a new bundle to prevent name collisions. As the result, it won't cause many duplications as well as conflict and the existing interface won't be changed. However, we cannot use NSLocalizedString function at all because it will use the main bundle and our components' Localizable.strings won't exist in the main bundle. Instead, it's necessary to use the new bundle that generated by CocoaPods. In order to accomplish that, we can adjust a little bit in each component as following:

let path = Bundle(for: MyUIClass.self).path(forResource: "my_bundle_name_in_podspec", ofType: "bundle")!
let bundle = Bundle(path: path) ?? Bundle.main
label.text = NSLocalizedString("my_localizable_key", bundle: bundle, comment: "my_comment")


This is a temporary solution of localization and we will come back to move all strings into our main project in the future because it seems to be the best solution after all. However, this solution is very convenient if you are building up a library, because it still works for image assets and interface builders. Any comment and feedback are welcome, so please share your thoughts.

Thank you!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.