From d871bc40b864f64fe3291d8b8139abcab5b16f2f Mon Sep 17 00:00:00 2001 From: krzysztofmadejski Date: Sun, 6 Nov 2016 10:48:17 +0100 Subject: [PATCH] #459 - next iteration --- src/i18n/view.i18n.js | 66 +++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/src/i18n/view.i18n.js b/src/i18n/view.i18n.js index 20857fe8..c20d123d 100644 --- a/src/i18n/view.i18n.js +++ b/src/i18n/view.i18n.js @@ -1,48 +1,78 @@ /*jshint multistr:true */ +// todo probably some kind of mixin would be better +//usage: (zamienic na extension) +// my.View - Backbone.View.extend({}); +//_.extend(my.View, Backbone.I18nView); + Backbone.I18nView = Backbone.View.extend({ locale: 'en', - initialize: function() { - console.log("Call on me"); + initializeI18n: function(locale) { + this.locale = locale; // TODO implement cache //memoizeFormatConstructor(Intl.NumberFormat).getNumberFormat(); }, + + formatMessage(key, values) { + this.cachedMsg.format(key, values); + }, + + // TODO how to use it from outside? an singleton instance of I18n? use case: pass translated strings into view initializer t: function(key, values = {}, defaultMessage = null) { // get the message from current locale - var msg = recline.View.translations['pl'][key]; + var msg = recline.View.translations[this.locale][key]; // fallback to key or default message if no translation is defined if (msg == null) { - console.warn("Missing locale for " + "pl" + "." + key); // TODO when dfault set and it's default locale then be quiet + console.warn("Missing locale for " + this.locale + "." + key); // TODO when dfault set and it's default locale then be quiet msg = defaultMessage; } if (msg == null) { msg = key; } - // TODO test fallback to key - // TODO test fallback to default message - // TODO test overriding messages in external apps // TODO i18n documentation - var mf = new IntlMessageFormat(msg, 'pl'); - return mf.format(values); + try { + var mf = new IntlMessageFormat(msg, 'pl'); + var formatted = mf.format(values); + + return formatted; + } catch (e) { + console.error("Got error while formatting \"" + msg + "\": " + e.message); + // todo {{ wywala Message format, trzeba by wyescapować i pwrzywrócic po podmianie + return msg; + } + + // TODO document below error (most probable variables ref hasn't ben changed in template) + /* 19:10:40.954 SyntaxError: