如何获得本地化的字母Swift iOS



我正在将我的应用翻译成不同的语言,我如何根据本地化获得字母表?可以是拉丁文、西里尔文等。

试试这个:

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

你必须提供Localizable。项目的字符串基础文件,它由不同来源的不同文本组成:

等默认语言:

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

和for like在拉丁语中:

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

最新更新