Home > Blockchain >  How to get localized alphabet Swift iOS [duplicate]
How to get localized alphabet Swift iOS [duplicate]

Time:10-09

I’m translating my app to different languages, how could I get the alphabet based on the localization? Could be Latin, Cyrillic, etc.

CodePudding user response:

You have to provide Localizable.strings base file to you project, which consist of your different text for different origins:

Like for default language:

"Hello World!" = "Hello World!";

and for like in Latin language:

"Hello World!" = "salve mundi!";

CodePudding user response:

Try this:

import UIKit // Or Foundation


if let alphabetCharacterSet = Locale(identifier: "ru").exemplarCharacterSet?.intersection(CharacterSet.lowercaseLetters) {
    print(alphabetCharacterSet.characters())
}

extension CharacterSet {
    func characters() -> [Character] {
        // A Unicode scalar is any Unicode code point in the range U 0000 to U D7FF inclusive or U E000 to U 10FFFF inclusive.
        return codePoints().compactMap { UnicodeScalar($0) }.map { Character($0) }
    }

    func codePoints() -> [Int] {
        var result: [Int] = []
        var plane = 0
        // following documentation at https://developer.apple.com/documentation/foundation/nscharacterset/1417719-bitmaprepresentation
        for (i, w) in bitmapRepresentation.enumerated() {
            let k = i % 0x2001
            if k == 0x2000 {
                // plane index byte
                plane = Int(w) << 13
                continue
            }
            let base = (plane   k) << 3
            for j in 0 ..< 8 where w & 1 << j != 0 {
                result.append(base   j)
            }
        }
        return result
    }
}
  • Related