Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSLocalizedString带有swift变量_Ios_Swift_Localization_Nslocalizedstring_Xliff - Fatal编程技术网

Ios NSLocalizedString带有swift变量

Ios NSLocalizedString带有swift变量,ios,swift,localization,nslocalizedstring,xliff,Ios,Swift,Localization,Nslocalizedstring,Xliff,我正在尝试使用NSLocalizedString对我的应用程序进行本地化。当我导入XLIFF文件时,大多数都像一个符咒一样工作,但有些东西没有,有些字符串没有本地化。我注意到问题来自NSLocalizedString,其中包含一些变量,如: NSLocalizedString(" - \(count) Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare") 或 也许这不是这类东西的正确语法。有人

我正在尝试使用NSLocalizedString对我的应用程序进行本地化。当我导入XLIFF文件时,大多数都像一个符咒一样工作,但有些东西没有,有些字符串没有本地化。我注意到问题来自NSLocalizedString,其中包含一些变量,如:

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


也许这不是这类东西的正确语法。有人能告诉我怎么用swift吗?非常感谢。

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

let myString = String(format: NSLocalizedString(" - %d Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare"), count)
self.greetingLabel.setLocalizedText(key: "hi_n", arguments: [self.viewModel.account!.firstName])
// Shows this on the screen -> Hi, StackOverflow!
在WWDC2014“使用Xcode 6本地化”的第412节中,Swift中实现这一点的正确方法如下:

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

我创建了一个
扩展
字符串
,因为我有许多
字符串
要进行
本地化

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)

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

extension String {
    var localized: String {
        return NSLocalizedString(self, comment:"")
    }
}
要在代码中使用它进行本地化,请执行以下操作:

self.descriptionView.text = "Description".localized
对于带有动态变量的字符串,请执行以下操作:

self.entryTimeLabel.text = "\("Doors-open-at".localized) \(event.eventStartTime)"
在不同语言的字符串文件中声明字符串(例如:阿拉伯语和英语)


希望会有帮助

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

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

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

SWIFT 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? \"%@\"";

我为
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)
    }
}
如果需要,也可以将此
本地化的
属性移动到UILabel

extension String {
        
    var localized: String{
        return Bundle.main.localizedString(forKey: self, value: nil, table: nil)
    }
}
我的可本地化

"hi_n" = "Hi, %@!";
他们是这样使用的:

let myString = String(format: NSLocalizedString(" - %d Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare"), count)
self.greetingLabel.setLocalizedText(key: "hi_n", arguments: [self.viewModel.account!.firstName])
// Shows this on the screen -> Hi, StackOverflow!

它在Localizable.string中的外观与在Obj-C中的相同:
“-%d Notifica”=“-%d Notifica”如何替换字符串?我尝试使用
%s
修饰符替换字符串,但修饰符无效=\@igrek使用
%@
替换字符串。这些格式参数的参考是为什么需要使用NSLocalizedString(…)?这不是发生在String.localizedStringWithFormat(…)的实现中吗?签出()谢谢,我问过之后已经看到了。这将对其他人非常有帮助,因此感谢这种方法的一大缺点是,您必须手动提取字符串以作为
Editor>Export进行本地化发送给翻译人员…
不会提取字符串。根据@JasonMoore的建议,我认为这不是正确的方法。@JasonMoore完全同意您的意见。你们中有人找到了解决方案吗?我不知道为什么这个解决方案会被降级:(.和几乎下嵌套下的解决方案是相同的,但它已升级,但在我看来,您只是将时间附加到字符串上。如果您的本地化字符串类似于
,那么门在%@o点打开会怎么样?
。您的解决方案仍然有效吗?是的,它工作得很好,因为我从后面获得了作为字符串的时间。感谢您包含一个参考
Localizable.strings
。然而,我认为@Houman有一个合理的担忧。我不知道为什么人们想退出“评论”值。它向本地化工程师提供了文本的描述及其用途,如果没有这些描述,他们就无法理解将文本用于按钮或标签等的意图。这不像Houman所说的那样好,但这是一种连接字符串的通用方法。这是一个关于在Swift中进行本地化的强大架构Swift如何知道是哪种要选择“setLocalizedText”,因为在字符串扩展名中有2个?