AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 24 | * @param options.localesPath |
| 25 | * @param options.lang = options.lang |
| 26 | * |
| 27 | * @returns {string} |
| 28 | */ |
| 29 | function composeLocalesDirPath(options) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 30 | return options.localesPath + options.lang; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /** |
| 34 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | /** |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 46 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 47 | * @param options.localesPath |
| 48 | * @param options.lang = options.lang |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 99 | reject(err); |
| 100 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 101 | }); |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | module.exports = i18nTask; |