Skip to content

StevenSorial/SMLocalize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SMLocalize Version Swift 5 License Platform

An iOS library for changing localization at runtime.


Requirements: iOS 9.0+ • Swift 5.0+

Basic Usage

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()
  }
}

Animation

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)
  }
}

Default Language

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

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
  }
}

Views that supports flipping its images:

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

Example for more information about how to use the library

To run the example project, clone the repo, and open SMLocalizeExample.xcworkspace from the Example directory.

Installation

SMLocalize is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SMLocalize'

TODO

  • Support installation through Carthage & Swift Package Manager (Help Needed)
  • Localize Views with Text automatically
  • Improve the library API?

Credit

Other Libraries

SMLocalize was inspired by these libraries. Uses the same techniques in some parts and deviates in others.

Articles

Author

Steven, [email protected]

License

SMLocalize is available under the MIT license. See the LICENSE file for more info.