gulp-rename-langs.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var through = require('through2');
  2. var gutil = require('gulp-util');
  3. var PluginError = gutil.PluginError;
  4. const PLUGIN_NAME = 'gulp-rename-langs.js';
  5. module.exports = function(options) {
  6. // Creating a stream through which each file will pass
  7. var stream = through.obj(function(file, enc, callback) {
  8. if (file.isBuffer()) {
  9. var content = file.contents.toString();
  10. var parsed = JSON.parse(content);
  11. var keys = Object.keys(parsed);
  12. var lang = keys[0];
  13. // Changes en-us to en_US.
  14. var pieces = lang.split(/\-/);
  15. if (pieces.length > 1) {
  16. lang = pieces[0] + '_' + pieces[1].toUpperCase();
  17. parsed[lang] = parsed[keys[0]];
  18. delete parsed[keys[0]];
  19. file.contents = new Buffer(JSON.stringify(parsed), 'utf-8');
  20. }
  21. } else if (file.isStream()) {
  22. // Streams not supported.
  23. this.emit(
  24. 'error',
  25. new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported.')
  26. );
  27. return callback();
  28. }
  29. // Anything else just falls through.
  30. this.push(file);
  31. return callback();
  32. });
  33. // returning the file stream
  34. return stream;
  35. };