Friday, September 26, 2014

backbone.js + underscore.js + ??? = good localization

Starting a localization process for one of projects. Currently looking for localization of backbone and underscore parts. i18n plugin from require.js seems to do almost all that I need, but... there is a strange feeling that everything is too simple... Looking around on the web found some frameworks, projects but they don't offer anything that I can not get with a plain and simple require.js i18n... Strange... Someone passed through same feeling before? What was the result? :)

Thursday, September 25, 2014

npm + grunt + bower + grunt-bower-task

Maybe this will help someone... In any case - I'm keeping this KB mostly for myself :)

Since I prefer following structure for my node.js (and backbone / underscore) projects:

project/
  public/
   js/
     lib/ - here will go all libs/frameworks
     app/ - here will go all JS application files
   ...
   css/
   fonts/
   tpl/

1) Sample package.json:

{
  "name": "test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.*",
    "bower": "*"
  },
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-bower-task": "^0.4.0"
  }
}


2) Sample Gruntfile.js:

module.exports = function(grunt) {
  var path = require('path');

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    bower: {
      install: {
        options: {
          targetDir: './public',
          layout: function(type, component, source) {
            var renamedType = type;
            if (type == 'js') renamedType = 'js/lib';
            else if (type == 'js/map') renamedType = 'js/lib';
            else if (type == 'js/lang') renamedType = 'js/lib/lang';
            else if (type == 'css') renamedType = 'css';
            else if (type == 'css/img') renamedType = 'css';
            return path.join(renamedType);;
          },
          install: true,
          verbose: false,
          cleanTargetDir: false,
          cleanBowerDir: false,
          bowerOptions: {}
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-bower-task');

  // Default task(s).
  grunt.registerTask('default', ['bower']);
};

3) Sample bower.json

{
  "name": "test",
  "version": "0.0.0",
  "moduleType": [
    "amd"
  ],
  "authors": [
    "Viktor Yarmak "
  ],
  "license": "MIT",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "dependencies": {
    "requirejs": "*",
    "jquery": "1.*",
    "backbone": "*",
    "underscore": "*",
    "bootstrap": "*",
    "font-awesome": "*",
    "backbone-validation": "*",
    "backbone.paginator": "*",
    "fuelux": "*",
    "moment": "*",
    "chosen": "https://github.com/harvesthq/chosen/releases/download/v1.1.0/chosen_v1.1.0.zip"
  },
  "exportsOverride": {
    "requirejs": {
      "js": "require.js"
    },
    "jquery": {
      "js": "dist/jquery*.js",
      "js/map": "dist/jquery*.map"
    },
    "jquery-1.*": {},
    "backbone": {
      "js": "backbone.js"
    },
    "underscore": {
      "js": "underscore*.js",
      "js/map": "underscore*.map"
    },
    "bootstrap": {
      "js": "dist/js/bootstrap*.js",
      "js/map": "dist/js/bootstrap*.map",
      "fonts": "dist/fonts/*",
      "css": "dist/css/*"
    },
    "font-awesome": {
      "css": "css/*",
      "fonts": "fonts/*"
    },
    "backbone-validation": {
      "js": "dist/*.js"
    },
    "backbone.paginator": {
      "js": "lib/*.js"
    },
    "fuelux": {
      "js": "dist/js/*.js",
      "fonts": "dist/fonts/*",
      "css": "dist/css/*"
    },
    "chosen": {
      "js": "chosen.jquery*.js",
      "css/img": "chose*.png",
      "css": "chosen*.css"
    },
    "moment": {
      "js": "moment.js",
      "js": "min/*.js",
      "js/lang": "lang/*.js"
    }
  }
}

After this you can run:

npm update
grunt