Adding Quotes in Swift

后端 未结 3 600
情深已故
情深已故 2021-01-07 19:02

Is there a straightforward way in Swift for adding quotation marks to a String? The quotation marks should localize properly (see https://en.wikipedia.org/w

3条回答
  •  攒了一身酷
    2021-01-07 19:37

    Using the information from http://nshipster.com/nslocale/:

    let locale = NSLocale.currentLocale()
    let qBegin = locale.objectForKey(NSLocaleQuotationBeginDelimiterKey) as? String ?? "\""
    let qEnd = locale.objectForKey(NSLocaleQuotationEndDelimiterKey) as? String ?? "\""
    
    let quote = qBegin + "To be or not to be..." + qEnd
    print(quote)
    

    Sample results:

    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...」
    

    I don't know if the begin/end delimiter key can be undefined for a locale. In that case the above code would fall back to the normal double-quote ".

提交回复
热议问题