a17e8466b79f5f185c4529ffdfe1a663532cfc3b
[sdc.git] / openecomp-ui / tools / gulp / tasks / i18n.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
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');
21
22 /**
23  *
24  * @param options.localesPath
25  * @param options.lang = options.lang
26  *
27  * @returns {string}
28  */
29 function composeLocalesDirPath(options) {
30         return options.localesPath + options.lang;
31 }
32
33 /**
34  *
35  * @param options.localesPath
36  * @param options.lang
37  *
38  * @returns {string}
39  */
40 function composeLocaleFilePath(options) {
41         return composeLocalesDirPath(options) + '/locale.json';
42 }
43
44
45 /**
46  *
47  * @param options.localesPath
48  * @param options.lang = options.lang
49  */
50 function ensureLocalesDir(options) {
51
52         return new Promise(function (resolve, reject) {
53                 mkdirp(composeLocalesDirPath(options), function (err) {
54                         if (err) {
55                                 reject(err);
56                         }
57                         else {
58                                 resolve();
59                         }
60                 });
61         });
62 }
63
64 /**
65  *
66  * @param options
67  * @param options.outputPath
68  * @param options.localesPath
69  * @param options.lang = options.lang
70  *
71  */
72 function i18nTask(options) {
73
74         var i18nJson = {};
75
76         function addWord(expr) {
77                 var word = expr.substring('i18n(\''.length, expr.length - 1);
78                 i18nJson[word] = word;
79                 return expr;
80         }
81
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))
86                                 .pipe(clean())
87                                 .pipe(gulp.dest('./'))
88                                 .on('end', function () {
89
90                                         var i18nJsonWrapper = { dataWrapperArr: ["I18N_IDENTIFIER_START", i18nJson, "I18N_IDENTIFIER_END"] , i18nDataIdx: 1};
91
92                                         fs.writeFile(composeLocaleFilePath(options), JSON.stringify(i18nJsonWrapper), function (err) {
93                                                 if (err) {
94                                                         reject(err);
95                                                 }
96                                                 else resolve();
97                                         });
98                                 }).on('error', function (err) {
99                                 reject(err);
100                         });
101                 });
102         });
103 }
104
105 module.exports = i18nTask;