src/i18n/index.js
// initialize the i18n library in the server and returns its middleware
initialize the i18n library in the server and returns its middleware
initialize the i18n library in the server and returns its middleware
function i18nMiddleware() {
  init(INIT_OPTIONS);
  return (req, res, next) => {
    const isDocument = DOCUMENT_REGEX.test(req.originalUrl);
    if (!isDocument) {
      next();
      return;
    }
    let locale = getCookie("locale", req.headers.cookie);
    if (locale == null) {
      if (req.headers["accept-language"]) {
        const headerLang = req.headers["accept-language"].split(",")[0].trim();
        if (headerLang.length > 1) {
          locale = headerLang;
        }
      } else {
        locale = INIT_OPTIONS.initialLocale || INIT_OPTIONS.fallbackLocale;
      }
    }
    if (locale != null && locale !== currentLocale) {
      $locale.set(locale);
    }
    next();
  };
}
function i18nMiddleware ( ) { init ( INIT_OPTIONS ) ; return ( req , res , next ) => { const isDocument = DOCUMENT_REGEX . test ( req . originalUrl ) ; if ( ! isDocument ) { next ( ) ; return ; } let locale = getCookie ( "locale" , req . headers . cookie ) ; if ( locale == null ) { if ( req . headers [ "accept-language" ] ) { const headerLang = req . headers [ "accept-language" ] . split ( "," ) [ 0 ] . trim ( ) ; if ( headerLang . length > 1 ) { locale = headerLang ; } } else { locale = INIT_OPTIONS . initialLocale || INIT_OPTIONS . fallbackLocale ; } } if ( locale != null && locale !== currentLocale ) { $locale . set ( locale ) ; } next ( ) ; } ; }
initialize the i18n library in the server and returns its middleware
initialize the i18n library in the server and returns its middleware
// initialLocale will be set by the middleware // get the initial locale only for a document request // no cookie, let's get the first accepted language