ByvLocalizations
示例
要运行示例项目,请克隆仓库,然后在 Example 目录中首先运行 pod install
要求
安装
ByvLocalizations 通过 CocoaPods 提供。要安装它,请简单地在该 Podfile 中添加以下行:
pod "ByvLocalizations"
使用
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NotificationCenter.default.addObserver(
self,
selector: #selector(self.reloadLabels),
name: ByvLocalizator.notiName,
object: nil)
// This label will reload automatically when language change
autoLabel.locText("Localized_key")
// suport format strings with arguments to update localized text correctly
format.locText("%.2f POINTS", args: [22.586], comment: "%.2f is points value")
reloadLabels()
}
@IBAction func change(_ sender: UIButton) {
let av = UIAlertController(title: "Language".localize(comment: "Action sheet title"), message: "Select the language you want".localize(comment: "Action sheet description"), preferredStyle: .actionSheet)
for language in ByvLocalizator.shared.availableLanguages {
av.addAction(UIAlertAction(title: language.name(), style: .default, handler: { (action) in
ByvLocalizator.shared.setLanguage(code: language.code)
}))
}
av.addAction(UIAlertAction(title: "Cancel".localize(), style: .cancel, handler: nil))
self.present(av, animated: true, completion: nil)
}
func reloadLabels() {
label.text = "textToTranslate".localize()
languageName.text = ByvLocalizator.shared.currentLanguage.name()
}
扩展
UI扩展,使本地化更容易。它们会在语言更改时自动更新
UILabel => label.locText("localized_text")
UITextView => textView.locText("localized_text")
UIButton => button.locTitle("localized_text")
UITabBarItem => self.navigationController?.tabBarItem.locTitle("localized_text")
UINavigationItem => sself.navigationItem.locTitle("localized_text")
UIBarButtonItem => barButton.locTitle("localized_text")
UITextField => textField.locPlaceholder("localized_text")
UISearchBar => searchBar.locPlaceholder("localized_text")
UISegmentedControl => segmented.locTitle("first", at: 0)
segmented.locTitle("second", at: 1)
生成器
此生成器 ByvLocalizableStringsGenerator 可帮助创建和更新 Localizable.strings
界面构建器扩展
如果您想从界面构建器直接进行本地化,可以使用 ByvLocalizationsIB
pod "ByvLocalizationsIB"
作者
Adrian Apodaca,[email protected]
许可证
ByvLocalizations 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。