# I18n

If you want to support multiple languages in your project. Vuesion has some useful tools to make it easier to manage your translations.

# Default locale and supported locales

To tell the tooling which locale is your default locale you have to define it in the file: ./vuesion/config.json.

You can also add as many other locales as you want to support. The extract-i18n-messages script will add these translation files with the default messages defined for the default locale.

Example config:

  "i18n": {
    "defaultLocale": "en",
    "locales": [
        {
          "code": "en",
          "file": "en.json"
        },
        {
          "code": "de",
          "file": "de.json"
        }
    ]
    }

# Define and extract default messages

Open up a file, e.g. ./src/pages/index.vue:

<template>
  <div>
      <landing-page-header />
      ...
  </div>
</template>

and add a translation for our default locale (en):

<template>
  <div>
      {{ $t('home.test' /* this is a test!!! */) }}
      <landing-page-header />
      ...
  </div>
</template>

Now run npm run extract-i18n-messages and go to http://localhost:3000/ (opens new window).

You should see the message this is a test!!! one the top-left of the screen.

If you open up the file ./i18n/en.json you should see a new key with the default message as value:

{
...
  "home.test": "this is a test!!!"
}

You should also see that the file ./i18n/de.json has the exact same key and value added.

Let's translate it into German:

{
...
  "home.test": "das ist ein Test!!!"
}

and run npm run extract-i18n-messages again.

The translated value is not overwritten because it is not the default locale.

If you change the default message in your Vue component:

<template>
  <div>
      {{ $t('home.test' /* this is not a test!!! */) }}
      <landing-page-header />
      ...
  </div>
</template>

and run npm run extract-i18n-messages again.

You will have a new value in ./i18n/en.json:

{
...
  "home.test": "this is not a test!!!"
}

but the value in ./i18n/de.json will stay the same:

{
...
  "home.test": "das ist ein Test!!!"
}

# Known limitations

If you want to add HTML to your default messages inside a template, you have to use [] instead of <> e.g.:

<template>
  <div>
    <div v-html="$t('home.link' /* [a href='https://example.com' target='_blank']this is a link[/a] */)" />
    ...
  </div>
</template>