Skip to content

Commit

Permalink
Implemented theoephraim#2
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentGoderre committed Nov 1, 2013
1 parent 1bbc942 commit 85c2652
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules
npm-debug.log
tmp
tmp_*
locales
48 changes: 39 additions & 9 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* grunt-i18n-gspreadsheet
* https://github.com/theoephraim/grunt-i18n-gspreadsheet
* grunt-i18n-spreadsheet
* https://github.com/theoephraim/grunt-i18n-spreadsheet
*
* Copyright (c) 2013 Theo Ephraim
* Licensed under the MIT license.
Expand All @@ -25,17 +25,46 @@ module.exports = function(grunt) {

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp'],
tests: ['tmp_*'],
},

connect: {
server: {
options: {
port: 8080,
base: "server"
}
}
},

// Configuration to be run (and then tested).
i18n_gspreadsheet: {
test_config: {
i18n_spreadsheet: {
gdocs_test_config: {
options: {
key_column: 'key',
output_dir: 'tmp_gdocs',
google_docs: {
// this document key points to the test file -- see readme for more info
document_key: '0Araic6gTol6SdEtwb1Badl92c2tlek45OUxJZDlyN2c'
}
}
},
http_test_config: {
options: {
key_column: 'key',
output_dir: 'tmp_http',
http: {
url: 'http://localhost:8080/grunt-i18n-spreadsheet.csv'
}
}
},
local_test_config: {
options: {
key_column: 'key',
output_dir: 'tmp',
// this document key points to the test file -- see readme for more info
document_key: '0Araic6gTol6SdEtwb1Badl92c2tlek45OUxJZDlyN2c'
output_dir: 'tmp_local',
local: {
src: 'server/grunt-i18n-spreadsheet.csv'
}
}
}
},
Expand All @@ -53,11 +82,12 @@ module.exports = function(grunt) {
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', 'i18n_gspreadsheet', 'nodeunit']);
grunt.registerTask('test', ['clean', 'connect', 'i18n_spreadsheet', 'nodeunit']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# grunt-i18n-gspreadsheet
# grunt-i18n-spreadsheet

> Grunt plugin to generate i18n locale files from a google spreadsheet
Expand All @@ -8,23 +8,23 @@ This plugin requires Grunt `~0.4.1`
If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

```shell
npm install grunt-i18n-gspreadsheet --save-dev
npm install grunt-i18n-spreadsheet --save-dev
```

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
grunt.loadNpmTasks('grunt-i18n-gspreadsheet');
grunt.loadNpmTasks('grunt-i18n-spreadsheet');
```

## The "i18n_gspreadsheet" task
## The "i18n_spreadsheet" task

### Overview
In your project's Gruntfile, add a section named `i18n_gspreadsheet` to the data object passed into `grunt.initConfig()`.
In your project's Gruntfile, add a section named `i18n_spreadsheet` to the data object passed into `grunt.initConfig()`.

```js
grunt.initConfig({
i18n_gspreadsheet: {
i18n_spreadsheet: {
options: {
// Task-specific options go here.
},
Expand Down Expand Up @@ -87,12 +87,14 @@ Most likely, you will just be writing a single set of locale files from a single

```js
grunt.initConfig({
i18n_gspreadsheet: {
i18n_spreadsheet: {
my_config: {
options: {
google_account: '[email protected]',
google_password: 'MySuperSecretPassword',
document_key: '0Araic6gTol6SdEtwb1Badl92c2tlek45OUxJZDlyN2c'
google_docs: {
google_account: '[email protected]',
google_password: 'MySuperSecretPassword',
document_key: '0Araic6gTol6SdEtwb1Badl92c2tlek45OUxJZDlyN2c'
}
}
}
},
Expand Down
25 changes: 17 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "grunt-i18n-gspreadsheet",
"name": "grunt-i18n-spreadsheet",
"description": "Grunt plugin to generate i18n locale files from a google spreadsheet",
"version": "0.1.5",
"homepage": "https://github.com/theoephraim/grunt-i18n-gspreadsheet",
"homepage": "https://github.com/theoephraim/grunt-i18n-spreadsheet",
"author": {
"name": "Theo Ephraim",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "git://github.com/theoephraim/grunt-i18n-gspreadsheet.git"
"url": "git://github.com/theoephraim/grunt-i18n-spreadsheet.git"
},
"bugs": {
"url": "https://github.com/theoephraim/grunt-i18n-gspreadsheet/issues"
"url": "https://github.com/theoephraim/grunt-i18n-spreadsheet/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/theoephraim/grunt-i18n-gspreadsheet/blob/master/LICENSE-MIT"
"url": "https://github.com/theoephraim/grunt-i18n-spreadsheet/blob/master/LICENSE-MIT"
}
],
"main": "Gruntfile.js",
Expand All @@ -31,18 +31,27 @@
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt": "~0.4.1"
"grunt": "~0.4.1",
"grunt-contrib-connect": "~0.5.0"
},
"peerDependencies": {
"grunt": "~0.4.1"
},
"keywords": [
"gruntplugin", "grunt", "i18n", "google", "spreadsheet", "gdocs", "locale"
"gruntplugin",
"grunt",
"i18n",
"google",
"spreadsheet",
"gdocs",
"locale"
],
"dependencies": {
"google-spreadsheet": "~0.2.5",
"step": "0.0.5",
"underscore": "~1.5.2",
"mkdirp": "~0.3.5"
"mkdirp": "~0.3.5",
"request": "~2.27.0",
"csv": "~0.3.6"
}
}
9 changes: 9 additions & 0 deletions server/grunt-i18n-spreadsheet.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
key,en,fr,es,comments
!test,en,fr,es,Must be the same as locale -- used for testing
,hello,bonjour,hola,default (en) is empty for testing
,goodbye,Au revoir,Adios,
!newlineTest1,"This item
has a newline in it.",,,
!newlineTest2,"This item

has 2 newlines in it.",,,
93 changes: 74 additions & 19 deletions tasks/i18n_gspreadsheet.js → tasks/i18n_spreadsheet.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,85 @@
/*
* grunt-i18n-gspreadsheet
* https://github.com/theoephraim/grunt-i18n-gspreadsheet
* grunt-i18n-spreadsheet
* https://github.com/theoephraim/grunt-i18n-spreadsheet
*
* Copyright (c) 2013 Theo Ephraim
* Licensed under the MIT license.
*/

'use strict';

var GoogleSpreadsheet = require("google-spreadsheet");
var Step = require('step');
var _ = require('underscore');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var csv = require("csv");

module.exports = function(grunt) {

function loadGoogleSpreadSheet(options, parentStep) {
var GoogleSpreadsheet = require("google-spreadsheet");
var gsheet = new GoogleSpreadsheet( options.document_key );
Step(
function setAuth(){
if ( options.google_account && options.google_password ){
gsheet.setAuth( options.google_account, options.google_password, this );
} else {
this();
}
},
function fetchSheetInfo(err){
if ( err ){
grunt.log.error('Invalid google credentials for "' + options.google_account + '"');
parentStep(err);
}
gsheet.getRows( 1, parentStep );
}
);
}

function loadHttpCSVSpreadSheet(options, parentStep){
Step(
function fetchSheetInfo() {
var request = require('request');
request(options, this);
},
function parseSheet(err, response, body) {
if ( err ) {
grunt.log.error('Error fetch ' + options.url + '"');
parentStep (err);
}
csv().from.string(body).to.array(function(rows){
parentStep (false, normalizeCSV(rows));
});
}
);
}

function loadLocalCSVSpreadsheet(options, parentStep){
csv().from.path(options.src).to.array(function(rows){
parentStep (false, normalizeCSV(rows));
});
}

function normalizeCSV(csv){
var out = [];
var keys = [];

for (var k = 0; k < csv[0].length; k++){
keys.push(csv[0][k]);
}
for (var r = 1; r < csv.length; r++){
var row = csv[r];
var o = {};
for (var c = 0; c < row.length; c++){
o[c < keys.length ? keys[c] : "col" + c] = row[c];
}
out.push(o);
}
return out;
}

function sortObjectByKeys(map) {
var keys = _.sortBy(_.keys(map), function(a) { return a.toLowerCase(); });
var newmap = {};
Expand All @@ -28,7 +90,7 @@ module.exports = function(grunt) {
}


grunt.registerMultiTask('i18n_gspreadsheet', 'Grunt plugin to generate i18n locale files from a google spreadsheet', function() {
grunt.registerMultiTask('i18n_spreadsheet', 'Grunt plugin to generate i18n locale files from a google spreadsheet', function() {

// default options
var options = this.options({
Expand All @@ -43,25 +105,18 @@ module.exports = function(grunt) {

var locales = [];
var translations = {};
var gsheet = new GoogleSpreadsheet( options.document_key );
var output_dir = path.resolve( process.cwd() + '/' + options.output_dir );

Step(
function setAuth(){
if ( options.google_account && options.google_password ){
gsheet.setAuth( options.google_account, options.google_password, this );
} else {
this();
function loadSpreadSheet(){
if (options.google_docs !== undefined){
loadGoogleSpreadSheet(options.google_docs, this);
} else if (options.http !== undefined){
loadHttpCSVSpreadSheet(options.http, this);
}else {
loadLocalCSVSpreadsheet(options.local, this);
}
},
function fetchSheetInfo(err){
if ( err ){
grunt.log.error('Invalid google credentials for "' + options.google_account + '"');
return done( false );
}

gsheet.getRows( 1, this );
},
function buildTranslationJson(err, rows){
if ( err ){
grunt.log.error( err );
Expand All @@ -71,7 +126,7 @@ module.exports = function(grunt) {
grunt.log.error('ERROR: no translations found in sheet');
return done( false );
}

// First determine which locales are supported
var gsheet_keys = _(rows[0]).keys();
_(gsheet_keys).each(function(locale){
Expand Down
34 changes: 0 additions & 34 deletions test/i18n_gspreadsheet_test.js

This file was deleted.

Loading

0 comments on commit 85c2652

Please sign in to comment.