This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #563 from edx/dsjen/fix-lang
AN-7761: Refactored setting window.language.
- Loading branch information
Showing
6 changed files
with
98 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
analytics_dashboard/static/js/test/specs/fix-language-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
define(['utils/fix-language'], function(fixLanguages) { | ||
'use strict'; | ||
|
||
function expectLanguages(actualLanguages, expectedLanguageCode) { | ||
actualLanguages.forEach(function(languageCode) { | ||
expect(fixLanguages(languageCode), expectedLanguageCode); | ||
}); | ||
} | ||
|
||
describe('window.language', function() { | ||
it('should default to English if an invalid argument is provided or not known to CLDR', function() { | ||
expectLanguages([null, '', 1, false, 'not-real'], 'en'); | ||
}); | ||
|
||
it('should return zh if the given language code is either zh-cn, zh-sg and zh-hans(-*) ', function() { | ||
expectLanguages(['zh-cn', 'zh-sg', 'zh-hans', 'zh-hans-cn'], 'zh'); | ||
}); | ||
|
||
it('should return zh-Hant if the given language code is either zh-tw, zh-hk, zh-mo and zh-hant-* ', | ||
function() { | ||
expectLanguages(['zh-tw', 'zh-hk', 'zh-mo', 'zh-hant-tw'], 'zh-Hant'); | ||
} | ||
); | ||
|
||
it('should return correct-casing any given cased language code', function() { | ||
expectLanguages(['en-gb', 'en-GB', 'EN-GB'], 'en-GB'); | ||
}); | ||
}); | ||
}); |
41 changes: 5 additions & 36 deletions
41
analytics_dashboard/static/js/test/specs/globalization-spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,11 @@ | ||
// eslint-disable-next-line import/no-unresolved | ||
define(['json!cldr-data/availableLocales.json', 'utils/globalization'], function(availableLocales) { | ||
define(['utils/globalization'], function(Globalization) { | ||
'use strict'; | ||
|
||
var lowerLocalesMapping = {}; | ||
availableLocales.availableLocales.forEach(function(locale) { | ||
lowerLocalesMapping[locale.toLowerCase()] = locale; | ||
}); | ||
|
||
describe('fixLanguageCode', function() { | ||
it('should default to English if an invalid argument is provided', function() { | ||
expect(fixLanguageCode(null, lowerLocalesMapping)).toEqual('en'); | ||
expect(fixLanguageCode('', lowerLocalesMapping)).toEqual('en'); | ||
expect(fixLanguageCode(1, lowerLocalesMapping)).toEqual('en'); | ||
expect(fixLanguageCode(true, lowerLocalesMapping)).toEqual('en'); | ||
}); | ||
|
||
it('should return zh if the given language code is either zh-cn, zh-sg and zh-hans(-*) ', function() { | ||
expect(fixLanguageCode('zh-cn', lowerLocalesMapping)).toEqual('zh'); | ||
expect(fixLanguageCode('zh-sg', lowerLocalesMapping)).toEqual('zh'); | ||
expect(fixLanguageCode('zh-hans', lowerLocalesMapping)).toEqual('zh'); | ||
expect(fixLanguageCode('zh-hans-cn', lowerLocalesMapping)).toEqual('zh'); | ||
}); | ||
|
||
it('should return zh-Hant if the given language code is either zh-tw, zh-hk, zh-mo and zh-hant-* ', function() { | ||
expect(fixLanguageCode('zh-tw', lowerLocalesMapping)).toEqual('zh-Hant'); | ||
expect(fixLanguageCode('zh-hk', lowerLocalesMapping)).toEqual('zh-Hant'); | ||
expect(fixLanguageCode('zh-mo', lowerLocalesMapping)).toEqual('zh-Hant'); | ||
expect(fixLanguageCode('zh-hant-tw', lowerLocalesMapping)).toEqual('zh-Hant'); | ||
}); | ||
|
||
it('should return en if the given language code is not known to CLDR', function() { | ||
expect(fixLanguageCode('not-real', lowerLocalesMapping)).toEqual('en'); | ||
}); | ||
|
||
it('should return correct-casing any given cased language code', function() { | ||
expect(fixLanguageCode('en-gb', lowerLocalesMapping)).toEqual('en-GB'); | ||
expect(fixLanguageCode('en-GB', lowerLocalesMapping)).toEqual('en-GB'); | ||
expect(fixLanguageCode('EN-GB', lowerLocalesMapping)).toEqual('en-GB'); | ||
describe('Globalization', function() { | ||
// globalization functionality (e.g. number formatting) is tested in utils-spec | ||
it('should be returned', function() { | ||
expect(Globalization).toBeDefined(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* In order to localize numbers and dates, the language codes must made those in | ||
* CLDR. This module standardizes language code argument so that the | ||
* correct language settings are loaded. | ||
*/ | ||
define([ | ||
'json!cldr-data/availableLocales.json' // eslint-disable-line import/no-unresolved | ||
], function(availableLocales) { | ||
'use strict'; | ||
|
||
var lowerLocalesMapping = {}; | ||
availableLocales.availableLocales.forEach(function(locale) { | ||
lowerLocalesMapping[locale.toLowerCase()] = locale; | ||
}); | ||
|
||
return function(languageCode) { | ||
var fixedLanguageCode; | ||
|
||
if (!languageCode || typeof languageCode !== 'string') { | ||
return 'en'; | ||
} | ||
|
||
fixedLanguageCode = languageCode.toLowerCase(); | ||
|
||
// CLDR uses zh for Simplified Chinese, while Django may use different strings. | ||
if (fixedLanguageCode === 'zh-cn' || fixedLanguageCode === 'zh-sg' || | ||
fixedLanguageCode.indexOf('zh-hans') === 0) { | ||
fixedLanguageCode = 'zh'; | ||
} | ||
// CLDR uses zh-hant for Traditional Chinese, while Django may use different strings. | ||
if (fixedLanguageCode === 'zh-tw' || fixedLanguageCode === 'zh-hk' || | ||
fixedLanguageCode === 'zh-mo' || fixedLanguageCode.indexOf('zh-hant') === 0) { | ||
fixedLanguageCode = 'zh-hant'; | ||
} | ||
|
||
// There doesn't seem to be an onFailure event for the text! plugin. Make sure we only pass valid language codes | ||
// so the plugin does not attempt to load non-existent files. | ||
if (fixedLanguageCode in lowerLocalesMapping) { | ||
return lowerLocalesMapping[fixedLanguageCode]; | ||
} | ||
|
||
return 'en'; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters