Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
feat(service): format bcp47 with script and language only correctly
Browse files Browse the repository at this point in the history
PR #1815
Solves #1811
  • Loading branch information
tenjaa authored and knalli committed Dec 21, 2017
1 parent 776c90a commit 6c3b63e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,29 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
bcp47 : function (tag) {
var temp = (tag || '').split('_').join('-');
var parts = temp.split('-');
return parts.length > 1 ? (parts[0].toLowerCase() + '-' + parts[1].toUpperCase()) : temp;

switch (parts.length) {
case 1: // language only
parts[0] = parts[0].toLowerCase();
break;
case 2: // language-script or language-region
parts[0] = parts[0].toLowerCase();
if (parts[1].length === 4) { // parts[1] is script
parts[1] = parts[1].charAt(0).toUpperCase() + parts[1].slice(1).toLowerCase();
} else { // parts[1] is region
parts[1] = parts[1].toUpperCase();
}
break;
case 3: // language-script-region
parts[0] = parts[0].toLowerCase();
parts[1] = parts[1].charAt(0).toUpperCase() + parts[1].slice(1).toLowerCase();
parts[2] = parts[2].toUpperCase();
break;
default:
return temp;
}

return parts.join('-');
},
'iso639-1' : function (tag) {
var temp = (tag || '').split('_').join('-');
Expand Down Expand Up @@ -825,9 +847,12 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
* en_US => en_US
* en-us => en_US
* * BCP 47 (RFC 4646 & 4647)
* EN => en
* en-US => en-US
* en_US => en-US
* en-us => en-US
* sr-latn => sr-Latn
* sr-latn-rs => sr-Latn-RS
*
* See also:
* * http://en.wikipedia.org/wiki/IETF_language_tag
Expand Down
42 changes: 42 additions & 0 deletions test/unit/service/translate.determineLocale.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,20 @@ describe('pascalprecht.translate', function () {
});

describe('using resolver "bcp47"', function () {
describe('should resolve to EN to en', function () {
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide, pascalprechtTranslateOverrider) {
pascalprechtTranslateOverrider.getLocale = function () {
return 'EN';
};
$translateProvider
.uniformLanguageTag('bcp47')
.determinePreferredLanguage();
}));
it('test', inject(function ($window, $translate) {
expect($translate.use()).toEqual('en');
}));
});

describe('should resolve to en-US to en-US', function () {
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide, pascalprechtTranslateOverrider) {
pascalprechtTranslateOverrider.getLocale = function () {
Expand Down Expand Up @@ -428,6 +442,34 @@ describe('pascalprecht.translate', function () {
expect($translate.use()).toEqual('en');
}));
});

describe('should resolve script without region', function () {
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide, pascalprechtTranslateOverrider) {
pascalprechtTranslateOverrider.getLocale = function () {
return 'sr-latn';
};
$translateProvider
.uniformLanguageTag('bcp47')
.determinePreferredLanguage();
}));
it('test', inject(function ($window, $translate) {
expect($translate.use()).toEqual('sr-Latn');
}));
});

describe('should resolve script with region', function () {
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide, pascalprechtTranslateOverrider) {
pascalprechtTranslateOverrider.getLocale = function () {
return 'sr-latn-rs';
};
$translateProvider
.uniformLanguageTag('bcp47')
.determinePreferredLanguage();
}));
it('test', inject(function ($window, $translate) {
expect($translate.use()).toEqual('sr-Latn-RS');
}));
});
});

describe('using resolver "iso639-1"', function () {
Expand Down

0 comments on commit 6c3b63e

Please sign in to comment.