|
| 1 | +WBLanguage |
| 2 | +========== |
| 3 | + |
| 4 | +[](https://travis-ci.org/JsonBin/WBLanguage) |
| 5 | + |
| 6 | +[](https://github.com/Carthage/Carthage) |
| 7 | +[](http://cocoadocs.org/docsets/WBLanguage) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## What |
| 12 | + |
| 13 | +WBLanguage is a high level LanguageKit. It provides a High Level API set international language for your app. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +* Less time to switch the language |
| 18 | +* Set the language more convenient |
| 19 | +* Supported languages: English/French/Italian/German/Russian/Chinese-Hans/Chinese-Hant |
| 20 | +* Subsequent can add more national languages |
| 21 | + |
| 22 | +## Installation |
| 23 | +WBLanguage supports multiple methods for installing the library in a project. |
| 24 | + |
| 25 | +## CocoaPods |
| 26 | + |
| 27 | +[CocoaPods](http://cocoapods.org) 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: |
| 28 | + |
| 29 | +```bash |
| 30 | +$ gem install cocoapods |
| 31 | +``` |
| 32 | + |
| 33 | +> CocoaPods 1.2.0+ is required to build WBLanguage. |
| 34 | +
|
| 35 | +#### Podfile |
| 36 | + |
| 37 | +To integrate WBLanguage into your Xcode project using CocoaPods, specify it in your `Podfile`: |
| 38 | + |
| 39 | +```ruby |
| 40 | +source 'https://github.com/CocoaPods/Specs.git' |
| 41 | +platform :ios, '8.0' |
| 42 | + |
| 43 | +target 'TargetName' do |
| 44 | +use_frameworks! |
| 45 | + |
| 46 | +pod 'WBLanguage' |
| 47 | + |
| 48 | +end |
| 49 | +``` |
| 50 | + |
| 51 | +Then, run the following command: |
| 52 | + |
| 53 | +```bash |
| 54 | +$ pod install |
| 55 | +``` |
| 56 | + |
| 57 | +## Carthage |
| 58 | + |
| 59 | + |
| 60 | +[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. |
| 61 | + |
| 62 | +You can install Carthage with [Homebrew](http://brew.sh/) using the following command: |
| 63 | + |
| 64 | +```bash |
| 65 | +$ brew update |
| 66 | +$ brew install carthage |
| 67 | +``` |
| 68 | + |
| 69 | +To integrate WBLanguage into your Xcode project using Carthage, specify it in your `Cartfile`: |
| 70 | + |
| 71 | +```ogdl |
| 72 | +github "JsonBin/WBLanguage" |
| 73 | +``` |
| 74 | + |
| 75 | +Run `carthage` to build the framework and drag the built `WBLanguage.framework` into your Xcode project. |
| 76 | + |
| 77 | +## Requirements |
| 78 | + |
| 79 | +- iOS 8+ |
| 80 | +- Xcode 9.0+ |
| 81 | +- Swift 4.0+ |
| 82 | + |
| 83 | +| WBLanguage Version | Minimum iOS Target | Note | |
| 84 | +|:------------------:|:-------------------:|:-----| |
| 85 | +| 1.x | iOS 8 | Xcode 9+ is required. | |
| 86 | + |
| 87 | +you must create a 'Language.bundle' resources in your project to hold the international language. |
| 88 | + |
| 89 | +## Usage |
| 90 | + |
| 91 | +### WBLanguageManager class |
| 92 | + |
| 93 | +We can set WBLanguageManager's property at the beggining of app launching, the sample is below: |
| 94 | + |
| 95 | +```swift |
| 96 | +func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { |
| 97 | + WBLanguageManager.duration = 2.0 |
| 98 | + return true |
| 99 | + } |
| 100 | +``` |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +```swift |
| 105 | +WBLanguageManager.setLanguage(type) |
| 106 | +``` |
| 107 | + |
| 108 | +you can use `textForKey(_:, value:)` to get the local text from *.lproj. |
| 109 | + |
| 110 | +```swift |
| 111 | +let string = WBLanguageManager.textForKey("key") |
| 112 | +``` |
| 113 | + |
| 114 | +### WBLanguage class |
| 115 | + |
| 116 | +WBLanguage supports serializing and deserializing any custom type as long as it conforms to the `WBLanguageProtocol` protocol. |
| 117 | + |
| 118 | +```swift |
| 119 | +public extension WBLanguageProtocol { |
| 120 | + public var lt: WBLanguage<Self> { |
| 121 | + return WBLanguage(self) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +extension `yourCustom` : WBLanguageProtocol {} |
| 126 | +``` |
| 127 | +Note: `WBLanguageProtocol` is a protocol that WBLanguage uses internally to directly. So you need to complete the language setting. |
| 128 | + |
| 129 | +For example: |
| 130 | + |
| 131 | +```swift |
| 132 | + |
| 133 | +private func setLanguagePicker( |
| 134 | + _ object: NSObject, |
| 135 | + Selector selector: String, |
| 136 | + Picker picker: WBLanguagePicker? |
| 137 | +) { |
| 138 | + object.languagePickers[selector] = picker |
| 139 | + object.performLanguage(selector, Picker: picker) |
| 140 | +} |
| 141 | + |
| 142 | +public extension WBLanguage where T : UILabel { |
| 143 | + public func setPicker(_ picker: WBLanguageTextPicker?) { |
| 144 | + setLanguagePicker(value, Selector: "setText:", Picker: picker) |
| 145 | + } |
| 146 | + public func setAttributedPicker(_ picker: WBLanguageDictionaryPicker?) { |
| 147 | + setLanguagePicker(value, Selector: "setAttributedText:", Picker: picker) |
| 148 | + } |
| 149 | + public var picker: WBLanguageTextPicker? { |
| 150 | + set{ setLanguagePicker(value, Selector: "setText:", Picker: newValue) } |
| 151 | + get{ return getLanguagePicker(value, Selector: "setText:") as? WBLanguageTextPicker } |
| 152 | + } |
| 153 | + public var attributedPicker: WBLanguageDictionaryPicker? { |
| 154 | + set{ setLanguagePicker(value, Selector: "setAttributedText:", Picker: newValue) } |
| 155 | + get{ return getLanguagePicker(value, Selector: "setAttributedText:") as? WBLanguageDictionaryPicker } |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## UIKit |
| 161 | + |
| 162 | +### UILabel |
| 163 | + |
| 164 | +```swift |
| 165 | +let label = UILabel() |
| 166 | +label.lt.picker = "Label" |
| 167 | +``` |
| 168 | +`UILabel` can use `*.lt.picker` or `*.lt.setPicker(_:)` to load text. |
| 169 | + |
| 170 | +Of course, if you want to add attributes to the font, you can use `WBLanguageDictionaryPicker` or `WBLanguageManager.textForKey("key")` |
| 171 | + |
| 172 | +to take out the text first and then add attributes. |
| 173 | + |
| 174 | +use `WBLanguageDictionaryPicker`: |
| 175 | + |
| 176 | +```swift |
| 177 | +label.lt.attributedPicker = WBLanguageDictionaryPicker(dicts: ["picker": "Label", NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17)]) |
| 178 | +``` |
| 179 | +another: |
| 180 | + |
| 181 | +```swift |
| 182 | +if let text = WBLanguageManager.textForKey("Label") { |
| 183 | + let att = NSAttributedString(string: text, attributes: [.font: UIFont.boldSystemFont(ofSize: 17), .foregroundColor: UIColor.black]) |
| 184 | + label.attributedText = att |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +### UITextField |
| 189 | + |
| 190 | +The use of the `UITextField` and `UILabel` is same, just more placeholder: |
| 191 | + |
| 192 | +```swift |
| 193 | +let textfield = UITextField() |
| 194 | +textfield.lt.placeHolderPicker = "Label" |
| 195 | +``` |
| 196 | + |
| 197 | +attribute picker: |
| 198 | + |
| 199 | +```swift |
| 200 | +textfield.lt.attributedPlaceHolderPicker = WBLanguageDictionaryPicker(dicts: ["picker": "Label", NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17)]) |
| 201 | +``` |
| 202 | + |
| 203 | +### UIButton |
| 204 | + |
| 205 | +```swift |
| 206 | +let button = UIButton(type: .system) |
| 207 | +button.lt.setPicker("Button", forState: .normal) |
| 208 | +``` |
| 209 | + |
| 210 | +attribute text: |
| 211 | + |
| 212 | +```swift |
| 213 | +let picker = WBLanguageDictionaryPicker(dicts: ["picker": "Button", NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17), NSAttributedStringKey.strikethroughStyle:NSUnderlineStyle.styleDouble.rawValue]) |
| 214 | +button.lt.setAttributedPicker(picker, forState: .normal) |
| 215 | +``` |
| 216 | + |
| 217 | +### UISegmentedControl |
| 218 | + |
| 219 | +```swift |
| 220 | +let segment = UISegmentedControl(items: ["","","","",""]) |
| 221 | +segment.lt.setPicker("English", forSegmentAt: 0) |
| 222 | +segment.lt.setPicker("Russian", forSegmentAt: 1) |
| 223 | +segment.lt.setPicker("French", forSegmentAt: 2) |
| 224 | +segment.lt.setPicker("Italian", forSegmentAt: 3) |
| 225 | +segment.lt.setPicker("German", forSegmentAt: 4) |
| 226 | +``` |
| 227 | +`UISegmentedControl` just has one method to set text picker. |
| 228 | + |
| 229 | +## License |
| 230 | + |
| 231 | +WBLanguage is available under the [MIT License](https://raw.github.com/rs/SDWebImage/master/LICENSE). See the LICENSE file for more info. |
0 commit comments