iosswiftlocalizationturkish

How to uppercase Turkish "i"?


In Turkish, there are two i

PROBLEM: Every time I uppercase an i, I get an I.

I want to get an İ (only in Turkish) when I uppercase an i, and an I when I uppercase an ı.

I have a function to do it

@objc public static func uppercaseTurkishString(_ string: String) -> String {
    return String(string.map { (char) -> Character in
        if char == "i" {
            return "İ"
        } else {
            return Character(String(char).uppercased())
        }
    })
}

But I have to check if the language is Turkish every time I use it, and doing it for every single string in the app is a very hard job.

Is there an easier way to do it?


Solution

  • The problem lies in the fact that the uppercased() function doesn't care about Locales and the small dotted i looks like the normal English i. You should use localizedUppercase, which will use the Turkish Locale for Turkish users (or uppercased(with: Locale(identifier: "tr_TR"), in case you want this to be done for all users of your app regardless of their locale settings, but I wouldn't recommend that).

    Moreover, there's no need to do the upper casing character-by-character, you can simply do it on the full String.

    @objc public static func uppercaseTurkishString(_ string: String) -> String {
        return string.localizedUppercase
    }