Localize is a framework writed in swift to localize your projects easier improves i18n, including storyboards and strings.
- Keep the Localizable.strings file your app already uses
- Support Apple strings and JSON Files
- Change your app language without changing device language
- Localize your Storyboards without extra files or/and ids
- iOS 8.0+
- Xcode 8.0+
- Swift 3.0+
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsCocoaPods 1.1.0+ is required to build Localize 1.+.
To integrate Localize into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
target '<Your Target Name>' do
pod 'Localize' , '~> 1.5.2'
endThen, run the following command:
$ pod installCarthage 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 carthageTo integrate Localize into your Xcode project using Carthage, specify it in your Cartfile:
github "Kekiiwaa/Localize"
Run carthage update to build the framework and drag the built Localize.framework into your Xcode project.
The Swift Pacakage Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.
Once you have your Swift package set up, adding Localize as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [
.Package(url: "https://github.com/Kekiiwaa/Localize.git")
]Add .localize() for any String if you want localize.
You don't need import anything in your code, Localize use extensions to localize your Strings.
textLabel.text = "hello.world".localize()You can decide if you want use JSON or Apple Strings, we support both, if you decide use JSON please following this instructions.
Please create a JSON file in your code with this rule:
{your file name}-{your lang code}.json
For example
- lang-en.json
- lang-es.json
- lang-fr.json
Example JSON File
{
"hello" : {
"world" : "Hello world!",
"name" : "Hello %!"
},
"values" : "Hello % we are %, see you soon",
"username" : "My username is :username",
"navigation.title" : ""
}If you decide use Apple strings, please follow Apple Localization Guide to create strings file.
String file example
"hello.world" = "Hello world!";
"name" = "Hello %";
"values" = "Hello everyone my name is % and I'm %, see you soon";
"username" = "My username is :username";
"level.one.two.three" = "This is a multilevel key";
"the.same.lavel" = "This is a localized in the same level";
"enlish" = "This key only exist in english file.";
Whatever way you choose to, use that methods.
print( "hello.world".localize() )
// Hello world!Localize use % identifier to replace the text
print( "hello.name".localize(value: "everyone") )
// Hello everyone!Localize use % identifier to replace the text
print( "values".localize(values: "everyone", "Software Developer") )
// Hello everyone we are Software Developer, see you soonLocalize use :yourid to search your id in JSON File
print( "username".localize(dictionary: ["username": "Localize"]) )
// My username is LocalizeIf you decide use different files use methods with tableName in the end of each method, for example.
print( "hello.world".localize(tableName: "Other") )
print( "hello.name".localize(value: "everyone", tableName: "Errors") )
print( "values".localize(values: "everyone", "Software Developer", tableName: "YourFileName") )
print( "username".localize(dictionary: ["username": "Localize"], tableName: "YourFileName") )You don't need import anything in your code, Localize use extensions to localize your UIView components
- lang-en.json
{
"navigation" : {
"title" : "Localize"
},
"app" : {
"label" : "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.",
"textfield" : "Write some here."
}
}You can use extensions for
UIBarButtonItemUIButtonUILabelUINavigationItemUISearchBarUISegmentedControlUITabBarItemUITextFieldUITextView
When you change a language, automatically all views update your content to new language
Localize.update(language: "fr")But with strings not is posible, for that your need implement a notification
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(localize), name: NSNotification.Name(LanguageChangeNotification), object: nil)
}
public func localize() {
yourLabel.text = "app.names".localize(values: "mark", "henrry", "peater")
otherLabel.text = "app.username".localize(value: "Your username")
}Implementing internal acction to change a language
@IBAction func updateLanguage(_ sender: Any) {
let actionSheet = UIAlertController(title: nil, message: "app.update.language".localize(), preferredStyle: UIAlertControllerStyle.actionSheet)
for language in Localize.availableLanguages() {
let displayName = Localize.displayNameForLanguage(language)
let languageAction = UIAlertAction(title: displayName, style: .default, handler: {
(alert: UIAlertAction!) -> Void in
Localize.update(language: language)
})
actionSheet.addAction(languageAction)
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: {
(alert: UIAlertAction) -> Void in
})
actionSheet.addAction(cancelAction)
self.present(actionSheet, animated: true, completion: nil)
}This not is necesary, only if you need diferent results.
// AppDelegate.swift
import Localize
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let localize = Localize.shared
// Set your localize provider.
localize.update(provider: .json)
// Set your file name
localize.update(fileName: "lang")
// Set your default languaje.
localize.update(defaultLanguage: "fr")
// If you want change a user language, different to default in phone use thimethod.
localize.update(language: "en")
// If you want remove storaged languaje use
localize.resetLanguage()
// The used language
print(localize.language())
// List of aviable languajes
print(localize.availableLanguages())
// Or you can use static methods for all
Localize.update(fileName: "lang")
Localize.update(defaultLanguage: "fr")
Localize.update(language: "en-DE")
return true
}Special thanks to Benjamin Erhart
Localize is released under the MIT license. See LICENSE for details.

