Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 在Swift中添加引号_Ios_Swift_Localization - Fatal编程技术网

Ios 在Swift中添加引号

Ios 在Swift中添加引号,ios,swift,localization,Ios,Swift,Localization,在Swift中是否有一种简单的方法可以将引号添加到字符串中?引号应根据用户的语言设置正确本地化(请参阅)。我想在添加引号后在UILabel中显示字符串 例如: var quote: String! quote = "To be or not to be..." // one or more lines of code that add localized quotation marks 对于法国用户:«生存还是毁灭…» 对于德国用户:„;是或不是…” 使用以下信息,输出将为:

在Swift中是否有一种简单的方法可以将引号添加到字符串中?引号应根据用户的语言设置正确本地化(请参阅)。我想在添加引号后在UILabel中显示字符串

例如:

var quote: String!
quote = "To be or not to be..."
// one or more lines of code that add localized quotation marks 
对于法国用户:«生存还是毁灭…»

对于德国用户:„;是或不是…”


使用以下信息,输出将为:“未来或不未来…”

样本结果:

Locale Output de „To be or not to be...“ en “To be or not to be...” fr «To be or not to be...» ja 「To be or not to be...」 区域设置输出 “生存还是毁灭…” “生存还是毁灭…” fr«生存还是毁灭…» “生存还是毁灭…” 我不知道是否可以为一个字符串取消定义开始/结束分隔符键 在这种情况下,上面的代码将恢复正常 双引号

Swift 4 使用相同的逻辑,但具有现代和简单的语法

extension String {
    static var quotes: (String, String) {
        guard
            let bQuote = Locale.current.quotationBeginDelimiter,
            let eQuote = Locale.current.quotationEndDelimiter
            else { return ("\"", "\"") }
        
        return (bQuote, eQuote)
    }
    
    var quoted: String {
        let (bQuote, eQuote) = String.quotes
        return bQuote + self + eQuote
    }
}
然后您可以像这样简单地使用它:

print("To be or not to be...".quoted)
结果

Locale Output de „To be or not to be...“ en “To be or not to be...” fr «To be or not to be...» ja 「To be or not to be...」 区域设置输出 “生存还是毁灭…” “生存还是毁灭…” fr«生存还是毁灭…» “生存还是毁灭…”
此外,我建议您阅读整个苹果的

谢谢。是的,这看起来很简单,但它没有本地化。请参阅问题中提供的维基百科链接,显示不同语言的引号样式。这是简单的引号。真实文本对不同语言使用不同的方式。例如,中文/日文文字 生存还是毁灭…“@LưuVĩnhPhúc:很好的观点(“工程师的引语”甚至对英语语言环境也不太正确)。马丁的回答解决了这个问题。为什么人们在没有回答问题的情况下继续向上投票?语言环境要求已经说明了
print("To be or not to be...".quoted)
Locale Output de „To be or not to be...“ en “To be or not to be...” fr «To be or not to be...» ja 「To be or not to be...」