javascript - 如何使用谷歌云维护的参考词汇表实现谷歌云翻译 API 来翻译前端数据?
问题描述
如何在前端实现谷歌云翻译 API,使用谷歌云存储桶中维护的参考词汇表将整个数据翻译成目标语言?
我知道以下代码片段可用于使用词汇表在后端翻译数据。
public static void translateTextWithGlossary(String projectId, String sourceLanguage, String targetLanguage,
String text, String glossaryId) throws IOException {
// Initialize client that will be used to send requests. This client only needs
// to be created
// once, and can be reused for multiple requests. After completing all of your
// requests, call
// the "close" method on the client to safely clean up any remaining background
// resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
String location = "us-central1";
LocationName parent = LocationName.of(projectId, location);
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
TranslateTextGlossaryConfig glossaryConfig = TranslateTextGlossaryConfig.newBuilder()
.setGlossary(glossaryName.toString()).build();
// Supported Mime Types:
// https://cloud.google.com/translate/docs/supported-formats
TranslateTextRequest request = TranslateTextRequest.newBuilder().setParent(parent.toString())
.setMimeType("text/plain").setSourceLanguageCode(sourceLanguage)
.setTargetLanguageCode(targetLanguage).addContents(text).setGlossaryConfig(glossaryConfig).build();
TranslateTextResponse response = client.translateText(request);
// Display the translation for each input text provided
for (Translation translation : response.getGlossaryTranslationsList()) {
System.out.printf("\n\nTranslated text: \n%s\n", translation.getTranslatedText());
}
}
}
解决方案
推荐阅读
- angular - 角度自定义文件夹
- javascript - NodeJs 和 HTTPS:如何使用来自 globalSign 的“CSR”和“PEM”数据
- python - 如何使用 WSL(Linux 的 Windows 子系统)访问 Django 服务器?
- python - 如何在 Windows 的终端内运行 python shell?
- replace - 在 Twig 中查找和替换动态创建的字符串?
- python - 将具有多个参数的函数应用于多个列以创建新列
- javascript - 在测试中使用回调排序
- botframework - 如何跟踪 QnAMaker 中提出的问题类型?
- unity3d - 为什么 Animator Controller 中的 Preview 窗口是空的?
- scripting - 相关记录的google app maker链接字段