Last active
August 29, 2015 14:14
-
-
Save Juansasa/ef67ca1018b31772c0fa to your computer and use it in GitHub Desktop.
Automate post development language translation for Angularjs webapp with Grunt
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
Dependencies required | |
Bower : angular-gettext | |
Grunt-plugins: | |
"grunt-angular-gettext" | |
"grunt-file-blocks" | |
Step 1: Create task for extracting texts | |
'nggettext_extract': { | |
pot: { | |
files: { | |
/* fileblock:html partials */ | |
/* endfileblock */ | |
// Html files | |
} | |
} | |
} | |
Step 2: Annotate your existing HTML files with "translate" directive as either attribute or element | |
For example: | |
original: <h1>Current text</h1> | |
can be anotated as | |
<h1 translate>Current text</h1> or <h1> <translate>Current text</translate> </h1> | |
Step 3 : Create fileblock task to inject Html files dependencies to nggettext task created in Step 1. | |
fileblocks: { | |
options: { | |
/* Task options */ | |
removeFiles: true, | |
templates: { | |
html: '${file}' | |
}, | |
templatesFn: { | |
html: function(file){ | |
var path = require('path'); | |
var fileExtension = path.extname(file); | |
var filename = path.basename(file, fileExtension); | |
return '\t' + '\'<%= yeoman.app %>/texts/' + filename + '.pot\'' + ':[\'<%= yeoman.app %>/' + file + '\'],'; | |
} | |
} | |
}, | |
htmls: { | |
files: [ | |
{ | |
src: 'Gruntfile.js', | |
blocks: { | |
partials : { | |
cwd: '<%= yeoman.app %>', | |
src: ['**/*.html', '!bower_components/**'] | |
} | |
} | |
} | |
] | |
} | |
} | |
Step 4. Annotate nggettext task from Step 1 to automatically inject the file dependencies | |
'nggettext_extract': { | |
pot: { | |
files: { | |
/* fileblock:html partials */ | |
// All dependencies will be injected here | |
/* endfileblock */ | |
} | |
} | |
} | |
Step 5. Register another Grunt task to do these steps all in one call. | |
grunt.registerTask('extract-text','Extract html texts to .pot files in texts/ folder', function(){ | |
grunt.task.run([ | |
'fileblocks', // To inject dependencies | |
'nggettext_extract' // To extract annotated texts to separated files | |
]); | |
}); | |
Step 5. Extract the texts | |
run "grunt extract-text" | |
Step 6. Do actual translation using various free gettext tools for .po/pot files | |
I prefer "Poedit" | |
Step 7. Create grunt task to convert translated texts to Angular-ready javascript files | |
'nggettext_compile': { | |
all: { | |
options: { | |
module: 'fmuClientApp' | |
}, | |
files: { | |
'<%= yeoman.app %>/texts/translations.js': ['<%= yeoman.app %>/texts/*.pot'] | |
} | |
}, | |
} | |
Step 8. Modify the Angular "app" module to apply the changes | |
angular.module('myapp', [ | |
'gettext' | |
]) | |
.run(['gettextCatalog', function(gettextCatalog){ | |
gettextCatalog.currentLanguage='sv'; | |
gettextCatalog.debug = true; | |
}]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment