Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
415 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ node_js: | |
- 0.8 | ||
before_install: | ||
- npm install | ||
- npm install grunt-cli -g | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module.exports = function(grunt) { | ||
|
||
'use strict'; | ||
|
||
// Project configuration. | ||
grunt.initConfig({ | ||
nodeunit: { | ||
files: ['test/**/*.js'] | ||
}, | ||
watch: { | ||
files: '<config:lint.files>', | ||
tasks: 'default' | ||
}, | ||
jshint: { | ||
options: { | ||
curly: true, | ||
eqeqeq: true, | ||
loopfunc: true, | ||
forin: false, | ||
immed: true, | ||
latedef: true, | ||
newcap: true, | ||
noarg: true, | ||
sub: true, | ||
undef: true, | ||
boss: true, | ||
eqnull: true, | ||
node: true, | ||
es5: true, | ||
supernew: true | ||
}, | ||
globals: { | ||
gt: true | ||
}, | ||
files: ['grunt.js', 'tasks/**/*.js'] | ||
}, | ||
|
||
translate: { | ||
options: { | ||
configDir: './test/translations', | ||
requireJS: true, | ||
defaultLanguage: 'en' // grunt-translate use it to update translation. | ||
}, | ||
compile: { | ||
output: './test/translations/output' | ||
}, | ||
update: { | ||
src: ['./test/example/**/*.js'] | ||
} | ||
} | ||
}); | ||
// Load local tasks. | ||
grunt.loadTasks('tasks'); | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
grunt.loadNpmTasks('grunt-contrib-nodeunit'); | ||
|
||
|
||
// Default task. | ||
grunt.registerTask('default', 'lint translate:update translate:compile test'); | ||
|
||
|
||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
var grunt = require('grunt'), | ||
engine = require('./engine'), | ||
path = require('path'), | ||
OPERATORS = require('./operators'); | ||
|
||
var config = {}; | ||
|
||
/** | ||
Get all locales | ||
return Array of all locales | ||
*/ | ||
config.getAllLocales = function(options){ | ||
return Object.keys(grunt.file.readJSON(options.configDir + '/locales.json')); | ||
}; | ||
|
||
/** | ||
Check for error duplicate | ||
@param Object res result that has already the key | ||
@param String key | ||
@param Array vars Translation vars | ||
@throws Duplicate Translation Keys Error | ||
*/ | ||
config.hasErrorDuplicate = function(res, key, vars){ | ||
|
||
// Check for error duplicate | ||
var errorDuplicate = false; | ||
if(vars.length !== res[key].vars.length) { | ||
errorDuplicate = true; | ||
} else { | ||
for(var i in vars) { | ||
if(res[key].vars[i] !== vars[i]) { | ||
errorDuplicate = true; | ||
break; | ||
} | ||
} | ||
} | ||
if(errorDuplicate) { | ||
throw { | ||
name: 'Duplicate Translation Keys', | ||
message: 'You have used gt(\'' + key + '\') and input two different vars: \n' + 'Variables: ' + res[key].vars.join(',') + '\n' + 'Is not equal: ' + vars.join(',') | ||
}; | ||
} | ||
}; | ||
|
||
/** | ||
Get all locales | ||
@return Object of all locales | ||
*/ | ||
config.getAllTranslations = function(options){ | ||
var files = grunt.file.expand({filter: 'isFile'}, options.configDir + '/locales/*.json'); | ||
var locales = {}; | ||
files.forEach(function(locale){ | ||
locales[path.basename(locale, '.json')] = grunt.file.readJSON(locale); | ||
}); | ||
return locales; | ||
}; | ||
|
||
/** | ||
Returns translation key from a translation function string | ||
@param String fn string of the function | ||
@return String Translation key | ||
*/ | ||
config.getTranslationKey = function(fn){ | ||
return (fn.match(/['|"][\w|\-|\s|\&|<|>|\/]+['|"]/))[0].replace(/'/g, ''); | ||
}; | ||
|
||
/** | ||
Returns all the vars in a translation function | ||
@param String fn Function string | ||
@return Array of all vars | ||
*/ | ||
config.getVars = function(fn){ | ||
var json = fn.match(/\{[\w|\s|:|"|'|\-|\{|\}|\,|\/]*\}/); | ||
if(json === null) { | ||
return []; | ||
} | ||
json = json[0].replace(/\s*\w+\s*\:/g, function(m){ | ||
var key = m.match(/\w+/); | ||
return '"' + key + '":'; | ||
}).replace(/'/g, function(){ | ||
return '"'; | ||
}); | ||
var vars = JSON.parse(json); | ||
return Object.keys(vars); | ||
}; | ||
|
||
/** | ||
Checks if an operand has the right syntax | ||
@param String text | ||
@return Boolean | ||
*/ | ||
config.isTranslationText = function(text, key) { | ||
if(typeof text === 'undefined') { | ||
throw { | ||
name: 'Undefined translation', | ||
message: 'You have an undefined translation in:\n' + key | ||
}; | ||
} else if(text.substr(0,1) !== '"' || text.substr(-1) !== '"') { | ||
throw { | ||
name: 'Translation Text Wrong Syntax', | ||
message: 'You have missed quotation in:\n' + text | ||
}; | ||
} | ||
}; | ||
|
||
/** | ||
Checks if operands and operators has the correct syntax | ||
@param String operand1 | ||
@param String operator | ||
@param String operand2 | ||
@return Boolean true | ||
@throw Syntax Wrong in Translation JSON | ||
*/ | ||
config.isConditions = function(operand1, operator, operand2) { | ||
|
||
// Check operands | ||
if(!this.isOperand(operand1) || !this.isOperand(operand2)) { | ||
throw { | ||
name: 'Syntax Wrong in Translation JSON', | ||
message: 'One of the operands have wrong syntax: ' + operand1 + ' or ' + operand2 | ||
}; | ||
} | ||
|
||
// Validate operator | ||
if(OPERATORS.indexOf(operator) === -1) { | ||
throw { | ||
name: 'Syntax Wrong in Translation JSON', | ||
message: '"' + operator + '" should be one of ' + OPERATORS.join(',') | ||
}; | ||
} | ||
return true; | ||
}; | ||
|
||
/** | ||
Checks if an operand has the right syntax | ||
@param String operand | ||
@return Boolean | ||
*/ | ||
config.isOperand = function(operand) { | ||
if(/^"?\$?\w+"?$/.test(operand)) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
module.exports = config; | ||
|
Oops, something went wrong.