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

Commit

Permalink
feat(directive): introduce a global keepContent setting
Browse files Browse the repository at this point in the history
  • Loading branch information
timfjord authored and knalli committed May 28, 2016
1 parent 00b2a82 commit 2015f79
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/directive/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function translateDirective($translate, $q, $interpolate, $compile, $parse, $roo
}
if (translateAttr === 'translate') {
// default translate into innerHTML
if (successful || (!successful && typeof iAttr.translateKeepContent === 'undefined')) {
if (successful || (!successful && !$translate.isKeepContent() && typeof iAttr.translateKeepContent === 'undefined')) {
iElement.empty().append(scope.preText + value + scope.postText);
}
var globallyEnabled = $translate.isPostCompilingEnabled();
Expand Down
38 changes: 38 additions & 0 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
$forceAsyncReloadEnabled = false,
$nestedObjectDelimeter = '.',
$isReady = false,
$keepContent = false,
loaderCache,
directivePriority = 0,
statefulFilter = true,
Expand Down Expand Up @@ -992,6 +993,29 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
return this;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#keepContent
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* If keepContent is set to true than translate directive will always use innerHTML
* as a default translation
*
* Example:
* <pre>
* app.config(function ($translateProvider) {
* $translateProvider.keepContent(true);
* });
* </pre>
*
* @param {boolean} value - valid values are true or false
*/
this.keepContent = function (value) {
$keepContent = !(!value);
return this;
};

/**
* @ngdoc object
* @name pascalprecht.translate.$translate
Expand Down Expand Up @@ -1951,6 +1975,20 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
return $forceAsyncReloadEnabled;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translate#isKeepContent
* @methodOf pascalprecht.translate.$translate
*
* @description
* Returns whether keepContent or not
*
* @return {boolean} keepContent value
*/
$translate.isKeepContent = function () {
return $keepContent;
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translate#refresh
Expand Down
25 changes: 25 additions & 0 deletions test/unit/directive/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,4 +852,29 @@ describe('pascalprecht.translate', function () {
expect(element.html()).toBe('Ich bin deutsch');
});
});

describe('set keepContent globally', function () {

var $compile, $rootScope, element;

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider
.translations('en', {
'HELLO': "Hello"
})
.keepContent(true)
.preferredLanguage('en');
}));

beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));

it('should use leave the inner content without change', function () {
element = $compile('<div translate="UNKNOWN">My content</divtranslate>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('My content');
});
});
});

0 comments on commit 2015f79

Please sign in to comment.