一个使用 Swift 调用 Google Cloud Translation API 的框架。:snowman::frog::penguin::whale::turtle
CocoaPods
pod 'SwiftGoogleTranslate'
Carthage
github "maximbilan/SwiftGoogleTranslate"
Swift Package Manager
dependencies: [ .package(url: "https://github.com/maximbilan/SwiftGoogleTranslate", from: "0.2.2")) ]
手动
Copy SwiftGoogleTranslate.swift to your project.
首先,你需要在 控制台 中生成 API 密钥才能使用 Google Cloud 服务。 然后使用以下代码
SwiftGoogleTranslate.shared.start(with: "API_KEY_HERE")
该框架支持 3 个端点:translate、detect、languages。 你可以在官方文档中找到更多信息。 以下是如何从框架中使用它们。
翻译
SwiftGoogleTranslate.shared.translate("Hello!", "es", "en") { (text, error) in
if let t = text {
print(t)
}
}
检测
SwiftGoogleTranslate.shared.detect("¡Hola!") { (detections, error) in
if let detections = detections {
for detection in detections {
print(detection.language)
print(detection.isReliable)
print(detection.confidence)
print("---")
}
}
}
语言列表
SwiftGoogleTranslate.shared.languages { (languages, error) in
if let languages = languages {
for language in languages {
print(language.language)
print(language.name)
print("---")
}
}
}
SwiftGoogleTranslate 基于 MIT 许可发布。 详情请查看 LICENSE 文件。