Skip to content

Commit

Permalink
feat: move acceptLanguage to StrongGlobalize
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jun 9, 2020
1 parent 2db8ac3 commit eaa9cc0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
34 changes: 32 additions & 2 deletions packages/runtime/src/helper.ts
Expand Up @@ -6,14 +6,14 @@
import dbg = require('debug');
const debug = dbg('strong-globalize');

import * as acceptLanguage from 'accept-language';
import * as assert from 'assert';
import * as fs from 'fs';
import * as _ from 'lodash';
import * as md5 from 'md5';
import * as mkdirp from 'mkdirp';
import * as path from 'path';
import {AnyObject, ResourceTag, STRONGLOOP_GLB} from './config';
import {create} from 'accept-language';

export const ENGLISH = 'en';
export const PSEUDO_LANG = 'zz';
Expand Down Expand Up @@ -859,7 +859,6 @@ export function repackArgs(
* @param globalize
* @returns {*}
*/

export function getLanguageFromRequest(
req: {headers: {[name: string]: string}},
appLanguages: string[],
Expand All @@ -883,11 +882,42 @@ export function getLanguageFromRequest(
// Copy the array so that it won't be mutated
appLanguages = [defaultLanguage, ...appLanguages];

const acceptLanguage = create();
acceptLanguage.languages(appLanguages);
const bestLanguage = acceptLanguage.get(reqLanguage);
return bestLanguage || defaultLanguage;
}

/**
* Get a language from the http request
* @param req - Http request
* @param acceptLanguage - An instance of AcceptLanguage
* @param defaultLanguage - The default language
*/
export function acceptLanguageFromRequest(
req: {headers: {[name: string]: string}},
acceptLanguage: ReturnType<typeof create>,
defaultLanguage: string
): string {
if (!req || !req.headers || !req.headers['accept-language']) {
return defaultLanguage;
}

let languages = req.headers['accept-language'].split(',');
for (let i = 0; i < languages.length; i++) {
let languageWeighted = languages[i].split(';');
languageWeighted[0] = getLangAlias(languageWeighted[0].trim());
languages[i] = languageWeighted.join(';');
}
const reqLanguage = languages.join(',');
if (!reqLanguage) {
return defaultLanguage;
}

const bestLanguage = acceptLanguage.get(reqLanguage);
return bestLanguage || defaultLanguage;
}

export function myIntlDir() {
return INTL_DIR;
}
Expand Down
17 changes: 15 additions & 2 deletions packages/runtime/src/strong-globalize.ts
Expand Up @@ -7,6 +7,7 @@
import * as globalize from './globalize';
import * as helper from './helper';
import * as path from 'path';
import {create} from 'accept-language';

import {AnyObject, STRONGLOOP_GLB} from './config';
import {getLangAlias} from './helper';
Expand Down Expand Up @@ -43,6 +44,7 @@ export class StrongGlobalize {
static readonly STRONGLOOP_GLB: AnyObject = STRONGLOOP_GLB;

private _options: AnyObject;
private acceptLanguage: ReturnType<typeof create>;

constructor(options?: AnyObject) {
if (typeof options === 'string') {
Expand All @@ -60,6 +62,12 @@ export class StrongGlobalize {
};

this._options = options ? Object.assign(defaults, options) : defaults;

this.acceptLanguage = create();
this.acceptLanguage.languages([
this._options.language,
...(this._options.appLanguages ?? []),
]);
}

static SetPersistentLogging = globalize.setPersistentLogging;
Expand Down Expand Up @@ -353,10 +361,15 @@ export class StrongGlobalize {
string,
StrongGlobalize
>(); /* eslint-env es6 */

/**
* Get an instance for the given http request
* @param req - http request
*/
http(req: {headers: AnyObject}) {
const matchingLang = helper.getLanguageFromRequest(
const matchingLang = helper.acceptLanguageFromRequest(
req,
this._options.appLanguages,
this.acceptLanguage,
this._options.language
);

Expand Down

0 comments on commit eaa9cc0

Please sign in to comment.