Try use library Localize for change language


link GitHub https://github.com/andresilvagomez/Localize

You can use Cocoapod or Carthage, for install cek link github Localize, this time I try localize use xib file. let's start.

1. Create new file ".xib" and ".swift" file
2. Intall library Localize
3.  In xib file use label and button,  in view you can give 4 label and 1 button like image below.



4.  Follow or copy this code. in full file

- LocalizeViewController.swift

import UIKit
import Localize

class MenuViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

     
    }
    @IBAction func bAction(_ sender: Any) {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alert.addAction(UIAlertAction(title: "English", style: .default, handler: { _ in
            Localize.update(language: "en")
        }))
        alert.addAction(UIAlertAction(title: "Indonesia", style: .default, handler: { _ in
            Localize.update(language: "id")
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        present(alert, animated: true, completion: nil)
    }
}


- AppDelegate.swift

import UIKit
import Localize

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

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: "id")
// 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.currentLanguage)
// List of aviable languajes
print(localize.availableLanguages)

// Or you can use static methods for all
Localize.update(fileName: "lang")
Localize.update(defaultLanguage: "id")
Localize.update(language: "en-DE")
        
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = UINavigationController(rootViewController: MenuViewController())

return true
}

func applicationWillResignActive(_ application: UIApplication) {}
func applicationDidEnterBackground(_ application: UIApplication) {}
func applicationWillEnterForeground(_ application: UIApplication) {}
func applicationDidBecomeActive(_ application: UIApplication) {}
func applicationWillTerminate(_ application: UIApplication) {}


}

No comments: