blob: a17e8466b79f5f185c4529ffdfe1a663532cfc3b [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016var gulp = require('gulp');
17var fs = require('fs');
18var replace = require('gulp-replace');
19var clean = require('gulp-clean');
20var mkdirp = require('mkdirp');
21
22/**
23 *
Michael Landoefa037d2017-02-19 12:57:33 +020024 * @param options.localesPath
25 * @param options.lang = options.lang
26 *
27 * @returns {string}
28 */
29function composeLocalesDirPath(options) {
AviZi280f8012017-06-09 02:39:56 +030030 return options.localesPath + options.lang;
Michael Landoefa037d2017-02-19 12:57:33 +020031}
32
33/**
34 *
Michael Landoefa037d2017-02-19 12:57:33 +020035 * @param options.localesPath
36 * @param options.lang
37 *
38 * @returns {string}
39 */
40function composeLocaleFilePath(options) {
41 return composeLocalesDirPath(options) + '/locale.json';
42}
43
44
45/**
AviZi280f8012017-06-09 02:39:56 +030046 *
Michael Landoefa037d2017-02-19 12:57:33 +020047 * @param options.localesPath
48 * @param options.lang = options.lang
Michael Landoefa037d2017-02-19 12:57:33 +020049 */
50function 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 });
Michael Landoefa037d2017-02-19 12:57:33 +020062}
63
64/**
65 *
66 * @param options
67 * @param options.outputPath
68 * @param options.localesPath
69 * @param options.lang = options.lang
70 *
71 */
72function 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) {
AviZi280f8012017-06-09 02:39:56 +030099 reject(err);
100 });
Michael Landoefa037d2017-02-19 12:57:33 +0200101 });
102 });
103}
104
105module.exports = i18nTask;