Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

node-i18n v0.2 #1

Merged
merged 8 commits into from about 3 years ago

2 participants

Kit Cambridge Dan Hansen
Kit Cambridge

I've made some improvements and optimizations to node-i18n, and bumped the version to 0.2. Here's a brief synopsis of my changes:

  • Removed the Underscore dependency, since it was only used in I18n#t. I've also renamed this method to I18n#interpolate for clarity.
  • The top-level I18n function now works as both a factory and a constructor, removing the need for the I18n.factory method.
  • Fixed a few typos in the examples. node examples/templating.js should now run without throwing any exceptions.
  • A few other minor optimizations.
Dan Hansen OhaiBBQ referenced this pull request from a commit
Dan Hansen Merged pull request #1 from kitgoncharov/v0.2.
node-i18n v0.2
9fc693a
Dan Hansen OhaiBBQ merged commit 9fc693a into from
Dan Hansen OhaiBBQ closed this
Dan Hansen
Owner

Thanks a ton! I'm really loving the changes, I wrote this very quickly for something to do and neglected it after that. We should continue to work on it. :)

Kit Cambridge

Thanks for the quick merge! Sure, we could definitely keep working on it...were you thinking of adding anything in particular?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
10 examples/templating.js
... ... @@ -1,9 +1,9 @@
1 1 var i18n = require("./../index").i18n,
2 2 instance = new i18n(__dirname + "/locales");
3 3 console.log(instance);
4   -instance.load("en");
  4 +instance.setLocale("en");
5 5
6   -console.log(instance.t("sup") === "hi"); // true
7   -console.log(instance.t("object.what.is") === "new"); // true
8   -console.log(instance.t("object.value" === { hi: "hello"}), "testing hello"); // true
9   -console.log(instance.t("object.value") === "testing hello"); // false
  6 +console.log(instance.interpolate("sup") === "hi"); // true
  7 +console.log(instance.interpolate("object.what.is") === "new"); // true
  8 +console.log(instance.interpolate("object.value", { hi: "hello"}) === "testing hello"); // true
  9 +console.log(instance.interpolate("object.value") === "testing hello"); // false
74 lib/i18n.js
... ... @@ -1,45 +1,57 @@
1   -var _ = require("underscore");
  1 +// Convenience aliases and the `I18n` function.
  2 +var path = require('path'), I18n = module.exports = function I18n(path, language, locale) {
  3 + return new I18n.prototype.initialize(path, language, locale);
  4 +};
2 5
3   -var I18n = function(path, language, locale) {
4   - if(typeof arguments[0] === "object") {
5   - arguments = Array.prototype.slice.call(arguments[0]);
6   - }
  6 +// The current version of `node-i18n`. Keep in sync with `package.json`.
  7 +I18n.VERSION = '0.2';
7 8
8   - this.path = arguments[0];
9   - this.language = arguments[1];
10   - this.locale = arguments[2];
  9 +// Default template settings; used in `I18n#interpolate`.
  10 +I18n.templateSettings = {
  11 + 'evaluate': /<%([\s\S]+?)%>/g,
  12 + 'interpolate': /<%=([\s\S]+?)%>/g
11 13 };
12 14
13   -I18n.factory = function(path, language, locale, instance) {
14   - return (typeof instance !== "undefined")
15   - ? instance
16   - : new I18n(arguments);
  15 +// Creates a new `I18n` instance.
  16 +I18n.prototype.initialize = function initialize(path, language, locale) {
  17 + this.path = path;
  18 + this.language = language;
  19 + this.locale = locale;
  20 + return this;
17 21 };
18 22
19   -I18n.prototype.setLocale = function(language) {
20   - this.language = (this.language !== language)
21   - ? language
22   - : language;
  23 +I18n.prototype.initialize.prototype = I18n.prototype;
23 24
24   - this.locale = require(this.path + "/" + language).all;
  25 +// Imports the specified locale.
  26 +I18n.prototype.setLocale = function setLocale(language) {
  27 + if (language != this.language) {
  28 + this.language = language;
  29 + this.locale = require(path.join(this.path, language)).all;
  30 + }
  31 + return this;
25 32 };
26 33
27   -I18n.prototype.t = function(item, context) {
28   - var ret,
29   - _item = item.split(".");
  34 +// Interpolates a template using the provided locale.
  35 +I18n.prototype.interpolate = function interpolate(item, context) {
  36 + var result, property = item.split('.'), section, settings;
30 37
31   - while(part = _item.shift()) {
32   - ret = (ret) ? ret[part] : this.locale[part];
33   - if(typeof ret === "undefined") {
34   - return "";
35   - }
  38 + while ((section = property.shift())) {
  39 + result = (result || this.locale)[section];
  40 + if (result == null) return '';
36 41 }
37 42
38   - if(typeof context === "object") {
39   - return _.template(ret, context);
  43 + if (typeof context == 'object') {
  44 + settings = I18n.templateSettings;
  45 + // JavaScript micro-templating implementation; taken from Underscore.
  46 + return Function('obj', 'var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push(\'' +
  47 + result.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(settings.interpolate, function(match, code) {
  48 + return "'," + code.replace(/\\'/g, "'") + ",'";
  49 + }).replace(settings.evaluate || null, function(match, code) {
  50 + return "');" + code.replace(/\\'/g, "'").replace(/[\r\n\t]/g, ' ') + "__p.push('";
  51 + }).replace(/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\t/g, '\\t') +
  52 + "');}return __p.join('');"
  53 + )(context);
40 54 }
41 55
42   - return ret;
43   -};
44   -
45   -module.exports = I18n;
  56 + return result;
  57 +};
25 package.json
... ... @@ -1,12 +1,23 @@
1 1 {
2 2 "name": "node-i18n",
  3 + "version": "0.2",
3 4 "description": "Minimalistic i18n module with templating",
4   - "version": "0.1",
5   - "author": "Dan Hansen <mokker1234@gmail.com>",
6   - "dependencies": {
7   - "underscore": ">= 1.1.4"
8   - },
9   - "keywords": ["i18n", "internationalization", "internationalisation", "translation"],
  5 + "homepage": "http://github.com/OhaiBBQ/node-i18n",
10 6 "main": "index",
11   - "engines": { "node": ">= 0.2.0" }
  7 + "keywords": ["i18n", "internationalization", "internationalisation", "translation"],
  8 + "author": {
  9 + "name": "Dan Hansen",
  10 + "email": "mokker1234@gmail.com"
  11 + },
  12 + "maintainers": [{
  13 + "name": "Kit Goncharov",
  14 + "web": "http://kitgoncharov.github.com"
  15 + }],
  16 + "bugs": {
  17 + "url": "http://github.com/OhaiBBQ/node-i18n/issues"
  18 + },
  19 + "repository": {
  20 + "type": "git",
  21 + "url": "git://github.com/OhaiBBQ/node-i18n.git"
  22 + }
12 23 }
4 test/test-i18n.js
@@ -4,7 +4,7 @@ exports["factory"] = {};
4 4 exports["factory"]["creates a new instance if one isnt passed"] = function(test) {
5 5
6 6 test.equal(
7   - i18n.factory("asdf", "asdf").path,
  7 + i18n("asdf", "asdf").path,
8 8 new i18n("asdf", "asdf").path
9 9 );
10 10
@@ -13,7 +13,7 @@ exports["factory"]["creates a new instance if one isnt passed"] = function(test)
13 13
14 14 exports["setLocale"] = {};
15 15 exports["setLocale"]["sets the locale to module.exports.all"] = function(test) {
16   - var instance = i18n.factory(__dirname + "/locales");
  16 + var instance = i18n(__dirname + "/locales");
17 17 instance.setLocale("en");
18 18 test.equal(instance.locale, require(__dirname + "/locales/en").all);
19 19 test.done();

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.