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

Commit

Permalink
feat(service): add option to customize the nested delimiter
Browse files Browse the repository at this point in the history
```js
// will change the default '.' to '_'
$translateProvider.nestedObjectDelimeter('_');
```

Relates #1171
  • Loading branch information
Caleb Amsden authored and knalli committed Aug 30, 2015
1 parent aeff411 commit 78161f8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
$notFoundIndicatorRight,
$postCompilingEnabled = false,
$forceAsyncReloadEnabled = false,
NESTED_OBJECT_DELIMITER = '.',
$nestedObjectDelimeter = '.',
loaderCache,
directivePriority = 0,
statefulFilter = true,
Expand Down Expand Up @@ -261,6 +261,26 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
return this;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#nestedObjectDelimeter
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
*
* Let's you change the delimiter for namespaced translations.
* Default delimiter is `.`.
*
* @param {string} delimiter namespace separator
*/
this.nestedObjectDelimeter = function (delimiter) {
if (!delimiter) {
return $nestedObjectDelimeter;
}
$nestedObjectDelimeter = delimiter;
return this;
};

/**
* @name flatObject
* @private
Expand All @@ -286,10 +306,10 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
if (angular.isObject(val)) {
flatObject(val, path.concat(key), result, key);
} else {
keyWithPath = path.length ? ('' + path.join(NESTED_OBJECT_DELIMITER) + NESTED_OBJECT_DELIMITER + key) : key;
keyWithPath = path.length ? ('' + path.join($nestedObjectDelimeter) + $nestedObjectDelimeter + key) : key;
if(path.length && key === prevKey){
// Create shortcut path (foo.bar == foo.bar.bar)
keyWithShortPath = '' + path.join(NESTED_OBJECT_DELIMITER);
keyWithShortPath = '' + path.join($nestedObjectDelimeter);
// Link it to original path
result[keyWithShortPath] = '@:' + keyWithPath;
}
Expand Down Expand Up @@ -1558,6 +1578,20 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
return $cloakClassName;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translate#nestedObjectDelimeter
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns the configured delimiter for nested namespaces.
*
* @return {string} nestedObjectDelimeter
*/
$translate.nestedObjectDelimeter = function () {
return $nestedObjectDelimeter;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translate#fallbackLanguage
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 @@ -123,6 +123,17 @@ describe('pascalprecht.translate', function () {
});
});

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

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

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

it('should return a promise', function () {
expect($translate('FOO').then).toBeDefined();
expect(typeof $translate('FOO').then).toEqual('function');
Expand Down Expand Up @@ -304,6 +315,23 @@ describe('pascalprecht.translate', function () {
});
});

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

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider.nestedObjectDelimeter('_');
}));

var $translate;

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

it('should return \'_\' if such delimiter is specified', function () {
expect($translate.nestedObjectDelimeter()).toEqual('_');
});
});

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

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

1 comment on commit 78161f8

@piotrd
Copy link

@piotrd piotrd commented on 78161f8 Nov 23, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knalli @ca136 Noticed by accident: there's a typo in all occurrences of 'delimeter'. Should be 'delimiter'.

Please sign in to comment.