在 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 範例瀏覽器