vChewing-macOS/Source/OpenCCBridge.swift

30 lines
1.0 KiB
Swift

import Foundation
import OpenCC
// Since SwiftyOpenCC only provide Swift classes, we create an NSObject subclass
// in Swift in order to bridge the Swift classes into our Objective-C++ project.
class OpenCCBridge : NSObject {
private static let shared = OpenCCBridge()
private var conveter: ChineseConverter?
override init() {
let mainBundle = Bundle.main
let dictionaryBundleUrl = mainBundle.bundleURL
.appendingPathComponent("Contents", isDirectory: true)
.appendingPathComponent("Plugins", isDirectory: true)
.appendingPathComponent("OpenCCDictionary.bundle")
if let dictionaryBundle = Bundle(url:dictionaryBundleUrl) {
try? conveter = ChineseConverter(bundle: dictionaryBundle, option: .simplify)
}
super.init()
}
@objc static func convert(_ string:String) -> String? {
return shared.conveter?.convert(string)
}
private func convert(_ string:String) -> String? {
return conveter?.convert(string)
}
}