From 555e1f07e074f28679c58d2c19eb53c50b54da9f Mon Sep 17 00:00:00 2001 From: Egor Talantsev Date: Mon, 8 May 2017 23:54:34 +0500 Subject: [PATCH] Add messageFallback property --- dist/lang.min.js | 2 +- src/lang.js | 40 ++++++++++++++++++++++++++++++++++++-- test/spec/lang_get_spec.js | 7 +++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/dist/lang.min.js b/dist/lang.min.js index ef23fc4..23dc93e 100644 --- a/dist/lang.min.js +++ b/dist/lang.min.js @@ -6,4 +6,4 @@ * @site https://github.com/rmariuzzo/Lang.js * @author Rubens Mariuzzo */ -(function(root,factory){"use strict";if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof exports==="object"){module.exports=factory()}else{root.Lang=factory()}})(this,function(){"use strict";function inferLocale(){if(typeof document!=="undefined"&&document.documentElement){return document.documentElement.lang}}function convertNumber(str){if(str==="-Inf"){return-Infinity}else if(str==="+Inf"||str==="Inf"){return Infinity}return parseInt(str,10)}var intervalRegexp=/^({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([\[\]])\s*(-Inf|\-?\d+(\.\d+)?)\s*,\s*(\+?Inf|\-?\d+(\.\d+)?)\s*([\[\]])$/;var anyIntervalRegexp=/({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([\[\]])\s*(-Inf|\-?\d+(\.\d+)?)\s*,\s*(\+?Inf|\-?\d+(\.\d+)?)\s*([\[\]])/;var defaults={locale:"en"};var Lang=function(options){options=options||{};this.locale=options.locale||inferLocale()||defaults.locale;this.fallback=options.fallback;this.messages=options.messages};Lang.prototype.setMessages=function(messages){this.messages=messages};Lang.prototype.getLocale=function(){return this.locale||options.fallback};Lang.prototype.setLocale=function(locale){this.locale=locale};Lang.prototype.getFallback=function(){return this.fallback};Lang.prototype.setFallback=function(fallback){this.fallback=fallback};Lang.prototype.has=function(key,locale){if(typeof key!=="string"||!this.messages){return false}return this._getMessage(key,locale)!==null};Lang.prototype.get=function(key,replacements,locale){if(!this.has(key)){return key}var message=this._getMessage(key,locale);if(message===null){return key}if(replacements){message=this._applyReplacements(message,replacements)}return message};Lang.prototype.trans=function(key,replacements){return this.get(key,replacements)};Lang.prototype.choice=function(key,number,replacements,locale){replacements=typeof replacements!=="undefined"?replacements:{};replacements.count=number;var message=this.get(key,replacements,locale);if(message===null||message===undefined){return message}var messageParts=message.split("|");var explicitRules=[];for(var i=0;i=leftNumber:count>leftNumber)&&(rightDelimiter==="]"?count<=rightNumber:count=2&&count%10<=4&&(count%100<10||count%100>=20)?1:2;case"cs":case"sk":return count==1?0:count>=2&&count<=4?1:2;case"ga":return count==1?0:count==2?1:2;case"lt":return count%10==1&&count%100!=11?0:count%10>=2&&(count%100<10||count%100>=20)?1:2;case"sl":return count%100==1?0:count%100==2?1:count%100==3||count%100==4?2:3;case"mk":return count%10==1?0:1;case"mt":return count==1?0:count===0||count%100>1&&count%100<11?1:count%100>10&&count%100<20?2:3;case"lv":return count===0?0:count%10==1&&count%100!=11?1:2;case"pl":return count==1?0:count%10>=2&&count%10<=4&&(count%100<12||count%100>14)?1:2;case"cy":return count==1?0:count==2?1:count==8||count==11?2:3;case"ro":return count==1?0:count===0||count%100>0&&count%100<20?1:2;case"ar":return count===0?0:count==1?1:count==2?2:count%100>=3&&count%100<=10?3:count%100>=11&&count%100<=99?4:5;default:return 0}};return Lang}); \ No newline at end of file +(function(root,factory){"use strict";if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof exports==="object"){module.exports=factory()}else{root.Lang=factory()}})(this,function(){"use strict";function inferLocale(){if(typeof document!=="undefined"&&document.documentElement){return document.documentElement.lang}}function convertNumber(str){if(str==="-Inf"){return-Infinity}else if(str==="+Inf"||str==="Inf"){return Infinity}return parseInt(str,10)}var intervalRegexp=/^({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([\[\]])\s*(-Inf|\-?\d+(\.\d+)?)\s*,\s*(\+?Inf|\-?\d+(\.\d+)?)\s*([\[\]])$/;var anyIntervalRegexp=/({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([\[\]])\s*(-Inf|\-?\d+(\.\d+)?)\s*,\s*(\+?Inf|\-?\d+(\.\d+)?)\s*([\[\]])/;var defaults={locale:"en"};var Lang=function(options){options=options||{};this.locale=options.locale||inferLocale()||defaults.locale;this.fallback=options.fallback;this.messages=options.messages;this.messageFallback=options.messageFallback};Lang.prototype.setMessages=function(messages){this.messages=messages};Lang.prototype.getLocale=function(){return this.locale||options.fallback};Lang.prototype.setLocale=function(locale){this.locale=locale};Lang.prototype.getFallback=function(){return this.fallback};Lang.prototype.setFallback=function(fallback){this.fallback=fallback};Lang.prototype.getMessageFallback=function(){return this.messageFallback};Lang.prototype.setMessageFallback=function(messageFallback){this.messageFallback=messageFallback};Lang.prototype.has=function(key,locale){if(typeof key!=="string"||!this.messages){return false}return this._getMessage(key,locale)!==null};Lang.prototype.get=function(key,replacements,locale){if(!this.has(key)){return this.printKey(key)}var message=this._getMessage(key,locale);if(message===null){return this.printKey(key)}if(replacements){message=this._applyReplacements(message,replacements)}return message};Lang.prototype.printKey=function(key){if(this.messageFallback!==undefined){this.messageFallback(key)}return key};Lang.prototype.trans=function(key,replacements){return this.get(key,replacements)};Lang.prototype.choice=function(key,number,replacements,locale){replacements=typeof replacements!=="undefined"?replacements:{};replacements.count=number;var message=this.get(key,replacements,locale);if(message===null||message===undefined){return message}var messageParts=message.split("|");var explicitRules=[];for(var i=0;i=leftNumber:count>leftNumber)&&(rightDelimiter==="]"?count<=rightNumber:count=2&&count%10<=4&&(count%100<10||count%100>=20)?1:2;case"cs":case"sk":return count==1?0:count>=2&&count<=4?1:2;case"ga":return count==1?0:count==2?1:2;case"lt":return count%10==1&&count%100!=11?0:count%10>=2&&(count%100<10||count%100>=20)?1:2;case"sl":return count%100==1?0:count%100==2?1:count%100==3||count%100==4?2:3;case"mk":return count%10==1?0:1;case"mt":return count==1?0:count===0||count%100>1&&count%100<11?1:count%100>10&&count%100<20?2:3;case"lv":return count===0?0:count%10==1&&count%100!=11?1:2;case"pl":return count==1?0:count%10>=2&&count%10<=4&&(count%100<12||count%100>14)?1:2;case"cy":return count==1?0:count==2?1:count==8||count==11?2:3;case"ro":return count==1?0:count===0||count%100>0&&count%100<20?1:2;case"ar":return count===0?0:count==1?1:count==2?2:count%100>=3&&count%100<=10?3:count%100>=11&&count%100<=99?4:5;default:return 0}};return Lang}); \ No newline at end of file diff --git a/src/lang.js b/src/lang.js index ff92851..0469f12 100644 --- a/src/lang.js +++ b/src/lang.js @@ -56,6 +56,7 @@ this.locale = options.locale || inferLocale() || defaults.locale; this.fallback = options.fallback; this.messages = options.messages; + this.messageFallback = options.messageFallback; }; // Methods // @@ -111,6 +112,26 @@ this.fallback = fallback; }; + /** + * Get the message fallback closure. + * + * @return closure|null + */ + Lang.prototype.getMessageFallback = function() { + return this.messageFallback; + }; + + /** + * Set the message fallback closure. + * + * @param messageFallback {string} The messageFallback closure. + * + * @return void + */ + Lang.prototype.setMessageFallback = function(messageFallback) { + this.messageFallback = messageFallback; + }; + /** * This method act as an alias to get() method. * @@ -138,12 +159,12 @@ */ Lang.prototype.get = function(key, replacements, locale) { if (!this.has(key)) { - return key; + return this.printKey(key); } var message = this._getMessage(key, locale); if (message === null) { - return key; + return this.printKey(key); } if (replacements) { @@ -153,6 +174,21 @@ return message; }; + /** + * Call message fallback and return key + * + * @param key {string} The key of the message. + * + * @return {string} The given key. + */ + Lang.prototype.printKey = function(key){ + if(this.messageFallback !== undefined) { + this.messageFallback(key); + } + + return key; + } + /** * This method act as an alias to get() method. * diff --git a/test/spec/lang_get_spec.js b/test/spec/lang_get_spec.js index c447992..7ab0f03 100644 --- a/test/spec/lang_get_spec.js +++ b/test/spec/lang_get_spec.js @@ -26,6 +26,13 @@ describe('The lang.get() method', function () { expect(lang.get(null)).toBe(null); }); + it('should call massage fallback closure and return the passed key when not found', function () { + var test = ''; + lang.setMessageFallback(function(key){ test = key }); + expect(lang.get('foo.bar')).toBe('foo.bar'); + expect(test).toBe('foo.bar'); + }); + it('should return the expected message', function () { expect(lang.get('messages.home')).toBe('Home'); });