diff --git a/app/assets/javascripts/helpers/i18n.js b/app/assets/javascripts/helpers/i18n.js index f675e9624218b80c3b3291b53fbcc5cec5b50419..0ece8f53b8ba87cb0768eab6a6af28abeff0e142 100644 --- a/app/assets/javascripts/helpers/i18n.js +++ b/app/assets/javascripts/helpers/i18n.js @@ -2,20 +2,21 @@ * licensed under the Affero General Public License version 3 or later. See * the COPYRIGHT file. */ - Diaspora.I18n = { - language: "en", - locale: {}, - - loadLocale: function(locale, language) { - this.locale = $.extend(this.locale, locale); - this.language = language; - rule = this.t('pluralization_rule'); - if (rule === "") - rule = 'function (n) { return n == 1 ? "one" : "other" }'; - eval("this.pluralizationKey = "+rule); - }, - - t: function(item, views) { + +Diaspora.I18n = { + language: "en", + locale: {}, + + loadLocale: function(locale, language) { + this.locale = $.extend(this.locale, locale); + this.language = language; + rule = this.t('pluralization_rule'); + if (rule === "") + rule = 'function (n) { return n == 1 ? "one" : "other" }'; + eval("this.pluralizationKey = "+rule); + }, + + t: function(item, views) { var items = item.split("."), translatedMessage, nextNamespace; @@ -35,5 +36,12 @@ } return _.template(translatedMessage, views || {}); - } - }; + }, + + reset: function() { + this.locale = {}; + + if( arguments.length > 0 && !(_.isEmpty(arguments[0])) ) + this.locale = arguments[0]; + } +}; diff --git a/spec/javascripts/widgets/i18n-spec.js b/spec/javascripts/widgets/i18n-spec.js index 8740a5deeb72d4560cdd9eacc2a9c92aff57ba0e..7f51f5c7658cb900b0eb31146fcd072c9cea4da3 100644 --- a/spec/javascripts/widgets/i18n-spec.js +++ b/spec/javascripts/widgets/i18n-spec.js @@ -3,69 +3,84 @@ * the COPYRIGHT file. */ -describe("Diaspora", function() { - describe("widgets", function() { - describe("i18n", function() { - var locale = {namespace: { - message: "hey", - template: "<%= myVar %>", - otherNamespace: { - message: "hello from another namespace", - otherMessage: { - zero: "none", - one: "just one", - few: "just a few", - many: "way too many", - other: "what?" - } - } +describe("Diaspora.I18n", function() { + var locale = {namespace: { + message: "hey", + template: "<%= myVar %>", + otherNamespace: { + message: "hello from another namespace", + otherMessage: { + zero: "none", + one: "just one", + few: "just a few", + many: "way too many", + other: "what?" } - }; + } + } + }; - describe("loadLocale", function() { - it("sets the class's locale variable", function() { - Diaspora.I18n.loadLocale(locale); + beforeEach(function(){ + Diaspora.I18n.reset(); // run tests with clean locale + }); - expect(Diaspora.I18n.locale).toEqual(locale); - }); + describe("::loadLocale", function() { + it("sets the class's locale variable", function() { + Diaspora.I18n.loadLocale(locale); - it("extends the class's locale variable on multiple calls", function() { - var data = {another: 'section'}, - extended = $.extend(locale, data); + expect(Diaspora.I18n.locale).toEqual(locale); + }); - Diaspora.I18n.loadLocale(locale); - Diaspora.I18n.loadLocale(data); + it("extends the class's locale variable on multiple calls", function() { + var data = {another: 'section'}, + extended = $.extend(locale, data); - expect(Diaspora.I18n.locale).toEqual(extended); - }); - }); + Diaspora.I18n.loadLocale(locale); + Diaspora.I18n.loadLocale(data); - describe("t", function() { - var translation; - beforeEach(function() { Diaspora.I18n.loadLocale(locale); }); + expect(Diaspora.I18n.locale).toEqual(extended); + }); + }); - it("returns the specified translation", function() { - translation = Diaspora.I18n.t("namespace.message"); + describe("::t", function() { + var translation; + beforeEach(function() { Diaspora.I18n.loadLocale(locale); }); - expect(translation).toEqual("hey"); - }); + it("returns the specified translation", function() { + translation = Diaspora.I18n.t("namespace.message"); - it("will go through a infinitely deep object", function() { - translation = Diaspora.I18n.t("namespace.otherNamespace.message"); + expect(translation).toEqual("hey"); + }); + + it("will go through a infinitely deep object", function() { + translation = Diaspora.I18n.t("namespace.otherNamespace.message"); - expect(translation).toEqual("hello from another namespace"); - }); + expect(translation).toEqual("hello from another namespace"); + }); - it("can render a mustache template", function() { - translation = Diaspora.I18n.t("namespace.template", { myVar: "it works!" }); + it("can render a mustache template", function() { + translation = Diaspora.I18n.t("namespace.template", { myVar: "it works!" }); - expect(translation).toEqual("it works!"); - }); + expect(translation).toEqual("it works!"); + }); + + it("returns an empty string if the translation is not found", function() { + expect(Diaspora.I18n.t("missing.locale")).toEqual(""); + }); + }); + + describe("::reset", function(){ + it("clears the current locale", function() { + Diaspora.I18n.loadLocale(locale); + Diaspora.I18n.reset() + expect(Diaspora.I18n.locale).toEqual({}); + }); - it("returns an empty string if the translation is not found", function() { - expect(Diaspora.I18n.t("missing.locale")).toEqual(""); - }); - }); + it("sets the locale to only a specific value", function() { + var data = { some: 'value' }; + Diaspora.I18n.loadLocale(locale); + Diaspora.I18n.reset(data); + expect(Diaspora.I18n.locale).toEqual(data); }); }); });