Skip to content

Commit

Permalink
feat(service): add allowNamespace parameter angular-translate#1910
Browse files Browse the repository at this point in the history
If not used, disabling namespaces can improve performance
  • Loading branch information
frenautvh authored and knalli committed Sep 2, 2021
1 parent 81ce733 commit 443a81f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
$notFoundIndicatorRight,
$postCompilingEnabled = false,
$forceAsyncReloadEnabled = false,
$allowNamespaces = true,
$nestedObjectDelimeter = '.',
$isReady = false,
$keepContent = false,
Expand Down Expand Up @@ -310,6 +311,27 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
return this;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#allowNamespaces
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Let's you disable namespaces, if you don't need nested translation
* object files. Disabling namespaces can improve performance.
*
* Default value is `true`. Namespaces are enabled by default.
*
* @param {boolean} namespacesEnabled - namespaces are enabled or not
*/
this.allowNamespaces = function (namespacesEnabled) {
if (namespacesEnabled === undefined) {
return $allowNamespaces;
}
$allowNamespaces = namespacesEnabled;
return this;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#nestedObjectDelimeter
Expand Down Expand Up @@ -347,6 +369,9 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
if (!result) {
result = {};
}
if (!$allowNamespaces) {
return data;
}
for (key in data) {
if (!Object.prototype.hasOwnProperty.call(data, key)) {
continue;
Expand Down Expand Up @@ -1761,6 +1786,20 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
return $cloakClassName;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translate#allowNamespaces
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns if namespaces are enabled
*
* @return {boolean} allowNamespaces value
*/
$translate.allowNamespaces = function () {
return $allowNamespaces;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translate#nestedObjectDelimeter
Expand Down
28 changes: 28 additions & 0 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translate#allowNamespaces()', function () {

it('should be a function', function () {
expect(typeof $translate.allowNamespaces).toBe('function');
});

it('should return \'true\' if no value is specified', function () {
expect($translate.allowNamespaces()).toEqual(true);
});
});

describe('$translate#nestedObjectDelimeter()', function () {

it('should be a function', function () {
Expand Down Expand Up @@ -472,6 +483,23 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translate#allowNamespaces()', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider.allowNamespaces(false);
}));

var $translate;

beforeEach(inject(function (_$translate_) {
$translate = _$translate_;
}));

it('should return \'false\' if namespaces were disabled', function () {
expect($translate.allowNamespaces()).toEqual(false);
});
});

describe('$translate#nestedObjectDelimeter()', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
Expand Down

0 comments on commit 443a81f

Please sign in to comment.