Skip to content

Installation

The purpose of this package is to make integration with Lingohub easy. It has two main functions:

  • Fetching all translation files
  • Pushing new translation keys to Lingohub.
bash
  npm i @xerius/codey-translate

Creating the translate.config.js

We can configure how the integration with Lingohub works, by using the translate.config.js file. This needs to be created in the root of your project.

js
module.exports = {
  authToken: process.env.LINGOHUB_AUTH_TOKEN,
  projectId: "codey",
  mergeCodeyTranslations: false,
  organisationId: "xerius",
  primaryFile: "nl-BE.json",
  secondaryFiles: ["fr-BE.json"],
  output: {
    folder: "src",
  },
};

Setting the auth token in .env

You might have noticed that we use an environment variable, we pre-install dot-env with our package. This makes sure that environment variables can be used which are defined in a .env file in the root of your project. Let's create this .env file with the content below.

LINGOHUB_AUTH_TOKEN=

The auth token can be found on the Lingohub portal

Adding the build script

The final step is to include the build script which is used to push and fetch translation keys.

json
{
  "scripts": {
    "build:translations": "npm run build:translations --prefix node_modules/@xerius/codey-translate"
  }
}

When we run npm run build:translations in our project, the translation files should appear in the configured output path.

Adding translations in Nuxt

The translations can be added in the Nuxt config file.

js
  i18n: {
    detectBrowserLanguage: false,
    parsePages: false,
    strategy: 'prefix',
    locales: [
      { code: 'nl-be', iso: 'nl-BE', file: 'nl-BE.json' },
      { code: 'fr-be', iso: 'fr-BE', file: 'fr-BE.json' }
    ],
    defaultLocale: 'nl-be',
    lazy: true,
    langDir: 'translations/'
  }

Adding the CI script (optional)

There is support to pushing new translation keys within a CI process. First we need to configure the CI task in your package.json:

json
{
  "scripts": {
    "build:ci": "npm run build:ci --prefix node_modules/@xerius/codey-translate"
  }
}

This script depends on the Lingohub auth token to be available. The token should be stored in an Azure Key Vault. When this is done, we can tell the pipeline script to use the key vault:

yaml
- task: AzureKeyVault@1
  displayName: "Azure Key Vault: kv-we-codey"
  inputs:
    azureSubscription: rg-we-shared # replace with your resource group
    KeyVaultName: kv-we-codey # replace with your key vault name

For the last step we need to add the build:ci script to the pipeline yaml:

yaml
- task: Npm@1
  displayName: run translations
  inputs:
    command: custom
    customCommand: run build:ci
    workingDir: your-working-dir
  env:
    LINGOHUB_AUTH_TOKEN: $(lingohub-auth-token)

The Codey module will push and fetch all latest translation keys and will make sure any changes are pushed to GIT.