2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
16 var gulp = require('gulp');
17 var fs = require('fs');
18 var replace = require('gulp-replace');
19 var clean = require('gulp-clean');
20 var mkdirp = require('mkdirp');
24 * @param options.localesPath
25 * @param options.lang = options.lang
29 function composeLocalesDirPath(options) {
30 return options.localesPath + options.lang;
35 * @param options.localesPath
40 function composeLocaleFilePath(options) {
41 return composeLocalesDirPath(options) + '/locale.json';
47 * @param options.localesPath
48 * @param options.lang = options.lang
50 function ensureLocalesDir(options) {
52 return new Promise(function (resolve, reject) {
53 mkdirp(composeLocalesDirPath(options), function (err) {
67 * @param options.outputPath
68 * @param options.localesPath
69 * @param options.lang = options.lang
72 function i18nTask(options) {
76 function addWord(expr) {
77 var word = expr.substring('i18n(\''.length, expr.length - 1);
78 i18nJson[word] = word;
82 return ensureLocalesDir(options).then(function () {
83 return new Promise(function(resolve, reject) {
84 gulp.src(options.outputPath + '**/*.js', {base: './'})
85 .pipe(replace(/i18n\('.*?'/g, addWord))
87 .pipe(gulp.dest('./'))
88 .on('end', function () {
90 var i18nJsonWrapper = { dataWrapperArr: ["I18N_IDENTIFIER_START", i18nJson, "I18N_IDENTIFIER_END"] , i18nDataIdx: 1};
92 fs.writeFile(composeLocaleFilePath(options), JSON.stringify(i18nJsonWrapper), function (err) {
98 }).on('error', function (err) {
105 module.exports = i18nTask;