From a64a977866a61380801b5e3162eb5ee4babe8b0c Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Wed, 9 Feb 2011 01:07:02 -0600 Subject: [PATCH] basic testing, fix factory method --- examples/templating.js | 10 +++++----- lib/i18n.js | 27 +++++++++++++-------------- test/locales/en.js | 16 ++++++++++++++++ test/test-i18n.js | 13 +++++++++++++ 4 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 test/locales/en.js create mode 100644 test/test-i18n.js diff --git a/examples/templating.js b/examples/templating.js index e8d3925..93cf3bc 100644 --- a/examples/templating.js +++ b/examples/templating.js @@ -5,13 +5,13 @@ * Time: 10:22:01 PM * To change this template use File | Settings | File Templates. */ - +/* TODO: Better examples */ var i18n = require("./../index").i18n, instance = new i18n(__dirname + "/locales"); console.log(instance); instance.load("en"); -console.log(instance.t("sup") === "hi"); // true -console.log(instance.t("object.what.is") === "new"); // true -console.log(instance.t("object.value", { hi: "hello"}) === "testing hello"); // true -console.log(instance.t("object.value") === "testing hello"); // false \ No newline at end of file +console.log(instance.t("sup") === "hi"); // true +console.log(instance.t("object.what.is") === "new"); // true +console.log(instance.t("object.value" === { hi: "hello"}), "testing hello"); // true +console.log(instance.t("object.value") === "testing hello"); // false \ No newline at end of file diff --git a/lib/i18n.js b/lib/i18n.js index 7ce3274..a6750c9 100644 --- a/lib/i18n.js +++ b/lib/i18n.js @@ -10,9 +10,19 @@ var _ = require("underscore"); var I18n = function(path, language, locale) { - this.path = path; - this.language = language; - this.locale = locale; + if(typeof arguments[0] === "object") { + arguments = Array.prototype.slice.call(arguments[0]); + } + + this.path = arguments[0]; + this.language = arguments[1]; + this.locale = arguments[2]; +}; + +I18n.factory = function(path, language, locale, instance) { + return (typeof instance !== "undefined") + ? instance + : new I18n(arguments); }; I18n.prototype.load = function(language) { @@ -41,15 +51,4 @@ I18n.prototype.t = function(item, context) { return ret; }; -I18n.prototype.factory = function(path, language, locale, instance) { - return (typeof instance !== "undefined") - ? instance - : new I18n( - arguments.slice( - 0, - arguments.length - 2 - ) - ); -}; - module.exports = I18n; \ No newline at end of file diff --git a/test/locales/en.js b/test/locales/en.js new file mode 100644 index 0000000..70f6236 --- /dev/null +++ b/test/locales/en.js @@ -0,0 +1,16 @@ +/** + * Created by . + * User: dan + * Date: Feb 8, 2011 + * Time: 10:20:06 PM + * To change this template use File | Settings | File Templates. + */ +exports.all = { + "sup": "hi", + "object": { + "what": { + "is": "new" + }, + "value": "testing <%= hi %>" + } +}; \ No newline at end of file diff --git a/test/test-i18n.js b/test/test-i18n.js new file mode 100644 index 0000000..d5614c4 --- /dev/null +++ b/test/test-i18n.js @@ -0,0 +1,13 @@ +/** + * Created by . + * User: dan + * Date: Feb 8, 2011 + * Time: 11:59:33 PM + * To change this template use File | Settings | File Templates. + */ + +var i18n = require("./../index").i18n; +exports["load"]["it does stuff"] = function() { + + +} \ No newline at end of file