23

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/wiki/Quotation_mark) based on the user's language settings. I'd like to show the string in a UILabel after adding the quotes.

For instance:

var quote: String!
quote = "To be or not to be..."
// one or more lines of code that add localized quotation marks 

For a French user: «To be or not to be...»

For a German user: „To be or not to be...”

3 Answers3

34

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

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
11

Swift 4

Using the same logic, but with a modern and simple syntax.

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
    }
}

Then you can use it simply like this:

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

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

Also, I advice you to read the whole Apple's Internationalization and Localization Guide

Community
  • 1
  • 1
Francescu
  • 16,974
  • 6
  • 49
  • 60
0
let quote = "\"To be or not to be...\""
println(quote)

output Will be: "To be or not to be..."

vijeesh
  • 1,317
  • 1
  • 17
  • 32
  • Thank you. Yes, that seems straightforward but it does not localize. –  Jun 30 '15 at 11:28
  • Please see the Wikipedia link provided in the question showing quotation mark styles in different languages. –  Jun 30 '15 at 11:33
  • 2
    That's simple quotes. Real texts use different [smart quotes](https://en.wiktionary.org/wiki/smart_quotes) for different languages. For example in Chinese/Japanese 「文字 To be or not to be...」 – phuclv Jun 30 '15 at 11:42
  • @LưuVĩnhPhúc: Good point ("engineer's quotes" are not quite correct even for English locales). Martin's answer addresses this. –  Jun 30 '15 at 13:21
  • 2
    Why do people continue upvoting this while it doesn't answer the question? The locale requirement was already stated – phuclv Jul 01 '15 at 10:29