***

title: "Translations"
metaTitle: "Internationalization and Translations in Vendure"
metaDescription: "Handle multi-language content in Vendure with translatable entities, Admin UI localization, and server-side error message translations."
----------------------------------------------------------------------------------------------------------------------------------------------------------

The following items in Vendure can be translated:

* Entities which implement the [`Translatable`](/current/framework/reference/typescript-api/entities/interfaces/#translatable) interface.
* Admin UI text labels and messages
* Server error message

## Translatable entities

The following entities implement the `Translatable` interface:

* [Collection](/current/framework/reference/typescript-api/entities/collection/)
* [Country](/current/framework/reference/typescript-api/entities/country/)
* [Facet](/current/framework/reference/typescript-api/entities/facet/)
* [FacetValue](/current/framework/reference/typescript-api/entities/facet-value/)
* [PaymentMethod](/current/framework/reference/typescript-api/entities/payment-method/)
* [Product](/current/framework/reference/typescript-api/entities/product/)
* [ProductOption](/current/framework/reference/typescript-api/entities/product-option/)
* [ProductOptionGroup](/current/framework/reference/typescript-api/entities/product-option-group/)
* [ProductVariant](/current/framework/reference/typescript-api/entities/product-variant/)
* [Promotion](/current/framework/reference/typescript-api/entities/promotion/)
* [Province](/current/framework/reference/typescript-api/entities/province/)
* [Region](/current/framework/reference/typescript-api/entities/region/)
* [ShippingMethod](/current/framework/reference/typescript-api/entities/shipping-method/)

To understand how translatable entities are implemented, let's take a look at a simplified version of the `Facet` entity:

```ts
@Entity()
export class Facet extends VendureEntity implements Translatable {
    
    name: LocaleString; // [!code highlight]

    @Column({ unique: true })
    code: string;

    @OneToMany(type => FacetTranslation, translation => translation.base, { eager: true }) // [!code highlight]
    translations: Array<Translation<Facet>>; // [!code highlight]
}
```

All translatable entities have a `translations` field which is a relation to the translations. Any fields that are to be translated are of type `LocaleString`, and **do note have a `@Column()` decorator**. This is because the `name` field here does not in fact exist in the database in the `facet` table. Instead, it belongs to the `facet_translations` table, which brings us to the `FacetTranslation` entity (again simplified for clarity):

```ts
@Entity()
export class FacetTranslation extends VendureEntity implements Translation<Facet> {

    @Column('varchar') languageCode: LanguageCode;

    @Column() name: string; // [!code highlight]

    @Index()
    @ManyToOne(type => Facet, base => base.translations, { onDelete: 'CASCADE' })
    base: Facet;
}
```

Thus there is a one-to-many relation between `Facet` and `FacetTranslation`, which allows Vendure to handle multiple translations of the same entity. The `FacetTranslation` entity also implements the `Translation` interface, which requires the `languageCode` field and a reference to the base entity.

### Loading translatable entities

If your plugin needs to load a translatable entity, you will need to use the [`TranslatorService`](/current/framework/reference/typescript-api/service-helpers/translator-service/) to hydrate all the `LocaleString` fields will the actual translated values from the correct translation.

For example, if you are loading a `Facet` entity, you would do the following:

```ts
import { Facet } from '@vendure/core';
import { LanguageCode, RequestContext, TranslatorService, TransactionalConnection } from '@vendure/core';

@Injectable()
export class MyService {
    
    constructor(private connection: TransactionalConnection, private translator: TranslatorService) {}

    async getFacet(ctx: RequestContext, id: ID): Promise<Facet | undefined> {
        const facet = await this.connection.getRepository(ctx, Facet).findOne(id);
        if (facet) {
            return this.translatorService.translate(facet, ctx); // [!code highlight]
        }
    }
    
    async getFacets(ctx: RequestContext): Promise<Facet[]> {
        const facets = await this.connection.getRepository(ctx, Facet).find(); // [!code highlight]
        return Promise.all(facets.map(facet => this.translatorService.translate(facet, ctx))); // [!code highlight]
    }
}
```

## Admin UI translations

See the [Adding Admin UI Translations guide](/current/framework/extending-the-admin-ui/adding-ui-translations/).

## Server message translations

Let's say you've implemented some custom server-side functionality as part of a plugin. You may be returning custom errors or other messages. Here's how you can
provide these messages in multiple languages.

Using [`addTranslation`](/current/framework/reference/typescript-api/common/i18n-service/#addtranslation) inside the `onApplicationBootstrap` ([Nestjs lifecycle hooks](https://docs.nestjs.com/fundamentals/lifecycle-events)) of a Plugin is the easiest way to add new translations.
While Vendure is only using the `error`, `errorResult`, `message` and `configurableOperation` resource keys you are free to use your own.

### Translatable Error

This example shows how to create a custom translatable error

```ts
/**
 * Custom error class
 */
class CustomError extends ErrorResult {
    readonly __typename = 'CustomError';
    readonly errorCode = 'CUSTOM_ERROR';
    readonly message = 'CUSTOM_ERROR'; //< looks up errorResult.CUSTOM_ERROR
}

@VendurePlugin({
    imports: [PluginCommonModule],
    providers: [I18nService],
    // ...
})
export class TranslationTestPlugin implements OnApplicationBootstrap {

    constructor(private i18nService: I18nService) {

    }

    onApplicationBootstrap(): any {
        this.i18nService.addTranslation('en', {
            errorResult: {
                CUSTOM_ERROR: 'A custom error message',
            },
            anything: {
                foo: 'bar'
            }
        });

        this.i18nService.addTranslation('de', {
            errorResult: {
                CUSTOM_ERROR: 'Eine eigene Fehlermeldung',
            },
            anything: {
                foo: 'bar'
            }
        });

    }
}
```

To receive an error in a specific language you need to use the `languageCode` query parameter
`query(QUERY_WITH_ERROR_RESULT, { variables }, { languageCode: LanguageCode.de });`

### Configurable operation translations

The `description` of a [`ConfigurableOperationDef`](/current/framework/reference/typescript-api/configurable-operation-def/configurable-operation-def/) — a promotion condition, shipping calculator, collection filter and so on — as well as the `label` and `description` of each of its args, can be defined inline as an array of localized strings:

```ts
export const customShippingCalculator = new ShippingCalculator({
    code: 'custom-shipping-calculator',
    description: [
        { languageCode: LanguageCode.en, value: 'Custom shipping calculator' },
        { languageCode: LanguageCode.de, value: 'Eigener Versandrechner' },
    ],
    args: {
        rate: {
            type: 'int',
            label: [{ languageCode: LanguageCode.en, value: 'Shipping price' }],
        },
    },
    calculate: order => ({ price: 500, priceIncludesTax: false, taxRate: 0 }),
});
```

The same strings can also come from the message catalogs, under the `configurableOperation` namespace. This keeps the operation definition readable when you support many languages, and it lets you translate an operation defined by someone else without forking it.

The keys are derived from the operation, so there is nothing to register:

```text
configurableOperation.<type>.<code>.description
configurableOperation.<type>.<code>.args.<argName>.label
configurableOperation.<type>.<code>.args.<argName>.description
configurableOperation.<type>.<code>.args.<argName>.options.<optionValue>.label
```

`<type>` is the registry the operation belongs to, such as `ShippingCalculator` or `PromotionCondition`. It forms part of the key because a `code` is only unique within its own registry — core itself uses `buy_x_get_y_free` for both a promotion condition and a promotion action.

So the German strings above could instead be registered like this:

```ts
@VendurePlugin({
    imports: [PluginCommonModule],
    providers: [I18nService],
    // ...
})
export class MyPlugin implements OnApplicationBootstrap {

    constructor(private i18nService: I18nService) {}

    onApplicationBootstrap(): any {
        this.i18nService.addTranslation('de', {
            configurableOperation: {
                ShippingCalculator: {
                    'custom-shipping-calculator': {
                        description: 'Eigener Versandrechner',
                        args: {
                            rate: { label: 'Versandpreis' },
                        },
                    },
                },
            },
        });
    }
}
```

### Which language these strings are resolved into

These strings describe the operation itself, so they should appear in whatever language the administrator reads. That is a separate setting from the one which selects the translation of your product and Collection data.

The two come from different parts of the request. The `languageCode` query parameter selects which translation of your data to return, such as the product name or the Collection description. The `Accept-Language` header tells the server which language to write its own text in, and operation descriptions and argument labels count as its own text. So an administrator reading the Admin UI in Japanese while editing the German translation of a product sends `?languageCode=de` together with `Accept-Language: ja`, and gets German product data described by Japanese labels.

A client which sends no `Accept-Language` header falls back to `languageCode`.

Vendure tries each language in turn: first the languages from `Accept-Language` in the order the header gives them, then the request's `languageCode`, then the Channel's default language, then English. For each one it looks in the catalogs first and in the inline array second. A catalog entry therefore wins over an inline string for the same language, which is what allows a third-party operation's wording to be overridden. An operation which ships only inline strings still resolves to its own language rather than to an English catalog entry defined elsewhere.

Select option labels behave slightly differently. The description and the argument labels arrive at the client already resolved to a single string, but option labels are sent as the full array with the catalog translation merged into it. The merged entry carries the language code the client asked for. Clients look up their own display language and fall back to the first entry in the array when they find no exact match, so a translation filed under any other code would be passed over.

:::caution
Browsers send an `Accept-Language` header of their own, derived from the operating system locale, on every request. A browser-based Admin API client which relies on `languageCode` alone for these strings will therefore resolve them against that OS locale.

If you maintain such a client, set the header explicitly from whatever language your interface is displayed in. The Dashboard and the Admin UI both do this. Server-to-server clients are unaffected, since they only send the header if they choose to.
:::

:::note
A key which matches no operation is never read, so a mistyped key produces no error — the inline string is used instead and the mistake is silent. Call `getTranslationKeys()` on the operation, which returns the exact key paths it expects.
:::

Several of core's descriptions are templates rather than sentences, for example `Discount order by { discount }%`. The admin UI substitutes the live argument values into these placeholders, so a translated string must reproduce them exactly, spaces inside the braces included.

### Use translations

Vendure uses the internationalization-framework [i18next](https://www.i18next.com/).

Therefore you are free to use the i18next translate function to [access keys](https://www.i18next.com/translation-function/essentials#accessing-keys) \
`i18next.t('error.any-message');`
