在使用Google Translate API进行翻译时,如果将保加利亚语的语言代码设置为“bg”,则无法正常翻译。这种问题可能是因为Google Translate API仅支持保加利亚语的西里尔语版本(代码为“bg-BG”)。因此,解决该问题的方法是将保加利亚语的语言代码更改为“bg-BG”。
示例代码:
#原始代码
# translate_text.py
def translate_text(text, target):
"""
Translates text into the target language.
Target must be an ISO 639-1 language code.
https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
import six
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
if isinstance(text, six.binary_type):
text = text.decode("utf-8")
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language=target)
print(u"Text: {}".format(result["input"]))
print(u"Translation: {}".format(result["translatedText"]))
print(u"Detected source language: {}".format(result["detectedSourceLanguage"]))
translate_text("Hello world", "bg") #改为 "bg-BG"
修改后的代码:
# translate_text.py
def translate_text(text, target):
"""
Translates text into the target language.
Target must be an ISO 639-1 language code.
https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
import six
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
if isinstance(text, six.binary_type):
text = text.decode("utf-8")
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language="bg-BG")
print(u"Text: {}".format(result["input
上一篇:不了解V8到底是什么
下一篇:不良的C代码是否会导致蓝屏死机?