Skip to content

Instantly share code, notes, and snippets.

@rijieli
Created May 20, 2022 03:09
Show Gist options
  • Save rijieli/01215f0d70576bc74e30060be5ef4d92 to your computer and use it in GitHub Desktop.
Save rijieli/01215f0d70576bc74e30060be5ef4d92 to your computer and use it in GitHub Desktop.
Transliterate Chinese between simplified and traditional (the wrong way).
import UIKit
struct ChineseTransliterator {
static func transliterate(text: String, sourceView: UIView) {
let textView = UITextView()
textView.isHidden = true
textView.text = text
textView.selectAll(nil)
sourceView.addSubview(textView)
DispatchQueue.main.asyncAfter(deadline: .now()) {
textView.perform(NSSelectorFromString("_transliterateChinese:"), with: self)
print(textView.text ?? "")
textView.removeFromSuperview()
}
}
}
// Above was a joke, use the following
let input = "简体中文内容"
let output = input.applyingTransform(
StringTransform(rawValue: "Simplified-Traditional"),
reverse: false
) ?? input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment