Latest1.1.01
Homepagehttps://github.com/JsonBin/WBLanguage
LicenseMIT
Platformsios 8.0, requires ARC
Authors

Build Status
Pod version
Carthage Compatible
Platform info

gif

What

WBLanguage is a high level LanguageKit. It provides a High Level API set international language for your app.

Features

  • Less time to switch the language
  • Set the language more convenient
  • Supported languages: English/French/Italian/German/Russian/Chinese-Hans/Chinese-Hant
  • Subsequent can add more national languages

Installation

WBLanguage supports multiple methods for installing the library in a project.

CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like WBAlamofire in your projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.2.0+ is required to build WBLanguage.

Podfile

To integrate WBLanguage into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target 'TargetName' do
use_frameworks!

pod 'WBLanguage'

end

Then, run the following command:

$ pod install

Carthage

Carthage 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 carthage

To integrate WBLanguage into your Xcode project using Carthage, specify it in your Cartfile:

github "JsonBin/WBLanguage"

Run carthage to build the framework and drag the built WBLanguage.framework into your Xcode project.

Requirements

  • iOS 8+
  • Xcode 9.0+
  • Swift 4.0+
WBLanguage VersionMinimum iOS TargetNote
1.xiOS 8Xcode 9+ is required.

you must create a ‘Language.bundle’ resources in your project to hold the international language.

Usage

WBLanguageManager class

We can set WBLanguageManager’s property at the beggining of app launching, the sample is below:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        WBLanguageManager.duration = 2.0
        return true
    }

if you want to set a new language, you can use setLanguage(_:) to update the UI. And it will automatically save the language of your choice.

WBLanguageManager.setLanguage(type)

you can use textForKey(_:, value:) to get the local text from *.lproj.

let string = WBLanguageManager.textForKey("key")

WBLanguage class

WBLanguage supports serializing and deserializing any custom type as long as it conforms to the WBLanguageProtocol protocol.

public extension WBLanguageProtocol {
    public var lt: WBLanguage<Self> {
        return WBLanguage(self)
    }
}

extension `yourCustom` : WBLanguageProtocol {}

Note: WBLanguageProtocol is a protocol that WBLanguage uses internally to directly. So you need to complete the language setting.

For example:


private func setLanguagePicker(
    _ object: NSObject,
    Selector selector: String,
    Picker picker: WBLanguagePicker?
) {
    object.languagePickers[selector] = picker
    object.performLanguage(selector, Picker: picker)
}

public extension WBLanguage where T : UILabel {
    public func setPicker(_ picker: WBLanguageTextPicker?) {
        setLanguagePicker(value, Selector: "setText:", Picker: picker)
    }
    public func setAttributedPicker(_ picker: WBLanguageDictionaryPicker?) {
        setLanguagePicker(value, Selector: "setAttributedText:", Picker: picker)
    }
    public var picker: WBLanguageTextPicker? {
        set{ setLanguagePicker(value, Selector: "setText:", Picker: newValue) }
        get{ return getLanguagePicker(value, Selector: "setText:") as? WBLanguageTextPicker }
    }
    public var attributedPicker: WBLanguageDictionaryPicker? {
        set{ setLanguagePicker(value, Selector: "setAttributedText:", Picker: newValue) }
        get{ return getLanguagePicker(value, Selector: "setAttributedText:") as? WBLanguageDictionaryPicker }
    }
}

UIKit

UILabel

let label = UILabel()
label.lt.picker = "Label"

UILabel can use *.lt.picker or *.lt.setPicker(_:) to load text.

Of course, if you want to add attributes to the font, you can use WBLanguageDictionaryPicker or WBLanguageManager.textForKey("key")

to take out the text first and then add attributes.

use WBLanguageDictionaryPicker:

label.lt.attributedPicker = WBLanguageDictionaryPicker(dicts: ["picker": "Label", NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17)])

another:

if let text = WBLanguageManager.textForKey("Label") {
    let att = NSAttributedString(string: text, attributes: [.font: UIFont.boldSystemFont(ofSize: 17), .foregroundColor: UIColor.black])
    label.attributedText = att
}

UITextField

The use of the UITextField and UILabel is same, just more placeholder:

let textfield = UITextField()
textfield.lt.placeHolderPicker = "Label"

attribute picker:

textfield.lt.attributedPlaceHolderPicker = WBLanguageDictionaryPicker(dicts: ["picker": "Label", NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17)])

UIButton

let button = UIButton(type: .system)
button.lt.setPicker("Button", forState: .normal)

attribute text:

let picker = WBLanguageDictionaryPicker(dicts: ["picker": "Button", NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17), NSAttributedStringKey.strikethroughStyle:NSUnderlineStyle.styleDouble.rawValue])
button.lt.setAttributedPicker(picker, forState: .normal)

UISegmentedControl

let segment = UISegmentedControl(items: ["","","","",""])
segment.lt.setPicker("English", forSegmentAt: 0)
segment.lt.setPicker("Russian", forSegmentAt: 1)
segment.lt.setPicker("French", forSegmentAt: 2)
segment.lt.setPicker("Italian", forSegmentAt: 3)
segment.lt.setPicker("German", forSegmentAt: 4)

UISegmentedControl just has one method to set text picker.

License

WBLanguage is available under the MIT License. See the LICENSE file for more info.

Latest podspec

{
    "name": "WBLanguage",
    "version": "1.1.01",
    "license": "MIT",
    "summary": "In your project, you can easy to change the language with swift.",
    "homepage": "https://github.com/JsonBin/WBLanguage",
    "authors": {
        "JsonBin": "[email protected]"
    },
    "source": {
        "git": "https://github.com/JsonBin/WBLanguage.git",
        "tag": "1.1.01"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Source/*.swift",
    "requires_arc": true
}

Pin It on Pinterest

Share This