Skip to content

Commit

Permalink
feat: support alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Jan 9, 2020
1 parent 7ebbf68 commit 72baa7a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
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 @@ -352,12 +353,14 @@ 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) {
return 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

0 comments on commit 72baa7a

Please sign in to comment.