在 for-in 循环中使用 Google 翻译

在 for-in 循环中使用 Cloud Translation API 连接器。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

YAML

- init:
    assign:
      - textAndSourceLang:
          "Hello": "en"
          "Ciao": "it"
          "Auf wiedersehen": "de"
          "Goodbye": "en"
          "Bonjour": "fr"
          "lkajshflkj": "unknown"
      - allowedSourceLang: ["en", "de", "it", "fr"]
- translateToFrench:
    for:
      value: text
      in: ${keys(textAndSourceLang)}
      steps:
        - verifySource:
            switch:
              - condition: ${not(textAndSourceLang[text] in allowedSourceLang)}
                next: continue
              - condition: ${textAndSourceLang[text] == "fr"}
                next: skipFrenchTranslation
        - translate:
            call: googleapis.translate.v2.translations.translate
            args:
              q: ${text}
              target: "fr"
              format: "text"
              source: ${textAndSourceLang[text]}
            result: translation
        - getTranslation:
            assign:
              - translated: ${translation.data.translations[0].translatedText}
            next: print
        - skipFrenchTranslation:
            assign:
              - translated: ${text}
        - print:
            call: sys.log
            args:
              text: '${"Original: " + text + ", Translation: " + translated}'

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器