Requirements: iOS 9.0+ • Swift 5.0+
In your AppDelegate:
import SMLocalize
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ReloadableAppDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Uncomment the next line if u want to use Arabic as the default language at the first app launch before the user changes the language manually.
// SMLocalize.defaultLanguage = "ar"
SMLocalize.configure()
reload()
return true
}
func reload() {
if window == nil {
window = UIWindow(frame: UIScreen.main.bounds)
window!.makeKeyAndVisible()
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateInitialViewController()
window!.rootViewController = vc
}
}
Then in your change language action:
import SMLocalize
class ViewController: UIViewController {
...
...
@IBAction func changeLanguageTapped(_ sender: UIButton) {
SMLocalize.currentLanguage = "ar" // Your new language
SMLocalize.reloadAppDelegate()
}
}
Playing an animation during language changes.
In your change language action:
import SMLocalize
class ViewController: UIViewController {
...
...
@IBAction func changeLanguageTapped(_ sender: UIButton) {
SMLocalize.currentLanguage = "ar" // Your new language
// Optional animation. Change to nil if not needed.
SMLocalize.reloadAppDelegate(animation: [.transitionFlipFromRight, .curveEaseOut], duration: 0.3)
}
}
Setting a default language to be set on the first app launch before the user changes the language.
In your AppDelegate:
import SMLocalize
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ReloadableAppDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SMLocalize.defaultLanguage = "ar" // Must be set before SMLocalize.configure()
SMLocalize.configure()
reload()
return true
}
}
Flipping images to match the current language direction, e.g., Arrows.
In your AppDelegate:
import SMLocalize
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ReloadableAppDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
SMLocalize.configure()
// Flip images in views with tags from 1 to 10
// Avoid including 0 in this set since it will cause UIKit issues.
SMLocalize.flipImagesInViewsWithTags = Set(1...10)
return true
}
}
In your ViewController:
import SMLocalize
class ViewController: UIViewController {
...
...
override func viewDidLoad() {
super.viewDidLoad()
arrowImgToFlip.tag = 1
anotherImgToFlip.tag = 2
myContainerView.tag = 5
imgInsideMyContainerView.tag = 6
}
}
View | Does support flipping its images? | Note |
---|---|---|
UIImageView | ✅ | _ |
UIButton | ✅ (For all states) |
_ |
UISlider | ✅ Thumb Image (For all states) ✅ minimumValueImage ✅ maximumValueImage ❌ minimumTrackImage ❌ maximumTrackImage |
_ |
UICollectionViewCell | ❌ | Use UIImage.imageFlippedForRightToLeftLayoutDirection() in your cellForItem delegate function |
UITableViewCell | ❌ | Use UIImage.imageFlippedForRightToLeftLayoutDirection() in your cellForRow delegate function |
To run the example project, clone the repo, and open SMLocalizeExample.xcworkspace from the Example directory.
SMLocalize is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SMLocalize'
- Support installation through Carthage & Swift Package Manager (Help Needed)
- Localize Views with Text automatically
- Improve the library API?
SMLocalize was inspired by these libraries. Uses the same techniques in some parts and deviates in others.
Steven, StevenMagdy92@gmail.com
SMLocalize is available under the MIT license. See the LICENSE file for more info.