我想用快速语言实现故事板本地化。 (意味着我想要修复标签和按钮文本的本地化)
我已经了解了NSLocalizedString,但我不想编写修复文本标签
例如
NSLocalizedString("Welcome", comment: "")
我已经添加了Localizable.strings文件以及特定语言的Main.string文件。但我无法成功实现本地化
Bhumesh
我使用这个库进行应用内本地化。这很容易使用。
https://github.com/marmelroy/Localize-Swift
现在,对于Storyboard支持,我创建了以下IBDesignable扩展,因此您可以轻松地从故事板本身提供本地化文本
1)将其添加到新的swift文件中
import Localize_Swift
@IBDesignable class LocalizableLabel: UILabel {
@IBInspectable var table :String? // Table
@IBInspectable var key:String? // KEY
@IBInspectable var extraTextToAppend:String? // Some text need to append , if any
override func awakeFromNib() {
guard let key = key else {return}
self.text = key.localized(using: table)
NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
if let extraText = self.extraTextToAppend, let text = self.text {
self.text = text + extraText
}
}
@objc func setText () {
guard let key = key else {return}
self.text = key.localized(using: table)
if let extraText = self.extraTextToAppend, let text = self.text {
self.text = text + extraText
}
}
}
@IBDesignable class LocalizableButton: UIButton {
@IBInspectable var table :String?
@IBInspectable var key:String?
override func awakeFromNib() {
guard let key = key else {return}
self.setTitle(key.localized(using: table), for: .normal)
if let attributedText = self.attributedTitle(for: .normal) {
let mutableAttributedText = NSMutableAttributedString(attributedString: attributedText)
mutableAttributedText.replaceCharacters(in: NSMakeRange(0, mutableAttributedText.length), with: key.localized(using: table))
self.setAttributedTitle(mutableAttributedText, for: .normal)
}
NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
}
@objc func setText () {
guard let key = key else {return}
self.setTitle(key.localized(using: table), for: .normal)
if let attributedText = self.attributedTitle(for: .normal) {
let mutableAttributedText = NSMutableAttributedString(attributedString: attributedText)
mutableAttributedText.replaceCharacters(in: NSMakeRange(0, mutableAttributedText.length), with: key.localized(using: table))
self.setAttributedTitle(mutableAttributedText, for: .normal)
}
}
}
@IBDesignable class LocalizeUINavigationItem: UINavigationItem {
@IBInspectable var table :String?
@IBInspectable var key:String?
override func awakeFromNib() {
guard let key = key else {return}
self.title = key.localized(using: table)
NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
}
@objc func setText () {
guard let key = key else {return}
self.title = key.localized(using: table)
}
}
@IBDesignable class LocalizableUITextField: UITextField {
@IBInspectable var table_placeholder :String?
@IBInspectable var key_place_holder:String?
override func awakeFromNib() {
guard let key = key_place_holder else {return}
self.placeholder = key.localized(using: table_placeholder)
NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)
}
@objc func setText () {
guard let key = key_place_holder else {return}
self.placeholder = key.localized(using: table_placeholder)
}
}
2)Goto Storyboard将类设置为标签并提供密钥
3)运行并测试
一旦您的本地化运行,您可以添加UI元素的扩展,为它们引入简单的本地化。
对于UIlabel
,它看起来像这样:
public extension UILabel {
@IBInspectable public var localizedText: String? {
get {
return text
}
set {
text = NSLocalizedString(newValue ?? "", comment: "")
}
}
}
@IBInspectable
允许您从故事板和编程方式设置本地化密钥。
故事板本地化是苹果公司提供的方式,但它让我很烦恼 - 当然不是程序员最友好的。
class ViewController: UIViewController {
@IBOutlet weak var resetOutlet: MyButton! {
didSet {
resetOutlet.setTitle("RESET".localized().uppercased(), for: .normal)
}
}
}
extension String {
func localized(tableName: String = "Localizable") -> String {
if let languageCode = Locale.current.languageCode, let preferredLanguagesFirst = Locale.preferredLanguages.first?.prefix(2) {
if languageCode != preferredLanguagesFirst {
if let path = Bundle.main.path(forResource: "en", ofType: "lproj") {
let bundle = Bundle.init(path: path)
return NSLocalizedString(self, tableName: tableName, bundle: bundle!, value: self, comment: "")
}
}
}
return NSLocalizedString(self, tableName: tableName, value: self, comment: "")
}
}