Skip to content

Commit 7b2da37

Browse files
authored
Merge pull request #1793 from BladeRunnerJS/additional-check-to-suppress-i18n-warnings
Additional check to suppress i18n warnings
2 parents 4ec1949 + f89102f commit 7b2da37

File tree

1 file changed

+149
-122
lines changed

1 file changed

+149
-122
lines changed

brjs-sdk/sdk/libs/javascript/br-i18n/src/br/i18n/Translator.js

Lines changed: 149 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* @module br/i18n/Translator
55
*/
66

7-
var Errors = require('br/Errors');
8-
var LocalisedNumber = require('./LocalisedNumber');
9-
var fell = require('fell');
10-
var log = fell.getLogger('br.i18n.Translator');
11-
var I18nStore = require('./I18nStore');
7+
var Errors = require("br/Errors");
8+
var LocalisedNumber = require("./LocalisedNumber");
9+
var fell = require("fell");
10+
var log = fell.getLogger("br.i18n.Translator");
11+
var I18nStore = require("./I18nStore");
1212
// LocalisedDate and LocalisedTime use br/i18n which depends on this class,
1313
// so they have to be required where they are used or there would be a circular
1414
// dependency.
@@ -17,7 +17,6 @@ var regExp = /\@\{(.*?)\}/m;
1717
var TEST_DATE_FORMAT_SHORT = "d-m-Y";
1818
var TEST_DATE_FORMAT_LONG = "D, d M, Y, h:i:s A";
1919

20-
2120
/**
2221
* @class
2322
* @alias module:br/i18n/Translator
@@ -33,20 +32,23 @@ var TEST_DATE_FORMAT_LONG = "D, d M, Y, h:i:s A";
3332
* For example <code>require("br/i18n").i18n("some.i18n.key")</code>.</p>
3433
*/
3534
function Translator(messageDefinitions, useLocale) {
36-
var defaultLocale = Object.keys(require('service!br.app-meta-service').getLocales())[0];
35+
var defaultLocale = Object.keys(
36+
require("service!br.app-meta-service").getLocales()
37+
)[0];
3738

38-
/** @private */
39-
this.localizationPrefs = {};
39+
/** @private */
40+
this.localizationPrefs = {};
4041

41-
I18nStore.initialize(messageDefinitions, useLocale || 'en', defaultLocale);
42+
I18nStore.initialize(messageDefinitions, useLocale || "en", defaultLocale);
4243
}
4344

4445
Translator.MESSAGES = {
45-
UNTRANSLATED_TOKEN_LOG_MSG: 'A translation has not been provided for the i18n key "{0}" in the "{1}" locale'
46+
UNTRANSLATED_TOKEN_LOG_MSG:
47+
'A translation has not been provided for the i18n key "{0}" in the "{1}" locale'
4648
};
4749

4850
Translator.prototype.setLocale = function(locale) {
49-
I18nStore.locale = locale;
51+
I18nStore.locale = locale;
5052
};
5153

5254
/**
@@ -67,18 +69,18 @@ Translator.prototype.setLocale = function(locale) {
6769
* messages.
6870
*/
6971
Translator.prototype.translate = function(text, type) {
70-
var message;
71-
var match = regExp.exec(text);
72-
type = type || "xml";
73-
while (match) {
74-
message = this._getTranslationForKey(match[1]);
75-
if (type == "xml") {
76-
message = this.convertXMLEntityChars(message);
77-
}
78-
text = text.replace(match[0], message);
79-
match = regExp.exec(text);
80-
}
81-
return text;
72+
var message;
73+
var match = regExp.exec(text);
74+
type = type || "xml";
75+
while (match) {
76+
message = this._getTranslationForKey(match[1]);
77+
if (type == "xml") {
78+
message = this.convertXMLEntityChars(message);
79+
}
80+
text = text.replace(match[0], message);
81+
match = regExp.exec(text);
82+
}
83+
return text;
8284
};
8385

8486
/**
@@ -92,7 +94,7 @@ Translator.prototype.translate = function(text, type) {
9294
* translation set, otherwise <code>false</code>.
9395
*/
9496
Translator.prototype.tokenExists = function(token) {
95-
return I18nStore.tokenExists(token);
97+
return I18nStore.tokenExists(token);
9698
};
9799

98100
/**
@@ -104,13 +106,13 @@ Translator.prototype.tokenExists = function(token) {
104106
* corresponding XML entity reference.
105107
*/
106108
Translator.prototype.convertXMLEntityChars = function(text) {
107-
text = text.replace(/&/g, "&amp;");
108-
text = text.replace(/</g, "&lt;");
109-
text = text.replace(/>/g, "&gt;");
110-
text = text.replace(/\"/g, "&quot;");
111-
text = text.replace(/\'/g, "&apos;");
109+
text = text.replace(/&/g, "&amp;");
110+
text = text.replace(/</g, "&lt;");
111+
text = text.replace(/>/g, "&gt;");
112+
text = text.replace(/\"/g, "&quot;");
113+
text = text.replace(/\'/g, "&apos;");
112114

113-
return text;
115+
return text;
114116
};
115117

116118
/**
@@ -133,15 +135,15 @@ Translator.prototype.convertXMLEntityChars = function(text) {
133135
* arguments.
134136
*/
135137
Translator.prototype.getMessage = function(token, templateArgs) {
136-
templateArgs = templateArgs || {};
137-
var text = this._getTranslationForKeyOrUndefined(token);
138-
if (text != null) {
139-
for (var key in templateArgs) {
140-
var regEx = new RegExp("\\[" + key + "\\]", "g");
141-
text = text.replace(regEx, templateArgs[key]);
142-
}
143-
}
144-
return formatTranslationResponseIfTranslationWasUnknown(token, text);
138+
templateArgs = templateArgs || {};
139+
var text = this._getTranslationForKeyOrUndefined(token);
140+
if (text != null) {
141+
for (var key in templateArgs) {
142+
var regEx = new RegExp("\\[" + key + "\\]", "g");
143+
text = text.replace(regEx, templateArgs[key]);
144+
}
145+
}
146+
return formatTranslationResponseIfTranslationWasUnknown(token, text);
145147
};
146148

147149
/**
@@ -155,7 +157,10 @@ Translator.prototype.getMessage = function(token, templateArgs) {
155157
* @returns The date format string, e.g. YYYY-mm-dd.
156158
*/
157159
Translator.prototype.getDateFormat = function() {
158-
return this.localizationPrefs.dateFormat || this._getTranslationForKey("br.i18n.date.format");
160+
return (
161+
this.localizationPrefs.dateFormat ||
162+
this._getTranslationForKey("br.i18n.date.format")
163+
);
159164
};
160165

161166
/**
@@ -169,7 +174,10 @@ Translator.prototype.getDateFormat = function() {
169174
* @returns The date format string, e.g. d/m/Y.
170175
*/
171176
Translator.prototype.getShortDateFormat = function() {
172-
return this.localizationPrefs.shortDateFormat || this._getTranslationForKey("br.i18n.date.format.typed");
177+
return (
178+
this.localizationPrefs.shortDateFormat ||
179+
this._getTranslationForKey("br.i18n.date.format.typed")
180+
);
173181
};
174182

175183
/**
@@ -205,12 +213,12 @@ Translator.prototype.getShortDateFormat = function() {
205213
* @returns The formatted date string.
206214
*/
207215
Translator.prototype.formatDate = function(date, dateFormat) {
208-
if (!dateFormat) {
209-
dateFormat = this.getDateFormat();
210-
}
216+
if (!dateFormat) {
217+
dateFormat = this.getDateFormat();
218+
}
211219

212-
var localisedDate = new (require('./LocalisedDate'))(date);
213-
return localisedDate.format(dateFormat);
220+
var localisedDate = new (require("./LocalisedDate"))(date);
221+
return localisedDate.format(dateFormat);
214222
};
215223

216224
/**
@@ -234,8 +242,8 @@ Translator.prototype.formatDate = function(date, dateFormat) {
234242
* @type String
235243
*/
236244
Translator.prototype.formatTime = function(time) {
237-
var localisedTime = new (require('./LocalisedTime'))(time);
238-
return localisedTime.format();
245+
var localisedTime = new (require("./LocalisedTime"))(time);
246+
return localisedTime.format();
239247
};
240248

241249
/**
@@ -259,15 +267,17 @@ Translator.prototype.formatTime = function(time) {
259267
* @type String
260268
*/
261269
Translator.prototype.formatNumber = function(number, thousandsSeparator) {
262-
var localisedNumber = new LocalisedNumber(number);
263-
if (!thousandsSeparator) {
264-
thousandsSeparator = this.localizationPrefs.thousandsSeparator ||
265-
this._getTranslationForKey("br.i18n.number.grouping.separator");
266-
}
267-
var decimalRadixCharacter = this.localizationPrefs.decimalRadixCharacter ||
268-
this._getTranslationForKey("br.i18n.decimal.radix.character");
269-
270-
return localisedNumber.format(thousandsSeparator, decimalRadixCharacter);
270+
var localisedNumber = new LocalisedNumber(number);
271+
if (!thousandsSeparator) {
272+
thousandsSeparator =
273+
this.localizationPrefs.thousandsSeparator ||
274+
this._getTranslationForKey("br.i18n.number.grouping.separator");
275+
}
276+
var decimalRadixCharacter =
277+
this.localizationPrefs.decimalRadixCharacter ||
278+
this._getTranslationForKey("br.i18n.decimal.radix.character");
279+
280+
return localisedNumber.format(thousandsSeparator, decimalRadixCharacter);
271281
};
272282

273283
/**
@@ -288,30 +298,35 @@ Translator.prototype.formatNumber = function(number, thousandsSeparator) {
288298
* @returns {Number} A parsed number or null if the value can't be parsed.
289299
*/
290300
Translator.prototype.parseNumber = function(number, thousandsSeparator) {
291-
if (!thousandsSeparator) {
292-
thousandsSeparator = this.localizationPrefs.thousandsSeparator ||
293-
this._getTranslationForKey('br.i18n.number.grouping.separator');
294-
}
295-
296-
var decimalPlaceCharacter = this.localizationPrefs.decimalRadixCharacter ||
297-
this._getTranslationForKey("br.i18n.decimal.radix.character");
298-
299-
thousandsSeparator = thousandsSeparator.replace(/[-[\]*+?.,\\^$|#\s]/g, "\\$&");
300-
var regEx = new RegExp(thousandsSeparator, "g");
301-
number = number.replace(regEx, '');
302-
number = number.replace(decimalPlaceCharacter, '.');
303-
304-
var numberLength = number.length;
305-
306-
if (number[numberLength - 1] === decimalPlaceCharacter) {
307-
number = number.substr(0, numberLength - 1);
308-
}
309-
310-
if (isNaN(number)) {
311-
return null;
312-
}
313-
314-
return Number(number);
301+
if (!thousandsSeparator) {
302+
thousandsSeparator =
303+
this.localizationPrefs.thousandsSeparator ||
304+
this._getTranslationForKey("br.i18n.number.grouping.separator");
305+
}
306+
307+
var decimalPlaceCharacter =
308+
this.localizationPrefs.decimalRadixCharacter ||
309+
this._getTranslationForKey("br.i18n.decimal.radix.character");
310+
311+
thousandsSeparator = thousandsSeparator.replace(
312+
/[-[\]*+?.,\\^$|#\s]/g,
313+
"\\$&"
314+
);
315+
var regEx = new RegExp(thousandsSeparator, "g");
316+
number = number.replace(regEx, "");
317+
number = number.replace(decimalPlaceCharacter, ".");
318+
319+
var numberLength = number.length;
320+
321+
if (number[numberLength - 1] === decimalPlaceCharacter) {
322+
number = number.substr(0, numberLength - 1);
323+
}
324+
325+
if (isNaN(number)) {
326+
return null;
327+
}
328+
329+
return Number(number);
315330
};
316331

317332
/**
@@ -323,25 +338,27 @@ Translator.prototype.parseNumber = function(number, thousandsSeparator) {
323338
* @type String
324339
*/
325340
Translator.prototype.stripNonNumericCharacters = function(value) {
326-
var length = value.length;
327-
var joiner = [];
328-
var isDecimalPointFound = false;
329-
var decimalPlaceCharacter = this.localizationPrefs.decimalRadixCharacter || this._getTranslationForKey("br.i18n.decimal.radix.character");
330-
331-
for (var i = 0; i < length; i++) {
332-
var thisChar = value.charAt(i);
333-
if (isNaN(thisChar) === true) {
334-
if (thisChar === decimalPlaceCharacter) {
335-
if (isDecimalPointFound == false) {
336-
joiner.push(".");
337-
isDecimalPointFound = true;
338-
}
339-
}
340-
} else {
341-
joiner.push(thisChar);
342-
}
343-
}
344-
return joiner.join("");
341+
var length = value.length;
342+
var joiner = [];
343+
var isDecimalPointFound = false;
344+
var decimalPlaceCharacter =
345+
this.localizationPrefs.decimalRadixCharacter ||
346+
this._getTranslationForKey("br.i18n.decimal.radix.character");
347+
348+
for (var i = 0; i < length; i++) {
349+
var thisChar = value.charAt(i);
350+
if (isNaN(thisChar) === true) {
351+
if (thisChar === decimalPlaceCharacter) {
352+
if (isDecimalPointFound == false) {
353+
joiner.push(".");
354+
isDecimalPointFound = true;
355+
}
356+
}
357+
} else {
358+
joiner.push(thisChar);
359+
}
360+
}
361+
return joiner.join("");
345362
};
346363

347364
/**
@@ -350,38 +367,48 @@ Translator.prototype.stripNonNumericCharacters = function(value) {
350367
* @param {Map} localizationPrefs A map containing the localization preferences.
351368
*/
352369
Translator.prototype.setLocalizationPreferences = function(localizationPrefs) {
353-
this.localizationPrefs = localizationPrefs;
370+
this.localizationPrefs = localizationPrefs;
354371
};
355372

356373
/** @private */
357374
Translator.prototype._getTranslationForKey = function(token) {
358-
var text = this._getTranslationForKeyOrUndefined(token);
359-
return formatTranslationResponseIfTranslationWasUnknown(token, text);
375+
var text = this._getTranslationForKeyOrUndefined(token);
376+
return formatTranslationResponseIfTranslationWasUnknown(token, text);
360377
};
361378

362379
/** @private */
363380
Translator.prototype._getTranslationForKeyOrUndefined = function(token) {
364-
if (!this.tokenExists(token)) {
365-
var logConsole = (window.jstestdriver) ? jstestdriver.console : window.console;
366-
if (logConsole && logConsole.warn && !window.suppressI18nWarnings) {
367-
logConsole.warn('Unable to find a replacement for the i18n key "' + token + '"');
368-
}
369-
}
370-
371-
var message = I18nStore.getTranslation(token);
372-
373-
if (typeof message === 'undefined') {
374-
log.warn(Translator.MESSAGES.UNTRANSLATED_TOKEN_LOG_MSG, token, I18nStore.locale);
375-
if (!require('service!br.app-meta-service').isDev()) {
376-
message = I18nStore.getDefaultTranslation(token);
377-
}
378-
}
379-
380-
return message;
381+
if (!this.tokenExists(token)) {
382+
var logConsole = window.jstestdriver
383+
? jstestdriver.console
384+
: window.console;
385+
if (logConsole && logConsole.warn && !window.suppressI18nWarnings) {
386+
logConsole.warn(
387+
'Unable to find a replacement for the i18n key "' + token + '"'
388+
);
389+
}
390+
}
391+
392+
var message = I18nStore.getTranslation(token);
393+
394+
if (typeof message === "undefined") {
395+
if (!window.suppressI18nWarnings) {
396+
log.warn(
397+
Translator.MESSAGES.UNTRANSLATED_TOKEN_LOG_MSG,
398+
token,
399+
I18nStore.locale
400+
);
401+
}
402+
if (!require("service!br.app-meta-service").isDev()) {
403+
message = I18nStore.getDefaultTranslation(token);
404+
}
405+
}
406+
407+
return message;
381408
};
382409

383410
function formatTranslationResponseIfTranslationWasUnknown(key, text) {
384-
return (text) ? text : "??? " + key + " ???";
411+
return text ? text : "??? " + key + " ???";
385412
}
386413

387414
module.exports = Translator;

0 commit comments

Comments
 (0)