TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Nov 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Kunstmaan Labs.
Example project can be found here i18n-swift-example
The preferred installation method of installation is using CocoaPods, this way you can configure the @ibdesignable and @IBInspectable properties in the Interface Builder. Apparently when using a standard IOS Framework these properties don't appear in the Interface Builder.
http://stackoverflow.com/questions/29933691/ibdesignable-from-external-framework
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate KunstmaanI18nSwift into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'KunstmaanI18nSwift', '~> 2.0'
Then, run the following command:
$ pod install
Finally add this to your Bridging-Header.h file:
#import <KunstmaanI18nSwift/KunstmaanI18nSwift.h>
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate KunstmaanI18nSwift into your Xcode project using Carthage, specify it in your Cartfile
:
github "Kunstmaan/i18n-swift" ~> 2.0
Run carthage update
to build the framework and drag the built KunstmaanI18nSwift.framework
into your Xcode project.
When the i18n module is loaded it will try to configure it self based on the preferred languages configured on the device. If no matching language is found there will be no language configured.
Following properties of UIViews have localization support inside the Interface Builder (the properties can also be set by code):
Special case:
By providing the baseName of the image, the view will look for the image named "baseName (locale)". It's possible to override this format by extending the i18n class and overriding:
public func localizedImageNameFor(name: String, lang: String?) -> String
You can make use of the String extension to quicky translate a key:
"your.translation.key".i18nLocalized
Or with extra options:
"your.translation.key".i18nLocalized(defaultValue: String, table: String, arguments: ...)
Or you can use the i18n class, to use this you should import the i18n module on top of your Swift classes where you want to use this:
import KunstmaanI18nSwift
This will make the i18n class available for you and gives you access to the following API:
i18n.possibleLanguages
This returns an array of languages which are configured for your project (for example: ["en", "nl"])i18n.language
This returns a String value of the current configured language (for example: "en")i18n.locale
This returns an NSLocale value of the current configured languagei18n.hasLanguage
Returns a boolean indicating if a current language is configuredi18n.setLanguage(lang: String)
Set the current languagei18n.isCurrentLanguage(lang: String)
Returns wheither the given language is the same as the current configured onei18n.clear()
Removes any configured languagei18n.localizedStringForKey(key: String, defaultValue: String, table: String, arguments: ...)
Returns the translated value for this translation keyi18n.localizedImageForName(name: String)
Returns the localized UIImage for the given name ("baseName (locale)")For now there is only one event being dispatched and that is when the Language is being changed. You can listen for this event as follows:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "onLanguageChange", name: I18n.Events.OnChange, object: nil)
public func onLanguageChange(notification: NSNotification) {
if let info = notification.userInfo as? Dictionary<String,String>, lang = info["lang"] {
let oldLang = info["oldLang"]
// Do something here
}
}