TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2018 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✗ |
Maintained by Jindrich Dolezy.
LocalizeNIB helps you with localizing storyboards and XIB files. Put everything you want to be localized to localizables
outlet collection in your UIViewController
and LocalizeNIB will use NSLocalizedString
on everything. No more messing with unreadable component IDs!
LocalizeNIB is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "LocalizeNIB"
LocalizeNIB uses NSLocalizedString
by default. If you have your Localizable.strings file ready, then all you need to do is add all components you want to localize to localizables
outlet collection (as seen on the picture below). LocalizeNIB will use string values set in storyboard as a key for localization. There are extensions for common UIKit components so you don’t need to use custom classes if you don’t want to.
Setting LocalizeNIB.instance.debugMode
to true will enable debug mode. LocalizeNIB will log unknown string keys and objects that either failed localization or do not support localization at all. Logging unknown keys works only
If you use your own logging solution you integrate it with LocalizeNIB by providing LocalizeNIB.instance.debugBlock
.
You can set LocalizeNIB.instance.stringProvider
block and provide custom localized strings. This block will be called quite a lot, so it should return quickly.
Following example takes all passed-in keys and simply returns their uppercased version.
LocalizeNIB.instance.stringProvider = { $0.uppercased() }
You can provide custom localization to any object by implementing Localizable
protocol. Extensions for UIKit classes are provided by default.
extension UILabel: Localizable {
open func localize(provider: LocalizedStringProvider) throws {
text = text.flatMap { provider($0) }
}
}
You can customize localization process or exclude some objects from localization by providing LocalizeNIB.instance.localizeAll
block. This block is called for every object being localized. You can skip localization for this object if you return true in this block.
Following example takes all UILabel
components with localizeAll
text, provides custom text for it.
LocalizeNIB.instance.localizeAll = { object, stringProvider in
if let label = object as? UILabel, label.text == "localizeAll" {
label.text = "Caught by localizeAll block"
return true
} else {
print("About to localize \(object)")
return false
}
}
When customizing behaviour of LocalizeNIB.instance
and using default storyboard, you must do all customization in the init method of your UIApplicationDelegate
otherwise the storyboard will be localized before your customizations. See AppDelegate in example project for reference.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Jindra Dolezy, [email protected]
LocalizeNIB is available under the MIT license. See the LICENSE file for more info.