How to use the @google-cloud/translate function in @google-cloud/translate

To help you get started, we’ve selected a few @google-cloud/translate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ZolaApp / zola-api / src / models / TranslationValue / prefillTranslationsValues / index.js View on Github external
// @flow
import Translate from '@google-cloud/translate'
import Project from '@models/Project'
import Locale from '@models/Locale'
import TranslationValue from '@models/TranslationValue'

const translate = new Translate({ key: process.env.TRANSLATE_API_KEY })

const prefillTranslationsValues = async (
  project: Project,
  newLocale: Locale
) => {
  const defaultLocale = project.locales[0]
  const sortedKeys = project.translationKeys.sort((a, b) => a.id - b.id)
  const stringsToBeTranslated = sortedKeys.map(translationKey => {
    const translationValue = translationKey.translationValues.find(
      translationValue => translationValue.locale.code === defaultLocale.code
    )

    return translationValue ? translationValue.value : ''
  })

  const targetLanguage = newLocale.code