Localizable.strings不翻译短语

时间:2018-08-06 19:26:54

标签: ios swift localization nsattributedstring

出于某些奇怪的原因,Localizable.strings中的部分文本只能正确翻译。如果字符串包含的第一个单词更多(英语一侧),则不会翻译。我确保它是相同的字符串(复制和粘贴)

"Login" = "כניסה"; //translates properly 
"Email" = "דואר אלקטרוני"; //translates properly 
"Password" = "סיסמא"; //translates properly 
"forgot password?" = "שכחת סיסמא?"; //translation does not work
"Sign up" = "הירשם"; //translation does not work

我使用此扩展名:

extension String {
    var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
    }
}

和代码中的

override func viewDidLoad() {
    super.viewDidLoad()
    self.emailTextField.placeholder = "Email".localized
    self.passwordTextField.placeholder = "Password".localized
    self.loginButton.setTitle("Login".localized, for: .normal)
    self.signupButton.setTitle("Sign up".localized, for: .normal)
    self.forgetPasswordButton.setTitle("forgot password?", for: .normal)
}

enter image description here

1 个答案:

答案 0 :(得分:1)

基于“注册”的外观带有下划线,看来您有一个NSAttributedString,然后您在后续评论中对其进行了确认。

虽然此处的代码段“ 忘记密码?”不包括您在localize的其他地方使用的viewDidLoad呼叫,但事实证明您也已识别出此身份作为属性字符串。

self.forgetPasswordButton.setTitle("forgot password?", for: .normal)

正如您在评论中指出的那样,此问题已通过将本地化应用于属性字符串而得到纠正:

let attributeString = NSMutableAttributedString(string: "Sign up".localized, 
                                                attributes: yourAttributes) 
self.signupButton.setAttributedTitle(attributeString, for: .normal)