Skip to content

Commit

Permalink
Merge 5fb2abb into 0c45eea
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffharcourt committed Mar 28, 2020
2 parents 0c45eea + 5fb2abb commit 5307769
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 11 additions & 4 deletions app/assets/javascripts/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@
// Set missing translation behavior. 'message' will display a message
// that the translation is missing, 'guess' will try to guess the string
, missingBehaviour: 'message'
// Set a callback to be called with translations are missing. Can be used to
// report missing translations to exception tracking
, missingCallback: undefined
// if you use missingBehaviour with 'message', but want to know that the
// string is actually missing for testing purposes, you can prefix the
// guessed string by setting the value here. By default, no prefix!
Expand Down Expand Up @@ -673,6 +676,14 @@

// Return a missing translation message for the given parameters.
I18n.missingTranslation = function(scope, options) {
var localeForTranslation = (options != null && options.locale != null) ? options.locale : this.currentLocale();
var fullScope = this.getFullScope(scope, options);
var fullScopeWithLocale = [localeForTranslation, fullScope].join(this.defaultSeparator);

if (this.missingCallback) {
this.missingCallback(fullScopeWithLocale);
}

//guess intended string
if(this.missingBehaviour === 'guess'){
//get only the last portion of the scope
Expand All @@ -683,10 +694,6 @@
function(match, p1, p2) {return p1 + ' ' + p2.toLowerCase()} );
}

var localeForTranslation = (options != null && options.locale != null) ? options.locale : this.currentLocale();
var fullScope = this.getFullScope(scope, options);
var fullScopeWithLocale = [localeForTranslation, fullScope].join(this.defaultSeparator);

return '[missing "' + fullScopeWithLocale + '" translation]';
};

Expand Down
8 changes: 8 additions & 0 deletions spec/js/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ describe("Translate", function(){
expect(actual).toEqual(expected);
});

it("calls the missing callback for missing translation when a callback is defined", function(){
var called = false;
callback = function() { called = true };
I18n.missingCallback = callback;
I18n.translate("monster", {scope: "greetings"});
expect(called).toBe(true);
});

it("returns translation for single scope on a custom locale", function(){
I18n.locale = "pt-BR";
expect(I18n.translate("hello")).toEqual("Olá Mundo!");
Expand Down

0 comments on commit 5307769

Please sign in to comment.