Skip to content

Commit

Permalink
refactor(i18n): Moved translations to own npm module
Browse files Browse the repository at this point in the history
Frontend translations are now maintained in an own repository and consumed by npm. Angular locales
are also installed by npm right now. Changed Grunt tasks for building, serving and testing.

BREAKING CHANGE: Translations can be found in repository sensebox/opensensemap-i18n
  • Loading branch information
mpfeil committed Dec 12, 2017
1 parent 0b03d67 commit daffd7f
Show file tree
Hide file tree
Showing 23 changed files with 112 additions and 3,194 deletions.
136 changes: 90 additions & 46 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ module.exports = function (grunt) {
// Define the configuration for all the tasks
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

env: {
dev: {
OPENSENSEMAP_API_URL: 'https://api.osem.vo1d.space',
OPENSENSEMAP_MAPTILES_URL: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
}
},

replace: {
control: {
options: {
Expand All @@ -40,11 +48,13 @@ module.exports = function (grunt) {
patterns: [
{
match: 'OPENSENSEMAP_API_URL',
replacement: process.env.OPENSENSEMAP_API_URL
replacement: '<%= OPENSENSEMAP_API_URL %>'
// replacement: process.env.OPENSENSEMAP_API_URL
},
{
match: 'OPENSENSEMAP_MAPTILES_URL',
replacement: process.env.OPENSENSEMAP_MAPTILES_URL
replacement: '<%= OPENSENSEMAP_MAPTILES_URL %>'
// replacement: process.env.OPENSENSEMAP_MAPTILES_URL
},
{
match: 'VERSION',
Expand All @@ -61,7 +71,7 @@ module.exports = function (grunt) {
patterns: [
{
match: 'OPENSENSEMAP_API_URL',
replacement: 'https://api.osem.vo1d.space'
replacement: '<%= OPENSENSEMAP_API_URL %>'
}
]
},
Expand All @@ -75,7 +85,7 @@ module.exports = function (grunt) {
patterns: [
{
match: 'OPENSENSEMAP_MAPTILES_URL',
replacement: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
replacement: '<%= OPENSENSEMAP_MAPTILES_URL %>'
}
]
},
Expand Down Expand Up @@ -121,12 +131,6 @@ module.exports = function (grunt) {
livereload: true
}
},
translations: {
files: ['<%= yeoman.app %>/translations/{,*/}*.json'],
options: {
livereload: true
}
},
//jsTest: {
// files: ['test/spec/{,*/}*.js'],
// tasks: ['newer:jshint:test', 'karma']
Expand Down Expand Up @@ -402,14 +406,24 @@ module.exports = function (grunt) {
}
},

'json-minify': {
build: {
files: '<%= yeoman.dist %>/translations/*.json'
}
},

// Copies remaining files to places other tasks can use
copy: {
translations: {
files: [
{
expand: true,
cwd: 'node_modules/@sensebox/opensensemap-i18n/dist',
dest: '.tmp/translations',
src: ['{,*/}*.json']
},
{
expand: true,
cwd: 'node_modules/angular-i18n/',
dest: '.tmp/translations/angular',
src: ['angular-locale_{<%= pkg.languages %>}.js']
}
]
},
dist: {
files: [{
expand: true,
Expand All @@ -425,11 +439,22 @@ module.exports = function (grunt) {
'images/{,*/}*.{webp}',
'images/{,*/}*.{gif}',
'fonts/*.*',
'fonts/webfonts/*.*',
'translations/*.json',
'translations/angular/*.js'
'fonts/webfonts/*.*'
]
}, {
},
{
expand: true,
cwd: 'node_modules/@sensebox/opensensemap-i18n/dist',
dest: 'dist/translations',
src: ['{,*/}*.json']
},
{
expand: true,
cwd: 'node_modules/angular-i18n/',
dest: 'dist/translations/angular',
src: ['angular-locale_{<%= pkg.languages %>}.js']
},
{
expand: true,
cwd: '.tmp/images',
dest: '<%= yeoman.dist %>/images',
Expand Down Expand Up @@ -584,7 +609,8 @@ module.exports = function (grunt) {
'copy:api',
'copy:apinew',
'copy:maps',
'copy:images'
'copy:images',
'copy:translations'
],
test: [
'copy:styles'
Expand Down Expand Up @@ -612,6 +638,8 @@ module.exports = function (grunt) {

grunt.task.run([
'clean:server',
'env:dev',
'loadconst',
'bowerInstall',
'concurrent:server',
'autoprefixer',
Expand All @@ -629,6 +657,11 @@ module.exports = function (grunt) {
grunt.task.run(['serve:' + target]);
});

grunt.registerTask('loadconst', 'Load constants', function(target) {
grunt.config('OPENSENSEMAP_API_URL', process.env.OPENSENSEMAP_API_URL);
grunt.config('OPENSENSEMAP_MAPTILES_URL', process.env.OPENSENSEMAP_MAPTILES_URL);
});

grunt.registerTask('test', [
'clean:server',
'concurrent:test',
Expand All @@ -637,21 +670,24 @@ module.exports = function (grunt) {
// 'karma'
]);

grunt.registerTask('testBuild', ['env:dev', 'build']);

grunt.registerTask('build', [
'clean:dist',
'loadconst',
'bowerInstall',
'useminPrepare',
'concurrent:dist',
'copy:dist',
'autoprefixer',
'concat',
'languages',
'ngAnnotate',
'cssmin',
'rev',
'uglify',
'usemin',
'htmlmin',
'json-minify',
'replace:control',
'replace:urls',
'compress'
Expand All @@ -663,31 +699,39 @@ module.exports = function (grunt) {
'build'
]);

grunt.registerTask('languages','',function(){
var fs = require('fs');
var done = this.async();
fs.readFile('app/index.html', 'utf8', function (err,data) {
if (err) {
return console.log(err);
grunt.registerTask('languages','',function () {
var target = grunt.option('target');
var translationsFolder = '.tmp/translations/';
var targetFile = '.tmp/index.html';
if (target === 'build' || target === 'testBuild') {
translationsFolder = 'dist/translations/';
targetFile = 'dist/index.html';
}

var fs = require('fs');
var done = this.async();
fs.readFile('app/index.html', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var html = '';
grunt.file.recurse(translationsFolder, function(abspath, rootdir, subdir, filename){
if (subdir !== undefined) { return; }
if (filename.indexOf('disabled') === -1) {
var languageCode = filename.split('.')[0];
var language = languageCode.split('_')[0];
html += '<li><a ng-click="header.changeLang(\''+languageCode+'\')"><span class="lang-sm lang-lbl-full" lang="'+language+'"></span></a></li>';
}
var html = '';
grunt.file.recurse('app/translations/', function(abspath, rootdir, subdir, filename){
if (subdir !== undefined) { return; }
if (filename.indexOf('disabled') === -1) {
var languageCode = filename.split('.')[0];
var language = languageCode.split('_')[0];
html += '<li><a ng-click="header.changeLang(\''+languageCode+'\')"><span class="lang-sm lang-lbl-full" lang="'+language+'"></span></a></li>';
}
});
var resultStart = data.split('<!-- languages-start -->');
var resultEnd = data.split('<!-- languages-end -->');
var res = resultStart[0] + '<!-- languages-start -->' + html + '<!-- languages-end -->' + resultEnd[1];

fs.writeFile('.tmp/index.html', res, 'utf8', function (err) {
if (err) { return console.log(err); }
// grunt.file.copy('.tmp/index.html','app/index.html');
done();
});
});
var resultStart = data.split('<!-- languages-start -->');
var resultEnd = data.split('<!-- languages-end -->');
var res = resultStart[0] + '<!-- languages-start -->' + html + '<!-- languages-end -->' + resultEnd[1];

fs.writeFile(targetFile, res, 'utf8', function (err) {
if (err) { return console.log(err); }
// grunt.file.copy('.tmp/index.html','app/index.html');
done();
});
});
});
};
1 change: 0 additions & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
</a>
<ul uib-dropdown-menu class="dropdown-menu" role="menu">
<!-- languages-start -->
<li><a ng-click="header.changeLang('de_DE')"><span class="lang-sm lang-lbl-full" lang="de"></span></a></li><li><a ng-click="header.changeLang('en_US')"><span class="lang-sm lang-lbl-full" lang="en"></span></a></li><li><a ng-click="header.changeLang('es_CO')"><span class="lang-sm lang-lbl-full" lang="es"></span></a></li><li><a ng-click="header.changeLang('fr_FR')"><span class="lang-sm lang-lbl-full" lang="fr"></span></a></li><li><a ng-click="header.changeLang('it_IT')"><span class="lang-sm lang-lbl-full" lang="it"></span></a></li><li><a ng-click="header.changeLang('ms_MY')"><span class="lang-sm lang-lbl-full" lang="ms"></span></a></li><li><a ng-click="header.changeLang('nl_NL')"><span class="lang-sm lang-lbl-full" lang="nl"></span></a></li><li><a ng-click="header.changeLang('pl_PL')"><span class="lang-sm lang-lbl-full" lang="pl"></span></a></li><li><a ng-click="header.changeLang('pt_BR')"><span class="lang-sm lang-lbl-full" lang="pt"></span></a></li><li><a ng-click="header.changeLang('ru_RU')"><span class="lang-sm lang-lbl-full" lang="ru"></span></a></li>
<!-- languages-end -->
</ul>
</li>
Expand Down
143 changes: 0 additions & 143 deletions app/translations/angular/angular-locale_de.js

This file was deleted.

Loading

0 comments on commit daffd7f

Please sign in to comment.