Localizing an iOS app is pretty straightforward thanks to Apple’s app internationalization options, whether you want to do it from interface builder or code. Things get a bit more complicated when you have some code which you want to separate into a framework (e.g. registration process, profile settings, etc.) in order to use through multiple projects.
You might still use the same way as above to translate the app and framework separately, but often times you’d want full control over the translations. In that case it’s best to add a class which would handle this work. The class is shown in the code below:
import Foundation public class Lang: NSObject { static let kLocalizedStringNotFound = "kLocalizedStringNotFound" public static func string(_ key:String) -> String { // Try app bundle var string:String = Bundle.main.localizedString(forKey: key, value:kLocalizedStringNotFound, table:nil) // Try the framework bundle if string == kLocalizedStringNotFound, let bundle = Bundle(identifier:"com.yourframework.bundleid") { string = bundle.localizedString(forKey:key, value:kLocalizedStringNotFound, table:nil) } // Translation not found if string == kLocalizedStringNotFound { print("No localized string for '\(key)'") string = key } return string; } }
The method named “string” receives a translation key, tries to translate it from the app (main bundle), then searches for translation in framework’s bundle. This enables you to override frameworks’ translations if needed.
If the app allows users to chose the app language independently of the device settings, this method can easily be extended to select a specific translation file, since all the translation code is in one place.