Interface LanguageDetectorOption

interface LanguageDetectorOption {
    cookie?: Cookie;
    fallbackLanguage: string;
    order?: ("searchParams" | "cookie" | "session" | "header" | "custom")[];
    searchParamKey?: string;
    sessionKey?: string;
    sessionStorage?: SessionStorage<SessionData, SessionData>;
    supportedLanguages: string[];
    findLocale(request: Request): Promise<null | string | string[]>;
}

Properties

cookie?: Cookie

If you want to use a cookie to store the user preferred language, you can pass the Cookie object here.

fallbackLanguage: string

Define the fallback language that it's going to be used in the case user expected language is not supported. This should be be same as the fallbackLng in the i18next options.

order?: ("searchParams" | "cookie" | "session" | "header" | "custom")[]

The order the library will use to detect the user preferred language. By default the order is

  • searchParams
  • cookie
  • session
  • header If customized, a an extra custom option can be added to the order. And finally the fallback language.
searchParamKey?: string

If you want to use search parameters for language detection and want to change the default key used to for the parameter name, you can pass the key here.

"lng"
sessionKey?: string

If defined a sessionStorage and want to change the default key used to store the user preferred language, you can pass the key here.

"lng"
sessionStorage?: SessionStorage<SessionData, SessionData>

If you want to use a session to store the user preferred language, you can pass the SessionStorage object here. When this is not defined, getting the locale will ignore the session.

supportedLanguages: string[]

Define the list of supported languages, this is used to determine if one of the languages requested by the user is supported by the application. This should be be same as the supportedLngs in the i18next options.

Methods

  • A function that can be used to find the locale based on the request object using any custom logic you want. This can be useful to get the locale from the URL pathname, or to query it from the database or fetch it from an API.

    Parameters

    • request: Request

      The request object received by the server.

    Returns Promise<null | string | string[]>