96

我正在尝试使用NSLocalizedString. 当我导入 XLIFF 文件时,大多数工作都像一个魅力,但有些东西没有,有些字符串没有本地化。我注意到问题出在NSLocalizedString其中包含一些变量,例如:

NSLocalizedString(" - \(count) Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare")

或者

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

也许这不是这类东西的正确语法。有人可以解释我如何在 Swift 中做到这一点吗?

4

7 回答 7

154

您可以在 中使用sprintf格式参数NSLocalizedString,因此您的示例如下所示:

let myString = String(format: NSLocalizedString(" - %d Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare"), count)
于 2014-10-09T12:00:50.093 回答
104

在 WWDC2014“使用 Xcode 6 本地化”的 Session #412 中,Swift 中的正确方法如下:

String.localizedStringWithFormat(
    NSLocalizedString(" - %d Notifica",
    comment: "sottotitolo prescrizione per le notifiche al singolare"),
    count)
于 2015-06-28T21:29:21.173 回答
26

我遵循了创建字符串扩展的方法,因为我有很多字符串要本地化。

extension String {
    var localized: String {
        return NSLocalizedString(self, comment:"")
    }
}

要将其用于代码中的本地化,请执行以下操作:

self.descriptionView.text = "Description".localized

对于具有动态变量的字符串,请遵循:

self.entryTimeLabel.text = "\("Doors-open-at".localized) \(event.eventStartTime)"

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

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

希望会有所帮助!

于 2017-06-28T06:57:20.587 回答
13

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

斯威夫特 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? \"%@\"";
于 2019-06-04T14:23:44.997 回答
12

这是我在 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")
     }
}

用法:

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)

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

于 2018-10-12T19:28:52.110 回答
1

我为UILabel

extension UILabel {
    
    func setLocalizedText(key: String) {
        self.text = key.localized
    }
    
    func setLocalizedText(key: String, arguments: [CVarArg]) {
        self.text = String(format: key.localized, arguments: arguments)
    }
}

如果您愿意,也可以将此localized属性移至 UILabel

extension String {
        
    var localized: String{
        return Bundle.main.localizedString(forKey: self, value: nil, table: nil)
    }
}

我的可本地化

"hi_n" = "Hi, %@!";

像这样使用它们:

self.greetingLabel.setLocalizedText(key: "hi_n", arguments: [self.viewModel.account!.firstName])
// Shows this on the screen -> Hi, StackOverflow!
于 2021-02-27T10:21:45.720 回答
-5

我创建了一个extensiontoString因为我有很多stringsto be localized

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

例如:

let myValue = 10
let anotherValue = "another value"

let localizedStr = "This string is localized: \(myValue) \(anotherValue)".localized
print(localizedStr)
于 2016-12-20T16:50:44.487 回答