I’m translating my app to different languages, how could I get the alphabet based on the localization? Could be Latin, Cyrillic, etc.
Asked
Active
Viewed 451 times
1
-
Does this your answere? https://stackoverflow.com/a/3161573/14733292 – Raja Kishan Oct 08 '21 at 11:47
-
I believe this is a duplicate, but please provide additional information if you believe it isn't covered by that answer. In particular, how is this related to translation? What should this return for non-alphabetic languages? What should this return for English (are "a" and "A" different letters)? What should this return for Arabic (is "ء" a letter)? Could you give a few examples of what you mean? – Rob Napier Oct 08 '21 at 19:05
2 Answers
3
Try this:
import UIKit // Or Foundation
if let alphabetCharacterSet = Locale(identifier: "ru").exemplarCharacterSet?.intersection(CharacterSet.lowercaseLetters) {
print(alphabetCharacterSet.characters().sorted(by: {String($0).localizedCompare(String($1)) == .orderedAscending }))
}
// If you don't sort alphabetical order is not guaranteed.
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
}
}

Paul B
- 3,989
- 33
- 46
-
there is a problem in some languages like Thai it does not show anything and the list of the characters is empty – Saeed Rahmatolahi Apr 11 '22 at 04:43
-
As you can see, @SaeedRahmatolahi, the code relies on `Foundation`'s [capabilities](https://developer.apple.com/documentation/foundation/locale/2293427-exemplarcharacterset). You should address those issues to Apple, perhaps submit a Radar. Or maybe use some external library. – Paul B Apr 11 '22 at 08:42
-
@PaulB the problem is not exemplarCharacterSet as I saw the code point function will get some thing and if I change the code of that I can get some of the characters but I am not sure how to do it but that's possible definitely with some changing the function and the libraries that you mentioned may do the same thing as well – Saeed Rahmatolahi Apr 12 '22 at 03:31
-
@PaulB this one worked for me https://stackoverflow.com/a/71836841/7567902 – Saeed Rahmatolahi Apr 12 '22 at 03:47
-
1You were right, @SaeedRahmatolahi. The problem was because some languages don't have a concept of lowercase/uppercase letters. – Paul B Apr 12 '22 at 08:26
0
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!";

Sumit_VE
- 429
- 2
- 12
-
I hope this will help in resolving your query, in case of any issue feel free to connect over same. – Sumit_VE Oct 08 '21 at 15:46
-
Thank you, but I’ve already translate texts with a Localizable.strings. I wanted to programatically get alphabet based on localization because I have a view which display all the letter of the alphabet. – Maël Navarro Oct 09 '21 at 14:23