Skip to content

Commit

Permalink
Merge cf2db9d into 0852b42
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Jan 13, 2020
2 parents 0852b42 + cf2db9d commit b2cd3fc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/runtime/src/globalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as Globalize from 'globalize';
import {parse} from 'yamljs';
import {AnyObject, STRONGLOOP_GLB} from './config';
import * as helper from './helper';
import {getLangAlias} from './helper';
const dbg = debugModule('strong-globalize');
const osLocale = require('os-locale');
const MapCache = require('lodash/_MapCache');
Expand Down Expand Up @@ -63,6 +64,7 @@ function osLanguage() {
* It tries to use OS language, then falls back to 'en'
*/
export function setDefaultLanguage(lang?: string) {
if (lang) lang = getLangAlias(lang);
lang = helper.isSupportedLanguage(lang) ? lang : undefined;
lang = lang || MY_APP_LANG || OS_LANG || helper.ENGLISH;
loadGlobalize(lang);
Expand All @@ -71,6 +73,7 @@ export function setDefaultLanguage(lang?: string) {
}
STRONGLOOP_GLB.locale!(lang);
STRONGLOOP_GLB.DEFAULT_LANG = lang;

return lang;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/runtime/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,3 +952,17 @@ export function removeDoubleCurlyBraces(json: AnyObject) {
debug(count, key + ' : ' + json[key]);
});
}

/**
* If an language has alias name that SG supports, return the alias name.
* @param lang
*/
export function getLangAlias(lang: string): string {
// The {lang: alias} pairs
const ALIAS_MAP: {[lang: string]: string} = {
'zh-cn': 'zh-Hans',
'zh-tw': 'zh-Hant',
};
if (lang && ALIAS_MAP.hasOwnProperty(lang)) return ALIAS_MAP[lang];
return lang;
}
5 changes: 4 additions & 1 deletion packages/runtime/src/strong-globalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as helper from './helper';
import * as path from 'path';

import {AnyObject, STRONGLOOP_GLB} from './config';
import {getLangAlias} from './helper';

// tslint:disable:no-any

Expand Down Expand Up @@ -93,6 +94,7 @@ export class StrongGlobalize {
}

setLanguage(lang?: string) {
if (lang) lang = getLangAlias(lang);
lang = helper.isSupportedLanguage(lang)
? lang
: STRONGLOOP_GLB.DEFAULT_LANG;
Expand Down Expand Up @@ -352,11 +354,12 @@ export class StrongGlobalize {
StrongGlobalize
>(); /* eslint-env es6 */
http(req: {headers: AnyObject}) {
const matchingLang = helper.getLanguageFromRequest(
let matchingLang = helper.getLanguageFromRequest(
req,
this._options.appLanguages,
this._options.language
);
matchingLang = getLangAlias(matchingLang);

let sg = StrongGlobalize.sgCache.get(matchingLang);
if (sg) {
Expand Down
12 changes: 12 additions & 0 deletions packages/runtime/test/test-globalize-singleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ test('formatMessage', function(t) {
targetMsg = 'Error:' + params.url + '或者' + params.port + '是無效。';
t.equal(message, targetMsg, 'Traditional Chinese message formatting works.');

g.setDefaultLanguage('zh-cn');
message = g.formatMessage(key, params);
t.comment(message);
targetMsg = 'Error:' + params.url + '或者' + params.port + '是无效。';
t.equal(message, targetMsg, 'Simplified Chinese message formatting works.');

g.setDefaultLanguage('zh-tw');
message = g.formatMessage(key, params);
t.comment(message);
targetMsg = 'Error:' + params.url + '或者' + params.port + '是無效。';
t.equal(message, targetMsg, 'Traditional Chinese message formatting works.');

g.setDefaultLanguage('de');
message = g.formatMessage(key, params);
t.comment(message);
Expand Down
21 changes: 19 additions & 2 deletions packages/runtime/test/test-load-msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var test = require('tap').test;

SG.SetRootDir(__dirname);
SG.SetDefaultLanguage();
SG.SetAppLanguages();
SG.SetAppLanguages(['en', 'zh-cn', 'zh-Hans']);

var g = new SG();

Expand Down Expand Up @@ -56,7 +56,7 @@ test('remove double curly braces', function(t) {
t.end();
});

test('accept-language header', function(t) {
test('accept-language header - en', function(t) {
var req = {
headers: {
'accept-language': 'en',
Expand All @@ -66,3 +66,20 @@ test('accept-language header', function(t) {
t.equal(message, 'Test message');
t.end();
});

test('accept-language header - alias', function(t) {
var req = {
headers: {
// alias to 'zh-Hans'
'accept-language': 'zh-cn',
},
};

// create a SG instance for language 'zh-Hans' and register it
var sg_hans = new SG({language: 'zh-Hans'});
SG.sgCache.set('zh-Hans', sg_hans);

var cachedSg = g.http(req);
t.equal(cachedSg.getLanguage(), 'zh-Hans');
t.end();
});

0 comments on commit b2cd3fc

Please sign in to comment.