UNPKG

@vendure/core

Version:

A modern, headless ecommerce framework

94 lines (93 loc) 3.42 kB
import { OnModuleInit } from '@nestjs/common'; import { Handler, Request } from 'express'; import { GraphQLError } from 'graphql'; import { TFunction } from 'i18next'; import { GraphQLErrorResult } from '../common/error/error-result'; import { ConfigService } from '../config/config.service'; /** * @description * I18n resources used for translations * * @docsCategory common * @docsPage I18nService */ export interface VendureTranslationResources { error: any; errorResult: any; message: any; } export interface I18nRequest extends Request { t: TFunction; } /** * This service is responsible for translating messages from the server before they reach the client. * The `i18next-express-middleware` middleware detects the client's preferred language based on * the `Accept-Language` header or "lang" query param and adds language-specific translation * functions to the Express request / response objects. * * @docsCategory common * @docsPage I18nService * @docsWeight 0 */ export declare class I18nService implements OnModuleInit { private configService; /** * The set of language codes we have translation resources for. Used as the i18next * `supportedLngs` allow-list. Without this, `i18next-http-middleware` appends every * distinct request language code to `options.preload` forever, which is then re-walked * (via `Intl.getCanonicalLocales`) on every request — degrading performance over time. */ private readonly supportedLanguages; /** * @internal * @param configService */ constructor(configService: ConfigService); /** * @internal */ onModuleInit(): Promise<TFunction<"translation", undefined>>; /** * Reads the language codes for which we ship message files, derived from the filenames * in the `messages` directory (e.g. `en.json` -> `en`, `pt_BR.json` -> `pt_BR`). Falls * back to a static list if the directory cannot be read. */ private getBundledLanguageCodes; /** * Registers a language code as supported, extending the i18next `supportedLngs` allow-list * at runtime. Needed because plugins may add translations for new languages after init via * {@link addTranslation}. `supportedLngs` is cached inside i18next's `languageUtils` at init * time, so both the cached copy and `options` must be updated for the change to take effect. */ private registerSupportedLanguage; /** * @internal */ handle(): Handler; /** * @description * Add a I18n translation by json file * * @param langKey language key of the I18n translation file * @param filePath path to the I18n translation file */ addTranslationFile(langKey: string, filePath: string): void; /** * @description * Add a I18n translation (key-value) resource * * @param langKey language key of the I18n translation file * @param resources key-value translations */ addTranslation(langKey: string, resources: VendureTranslationResources | any): void; /** * Translates the originalError if it is an instance of I18nError. * @internal */ translateError(req: I18nRequest, error: GraphQLError): GraphQLError; /** * Translates the message of an ErrorResult * @internal */ translateErrorResult(req: I18nRequest, error: GraphQLErrorResult): void; }