NSLocalizedString with swift variable

And*_*ath 74 localization nslocalizedstring ios xliff swift

I'm trying to localize my app using NSLocalizedString. When i import the XLIFF file, most works like a charm but something do not and some string is not localized. I have noticed that the problem is from NSLocalizedString containing something variable inside like:

NSLocalizedString(" - \(count) Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare")
Run Code Online (Sandbox Code Playgroud)

or

NSLocalizedString("Notifica per \(medicina!) della prescrizione \(prescription!)\nMemo: \(memoTextView.text)", comment: "Messaggio della Local Notification")
Run Code Online (Sandbox Code Playgroud)

Maybe this is not the correct syntax for thi kind of stuff. Some one can explain me how to do that in swift? Thank you very much.

zis*_*oft 113

You can use the sprintf format parameters within NSLocalizedString, so your example can look like this:

let myString = String(format: NSLocalizedString(" - %d Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare"), count)
Run Code Online (Sandbox Code Playgroud)

  • @igrek使用`%@`替换字符串. (9认同)
  • 它在Localizable.string中是什么样子的? (5认同)
  • 这些格式参数的参考是 https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html (3认同)
  • 我如何替换字符串?我试图使用`%s`修饰符,它不起作用= \ (2认同)

Mar*_*ark 90

在WWDC2014"使用Xcode 6进行本地化"的会话#412中,Swift的正确方法如下:

String.localizedStringWithFormat(
    NSLocalizedString(" - %d Notifica",
    comment: "sottotitolo prescrizione per le notifiche al singolare"),
    count)
Run Code Online (Sandbox Code Playgroud)

  • 为什么需要使用 NSLocalizedString(...)?它不会发生在 String.localizedStringWithFormat(...) 的实现中吗? (3认同)
  • 查看(http://stackoverflow.com/questions/26237549/how-does-localizedstringwithformat-work) (3认同)
  • 谢谢 我问了之后已经看到了。这对其他人非常有帮助,所以谢谢 (2认同)

Jas*_*our 19

我已经按照创建String扩展的方法,因为我有很多字符串要本地化.

extension String {
    var localized: String {
        return NSLocalizedString(self, comment:"")
    }
}
Run Code Online (Sandbox Code Playgroud)

要在代码中使用它进行本地化:

self.descriptionView.text = "Description".localized
Run Code Online (Sandbox Code Playgroud)

对于具有动态变量的字符串如下

self.entryTimeLabel.text = "\("Doors-open-at".localized) \(event.eventStartTime)"
Run Code Online (Sandbox Code Playgroud)

在String文件中声明不同语言的字符串(例如:阿拉伯语和英语)

在此输入图像描述 在此输入图像描述

希望会有所帮助!

  • 但是在我看来,您只是在花时间在弦上。如果您的本地化字符串就像“门在%@点打开”怎么办。您的解决方案仍然有效吗? (2认同)
  • 感谢您包含对“Localizable.strings”的引用。但是,我认为@Houman 有一个合理的担忧。 (2认同)

the*_*end 11

这是我在 String 中使用的扩展,它添加了一个带有可变参数的 localizeWithFormat 函数,

extension String:{

     func localizeWithFormat(arguments: CVarArg...) -> String{
        return String(format: self.localized, arguments: arguments)        
     }

     var localized: String{
         return Bundle.main.localizedString(forKey: self, value: nil, table: "StandardLocalizations")
     }
}
Run Code Online (Sandbox Code Playgroud)

用法:

let siriCalendarText = "AnyCalendar"
let localizedText = "LTo use Siri with my app, please set %@ as the default list on your device reminders settings".localizeWithFormat(arguments: siriCalendarTitle)
Run Code Online (Sandbox Code Playgroud)

请注意不要使用与 String 相同的函数和属性名称。我通常为所有框架函数使用 3 个字母的前缀。


Pra*_*tik 11

我尝试了上述解决方案,但下面的代码对我有用

快速 4

extension String {

    /// Fetches a localized String
    ///
    /// - Returns: return value(String) for key
    public func localized() -> String {
        let path = Bundle.main.path(forResource: "en", ofType: "lproj")
        let bundle = Bundle(path: path!)
        return (bundle?.localizedString(forKey: self, value: nil, table: nil))!
    }


    /// Fetches a localised String Arguments
    ///
    /// - Parameter arguments: parameters to be added in a string
    /// - Returns: localized string
    public func localized(with arguments: [CVarArg]) -> String {
        return String(format: self.localized(), locale: nil, arguments: arguments)
    }

}

// variable in a class
 let tcAndPPMessage = "By_signing_up_or_logging_in,_you_agree_to_our"
                                     .localized(with: [tAndc, pp, signin])

// Localization File String
"By_signing_up_or_logging_in,_you_agree_to_our" = "By signing up or logging in, you agree to our \"%@\" and \"%@\" \nAlready have an Account? \"%@\"";
Run Code Online (Sandbox Code Playgroud)