JsonLocalizable 1.1

JsonLocalizable 1.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Andres Silva.



  • By
  • Kekiiwaa

JsonLocalizable

JsonLocalizable is a framework writed in swift to localize your projects easier, including storyboards and strings.

JsonLocalizable Storyboard


Features

  • [x] Localize your strings
  • [x] Localize your Storyboards without extra files
  • [x] Localize your UIView components without xcode UIView ids
  • [x] Localize your UIView components only with keys
  • [x] Localize your UIView components with classes
  • [x] Update your current language and update all view components
  • [x] Update your current language and receive notification

Requirements

  • iOS 8.0+
  • Xcode 8.0+
  • Swift 3.0+

Installation

Swift Pacakge Manager

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 JsonLocalizable as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .Package(url: "https://github.com/Kekiiwaa/JsonLocalizable.git")
]


Usage

Config

This not is necesary, only if you need diferent results.

// AppDelegate.swift

import JsonLocalizable

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let localize = Localizable.shared
    // Set your file name
    localize.fileName = "myFileName"
    // Set your default languaje.
    localize.defaultLanguage = .french
    // If you want change a user language different to default in device, use this method.
    localize.update(language: .english)
    // If you want remove storaged languaje, use
    localize.resetLanguage()
    // The used language that you configured to localize
    print(localize.language())
    // List of storaged languajes
    print(localize.languages())
    // List of aviable languajes
    print(localize.availableLanguages())

    return true
}

Create your json file

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

Localize strings

print( "hello.world".localize() )

// Hello world!

Localize strings, replacing text

JsonLocalizable use % identifier to replace the text

print( "hello.name".localize(value: "everyone") )

// Hello everyone!

Localize strings, replacing many texts

JsonLocalizable use % identifier to replace the text

print( "values".localize(values: "everyone", "Software Developer") )

// Hello everyone we are Software Developer, see you soon

Localize strings, replacing dictionary values

JsonLocalizable use :yourid to search your id in JSON File

print( "username".localize(dictionary: ["username": "JsonLocalizable"]) )

// My username is JsonLocalizable


Localize your storyboard with extensions

You don’t need import anything in your code, JsonLocalizable use extensions to localize your UIView components

// This is lang-en.json
{
    "navigation" : {
        "title" : "JsonLocalizable"
    },
    "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."
    }
}

JsonLocalizable Storyboard

This is the result in your simulator

JsonLocalizable Simulator

You can use extensions for

  • UIBarButtonItem
  • UIButton
  • UILabel
  • UINavigationItem
  • UISearchBar
  • UISegmentedControl
  • UITabBarItem
  • UITextField
  • UITextView


Localize your storyboard with classes

You don’t need import anything in your code, JsonLocalizable use extensions to localize your UIView components

// This is lang-en.json
{
    "navigation" : {
        "title" : "JsonLocalizable"
    },
    "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."
    }
}

JsonLocalizable Storyboard

This is the result in your simulator

JsonLocalizable Simulator

You can use this classes

  • LocalizableBarButtonItem
  • LocalizableButton
  • LocalizableLabel
  • LocalizableNavigationBarItem
  • LocalizableSearchBar
  • LocalizableSegmentedControler
  • LocalizableBarItem
  • LocalizableTextField
  • LocalizableTextView


Updating language

When you change a language, automatically all views update your content to new language

let localize = Localize.shared
localize.update(language: .french)

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 Localizable.shared.availableLanguages() {
        let displayName = Localizable.shared.displayNameForLanguage(language)
        let languageAction = UIAlertAction(title: displayName, style: .default, handler: {
            (alert: UIAlertAction!) -> Void in
            Localizable.shared.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)
}


Credits

Kekiiwaa Inc, Andres Silva Gomez, Andres Felipe Montoya

License

JsonLocalizable is released under the MIT license. See LICENSE for details.