Skip to content

Commit 555e1f0

Browse files
committed
Add messageFallback property
1 parent 0512672 commit 555e1f0

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

dist/lang.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lang.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
this.locale = options.locale || inferLocale() || defaults.locale;
5757
this.fallback = options.fallback;
5858
this.messages = options.messages;
59+
this.messageFallback = options.messageFallback;
5960
};
6061

6162
// Methods //
@@ -111,6 +112,26 @@
111112
this.fallback = fallback;
112113
};
113114

115+
/**
116+
* Get the message fallback closure.
117+
*
118+
* @return closure|null
119+
*/
120+
Lang.prototype.getMessageFallback = function() {
121+
return this.messageFallback;
122+
};
123+
124+
/**
125+
* Set the message fallback closure.
126+
*
127+
* @param messageFallback {string} The messageFallback closure.
128+
*
129+
* @return void
130+
*/
131+
Lang.prototype.setMessageFallback = function(messageFallback) {
132+
this.messageFallback = messageFallback;
133+
};
134+
114135
/**
115136
* This method act as an alias to get() method.
116137
*
@@ -138,12 +159,12 @@
138159
*/
139160
Lang.prototype.get = function(key, replacements, locale) {
140161
if (!this.has(key)) {
141-
return key;
162+
return this.printKey(key);
142163
}
143164

144165
var message = this._getMessage(key, locale);
145166
if (message === null) {
146-
return key;
167+
return this.printKey(key);
147168
}
148169

149170
if (replacements) {
@@ -153,6 +174,21 @@
153174
return message;
154175
};
155176

177+
/**
178+
* Call message fallback and return key
179+
*
180+
* @param key {string} The key of the message.
181+
*
182+
* @return {string} The given key.
183+
*/
184+
Lang.prototype.printKey = function(key){
185+
if(this.messageFallback !== undefined) {
186+
this.messageFallback(key);
187+
}
188+
189+
return key;
190+
}
191+
156192
/**
157193
* This method act as an alias to get() method.
158194
*

test/spec/lang_get_spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ describe('The lang.get() method', function () {
2626
expect(lang.get(null)).toBe(null);
2727
});
2828

29+
it('should call massage fallback closure and return the passed key when not found', function () {
30+
var test = '';
31+
lang.setMessageFallback(function(key){ test = key });
32+
expect(lang.get('foo.bar')).toBe('foo.bar');
33+
expect(test).toBe('foo.bar');
34+
});
35+
2936
it('should return the expected message', function () {
3037
expect(lang.get('messages.home')).toBe('Home');
3138
});

0 commit comments

Comments
 (0)