|
| 1 | +/** |
| 2 | + * Copyright 2019, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +function main( |
| 18 | + projectId = 'YOUR_PROJECT_ID', |
| 19 | + location = 'us-central1', |
| 20 | + modelId = 'model-id', |
| 21 | + text = 'text to translate' |
| 22 | +) { |
| 23 | + // [START translate_text_with_model_beta] |
| 24 | + /** |
| 25 | + * TODO(developer): Uncomment these variables before running the sample. |
| 26 | + */ |
| 27 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 28 | + // const location = 'global'; |
| 29 | + // const modelId = 'YOUR_MODEL_ID'; |
| 30 | + |
| 31 | + // Imports the Google Cloud Translation library |
| 32 | + const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1; |
| 33 | + const automl = require('@google-cloud/automl'); |
| 34 | + |
| 35 | + // Instantiates a client |
| 36 | + const translationClient = new TranslationServiceClient(); |
| 37 | + const autoMLClient = new automl.AutoMlClient(); |
| 38 | + async function translateTextWithModel() { |
| 39 | + const model = autoMLClient.modelPath(projectId, location, modelId); |
| 40 | + // Construct request |
| 41 | + const request = { |
| 42 | + parent: translationClient.locationPath(projectId, location), |
| 43 | + contents: [text], |
| 44 | + mimeType: 'text/plain', // mime types: text/plain, text/html |
| 45 | + sourceLanguageCode: 'en-US', |
| 46 | + targetLanguageCode: 'ja', |
| 47 | + model: model, |
| 48 | + }; |
| 49 | + |
| 50 | + // Run request |
| 51 | + const [response] = await translationClient.translateText(request); |
| 52 | + |
| 53 | + for (const translation of response.translations) { |
| 54 | + console.log(`Translated Content: ${translation.translatedText}`); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + translateTextWithModel(); |
| 59 | + // [END translate_text_with_model_beta] |
| 60 | +} |
| 61 | + |
| 62 | +main(...process.argv.slice(2)); |
0 commit comments