Skip to content

Commit

Permalink
fix: 🐛 replace missing string with empty string
Browse files Browse the repository at this point in the history
Notify the user of what string is missing via the console rather than
the UI.

BREAKING CHANGE: MISSING string will no longer be displayed.

Closes: #7063
  • Loading branch information
Portugal authored and mportuga committed Sep 27, 2020
1 parent 4a63c5e commit f7d48ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
10 changes: 7 additions & 3 deletions packages/core/src/js/services/ui-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
}
};

function getMissingText(path) {
$log.warn(i18nConstants.MISSING + path);
return '';
}

var service = {

/**
Expand Down Expand Up @@ -174,14 +179,13 @@
getSafeText: function (path, lang) {
var language = lang || service.getCurrentLang(),
trans = langCache.get(language),
missing = i18nConstants.MISSING + path,
getter = $parse(path);

if (!trans) {
return missing;
return getMissingText(path);
}

return getter(trans) || missing;
return getter(trans) || getMissingText(path);
},

/**
Expand Down
18 changes: 9 additions & 9 deletions packages/core/test/i18n/directives.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ describe('i18n Directives', function() {

expect(element.find('p').text()).toBe('Search...');
});
it('should get missing text for missing property', function() {
it('should get empty string for missing property', function() {
element = angular.element('<div ui-i18n="en"><p ui-translate="search.bad.text"></p></div>');
recompile();

expect(element.find('p').text()).toBe('[MISSING]search.bad.text');
expect(element.find('p').text()).toBe('');
});
});

Expand Down Expand Up @@ -98,15 +98,15 @@ describe('i18n Directives', function() {

expect(element.find('p').text()).toBe('Search...');
});
it('should get missing text for missing property', function() {
it('should get empty string for missing property', function() {
element = angular.element('<div ui-i18n="en"><p ui-t="search.bad.text"></p></div>');
recompile();

expect(element.find('p').text()).toBe('[MISSING]search.bad.text');
expect(element.find('p').text()).toBe('');

$rootScope.$broadcast('$uiI18n');

expect(element.find('p').text()).toBe('[MISSING]search.bad.text');
expect(element.find('p').text()).toBe('');
});
});

Expand All @@ -120,11 +120,11 @@ describe('i18n Directives', function() {

expect(element.find('p').text()).toBe('Search...');
});
it('should get missing text for missing property', function() {
it('should get empty string for missing property', function() {
element = angular.element('<div ui-i18n="en"><p>{{"search.bad.text" | t}}</p></div>');
recompile();

expect(element.find('p').text()).toBe('[MISSING]search.bad.text');
expect(element.find('p').text()).toBe('');
});
});

Expand All @@ -144,11 +144,11 @@ describe('i18n Directives', function() {

expect(element.find('p').text()).toBe('Search...');
});
it('should get missing text for missing property', function() {
it('should get empty string for missing property', function() {
element = angular.element('<div ui-i18n="en"><p>{{"search.bad.text" | uiTranslate}}</p></div>');
recompile();

expect(element.find('p').text()).toBe('[MISSING]search.bad.text');
expect(element.find('p').text()).toBe('');
});
});
});
30 changes: 20 additions & 10 deletions packages/core/test/i18n/i18nService.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
describe('i18nService', function () {
var i18nConstants, i18nService;
var $log, i18nConstants, i18nService;

beforeEach(function() {
module('ui.grid');
$log = jasmine.createSpyObj('$log', ['warn']);
module('ui.grid', function($provide) {
$provide.value('$log', $log)
});
inject(function (_i18nConstants_, _i18nService_) {
i18nConstants = _i18nConstants_;
i18nService = _i18nService_;
});
});
afterEach(function() {
$log.warn.calls.reset();
});

describe('i18n service', function() {
it('should default to English', function() {
Expand Down Expand Up @@ -77,30 +83,34 @@ describe('i18nService', function () {
it('should get safe text when text is defined', function() {
expect(i18nService.getSafeText('search.placeholder')).toBe('Search...');
});
it('should get missing text for missing property', function() {
it('should get empty string for missing property', function() {
var badText = 'search.bad.text';

expect(i18nService.getSafeText(badText)).toBe(i18nConstants.MISSING + badText);
expect(i18nService.getSafeText(badText)).toBe('');
expect($log.warn).toHaveBeenCalledWith(i18nConstants.MISSING + badText);
});
it('should get fallback text when language is missing or nonexistent', function() {
expect(i18nService.getSafeText('search.placeholder', 'valerian')).toBe('Search...');
});
it('should get missing text when language is missing or nonexistent and there is no fallback', function() {
it('should get empty string when language is missing or nonexistent and there is no fallback', function() {
var badText = 'bad.text';

expect(i18nService.getSafeText(badText, 'valerian')).toBe(i18nConstants.MISSING + badText);
expect(i18nService.getSafeText(badText, 'valerian')).toBe('');
expect($log.warn).toHaveBeenCalledWith(i18nConstants.MISSING + badText);
});
it('should get missing text when language is missing or nonexistent and the fallback language is the same', function() {
it('should get empty string when language is missing or nonexistent and the fallback language is the same', function() {
var missingProperty = 'search.placeholder';

i18nService.setFallbackLang('valerian');
expect(i18nService.getSafeText(missingProperty, 'valerian')).toBe(i18nConstants.MISSING + missingProperty);
expect(i18nService.getSafeText(missingProperty, 'valerian')).toBe('');
expect($log.warn).toHaveBeenCalledWith(i18nConstants.MISSING + missingProperty);
});
it('should get missing text when language is missing or nonexistent and the fallback language is also missing it', function() {
it('should get empty string when language is missing or nonexistent and the fallback language is also missing it', function() {
var missingProperty = 'search.placeholder';

i18nService.setFallbackLang('orcish');
expect(i18nService.getSafeText(missingProperty, 'valerian')).toBe(i18nConstants.MISSING + missingProperty);
expect(i18nService.getSafeText(missingProperty, 'valerian')).toBe('');
expect($log.warn).toHaveBeenCalledWith(i18nConstants.MISSING + missingProperty);
});
});
});
Expand Down

0 comments on commit f7d48ee

Please sign in to comment.