lng-from-req.ts 587 字节
Newer Older
1
import {Config} from '../../types';
2
import {Request} from 'express';
P
Peter Pan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

export const lngFromReq = (req: Request) => {
    if (!req.i18n) {
        return null;
    }

    const {allLanguages, defaultLanguage, fallbackLng} = req.i18n.options as Config;
    const fallback = fallbackLng || defaultLanguage;

    if (!req.i18n.languages) {
        return typeof fallback === 'string' ? fallback : null;
    }

    const language = req.i18n.languages.find(l => allLanguages.includes(l)) || fallback;

    if (typeof language === 'string') {
        return language;
    }

    return null;
};