iosswiftnslocale

Uppercase All String According to Locale - Swift


I'm trying to uppercase all string according to 'Locale' but it is not working.

var text = "istanbul, izmir"

println(text.uppercaseStringWithLocale(NSLocale.currentLocale())) 

In Turkish language the uppercase for i is İ not I. My Result is ISTANBUL, IZMIR but It should returns İSTANBUL, İZMİR.

Please where would be my issue?


Solution

  • NSLocale.currentLocale() is the locale which was selected in the Settings of the device. If that is "en_US" then

    text.uppercaseStringWithLocale(NSLocale.currentLocale())
    

    will use the english language rules and the result is "ISTANBUL, IZMIR".

    You can either select the turkish language in the device settings, or specify the turkish locale explicitly:

    let text = "istanbul, izmir"
    text.uppercaseStringWithLocale(NSLocale(localeIdentifier: "tr"))
    // İSTANBUL, İZMİR
    

    Swift 3:

    text.uppercased(with: Locale(identifier: "tr"))