Skip to content

Instantly share code, notes, and snippets.

@zhaozzq
Created May 8, 2021 09:22
Show Gist options
  • Save zhaozzq/2e851ed3b1686530e634634f6ada63d3 to your computer and use it in GitHub Desktop.
Save zhaozzq/2e851ed3b1686530e634634f6ada63d3 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