当前位置: 首页>>代码示例>>Python>>正文


Python translate.Client方法代码示例

本文整理汇总了Python中google.cloud.translate.Client方法的典型用法代码示例。如果您正苦于以下问题:Python translate.Client方法的具体用法?Python translate.Client怎么用?Python translate.Client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在google.cloud.translate的用法示例。


在下文中一共展示了translate.Client方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: translate

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def translate(in_str):
    if not in_str:
        return
    trans_client = google_trans.Client()
    lang = trans_client.detect_language(in_str)['language']
    if lang == 'en':
        trans = trans_client.translate(
            in_str, target_language='zh')
        return trans['translatedText']
    else:
        trans = trans_client.translate(
            in_str, target_language='en')
        return trans['translatedText'] 
开发者ID:ideamark,项目名称:breadbot,代码行数:15,代码来源:web.py

示例2: google_translate

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def google_translate(sk_text):
    """ CAUTION! This is a paid service! """
    from google.cloud import translate
    translate_client = translate.Client()
    result = translate_client.translate(sk_text, source_language='sk', target_language='en')
    result = result['translatedText']
    print("Google Translate: {} -> {}".format(sk_text, result))
    return result 
开发者ID:verejnedigital,项目名称:verejne.digital,代码行数:10,代码来源:translate.py

示例3: __init__

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def __init__(self):
        super(GoogleTranslateFramework, self).__init__()
        credentials = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
        if credentials is None:
            raise ValueError("missing credentials")
        if credentials.startswith('{'):
            credential_path = os.path.join(self._tmp_dir, 'Gateway-Translate-API.json')
            with open(credential_path, 'w') as f:
                f.write(credentials)
            os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
        self._client = translate.Client()
        self.max_retry = 5
        self._GOOGLE_LIMIT_TIME = 100 
开发者ID:OpenNMT,项目名称:nmt-wizard-docker,代码行数:15,代码来源:entrypoint.py

示例4: get_labels

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def get_labels(photo_file):
    vision_client = vision.Client()
    image = vision_client.image(
                source_uri = 'gs://%s/%s' % (bucket_name, photo_file))
    return image.detect_labels(limit=3) 
开发者ID:GoogleCloudPlatform,项目名称:appengine-photoalbum-example,代码行数:7,代码来源:main.py

示例5: translate_text

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def translate_text(text):
    if tag_language == 'en':
        return text
    translate_client = translate.Client()
    result = translate_client.translate(text, target_language=tag_language)
    return result['translatedText'] 
开发者ID:GoogleCloudPlatform,项目名称:appengine-photoalbum-example,代码行数:8,代码来源:main.py

示例6: supported_locale_codes

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def supported_locale_codes():
    return [l[u'language'] for l in translate.Client().get_languages()]

#https://github.com/fastlane/fastlane/blob/master/fastlane_core/lib/fastlane_core/languages.rb 
开发者ID:metasmile,项目名称:strsync,代码行数:6,代码来源:gentxtl10n.py

示例7: _translate

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def _translate(dest_l, src_text_lines):
            lines = [t[u'translatedText']+'\n' for t in translate.Client(target_language=dest_l).translate(src_text_lines)]
            return map(lambda line: proc_after_translate(line), lines) 
开发者ID:metasmile,项目名称:strsync,代码行数:5,代码来源:gentxtl10n.py

示例8: supported_locale_codes

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def supported_locale_codes():
    return [l[u'language'] for l in translate.Client().get_languages()] 
开发者ID:metasmile,项目名称:strsync,代码行数:4,代码来源:strtrans.py

示例9: translate_strs

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def translate_strs(strs, to):
    __trans = translate.Client(target_language=to)

    assert len(strs) or isinstance(strs[0], str), "Input variables should be string list"
    pre_items = [item for item in __preprocessing_translate_strs(strs, to)]

    translated_items = __trans.translate([item.trans_input_text for item in pre_items])
    assert len(translated_items) == len(pre_items), "the numbers of input items and translated items must be same."

    # map results
    for i, t in enumerate(translated_items):
        _result = t[u'translatedText']
        _pre_trans_item = pre_items[i]

        _literal_replacement_exist = bool(len(_pre_trans_item.matched_literal_items))
        _literal_replacement_contains_all = bool(
            len(filter(lambda s: s in _result, [mitem.replacement for mitem in _pre_trans_item.matched_literal_items])))

        # if literal replacement did not contain from translator, use original text.
        if _literal_replacement_exist and not _literal_replacement_contains_all:
            _pre_trans_item.trans_output_text = _pre_trans_item.original_text
        # else, it means all replacing processes are completed, use/commit translated text.
        else:
            _pre_trans_item.trans_output_text = _result

        # print pre_items[i].trans_input_text, '->' , pre_items[i].trans_output_text
    return __PostprocessingTransItem(pre_items).finalize_strs() 
开发者ID:metasmile,项目名称:strsync,代码行数:29,代码来源:strtrans.py

示例10: get_g_translate

# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def get_g_translate(sent: str, lang: str) -> Optional[str]:
    try:
        client = translate.Client()
        t = client.translate(sent, target_language=lang)['translatedText']
        return t
    except Exception as e:
        print(e)
        return '' 
开发者ID:facebookresearch,项目名称:vizseq,代码行数:10,代码来源:google_translate.py


注:本文中的google.cloud.translate.Client方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。