12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- var fs = require('fs');
- var crypto = require('crypto');
- const getParams = query => {
- if (!query) {
- return { };
- }
- return (/^[?#]/.test(query) ? query.slice(1) : query)
- .split('&')
- .reduce((params, param) => {
- let [ key, value ] = param.split('=');
- params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
- return params;
- }, { });
- };
- module.exports = function(source) {
- const params = getParams(this.query);
- const chunk = crypto.createHash('md5').update(this.resource).digest("hex");
- if (params.path === undefined) {
- this.emitError('path parameter must be provided');
- }
- if(this.cacheable) this.cacheable();
- const regex = /<translate(\s*.*)>(.*)<\/translate>/mg;
- let data = {}
- // replace all <translate param="value">Something</translate> into <span param="value">{this.props.p.t('Something')}</span>
- const result = source.replace(regex, function(match, p1, p2) {
- const md5 = crypto.createHash('md5').update(p2).digest("hex");
- data[md5] = p2;
- if(p1.trim() == 'span') {
- return "<span"+p1+">{this.props.p.t('"+md5+"')}</span>";
- } else if (p1.trim() == 'nojsx') {
- return "this.props.p.t('"+md5+"')";
- } else {
- return "{this.props.p.t('"+md5+"')}";
- }
- });
- const path = params.path+"/_"+chunk+".json";
- if (Object.keys(data).length !== 0) {
- fs.writeFile(path, JSON.stringify(data), function(err) {
- if(err) {
- return console.log(err);
- }
- });
- }
- return result;
- };
|