google-spreadsheet-translation-sync 1.3.10 → 1.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -16,4 +16,7 @@ module.exports.exportToSpreadsheet = require('./src/export-to-spreadsheet');
16
16
  module.exports.importFromSpreadsheet = require('./src/import-from-spreadsheet');
17
17
 
18
18
 
19
- module.exports.possibleTranslationFormats = require('./src/util/constraints').translationFormats;
19
+ module.exports.possibleTranslationFormats = require('./src/util/constraints').translationFormats;
20
+
21
+
22
+ module.exports.interaction = require('./src/interaction');
package/interact.js ADDED
@@ -0,0 +1 @@
1
+ require('./src/interaction')();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "google-spreadsheet-translation-sync",
3
- "version": "1.3.10",
3
+ "version": "1.3.12",
4
4
  "description": "A plugin to read and write i18n translationsfrom and to google spreadsheets ",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -43,6 +43,7 @@
43
43
  "shell": "^0.5.0"
44
44
  },
45
45
  "dependencies": {
46
+ "@inquirer/prompts": "^3.0.3",
46
47
  "async": "^2.6.3",
47
48
  "fast-csv": "^4.3",
48
49
  "gettext-parser": "^4.0.2",
@@ -0,0 +1,144 @@
1
+ /**
2
+ * @param {string[]} translationFiles - an array of files
3
+ * @param {{translationFormat: string, mode: string, spreadsheetId: string, gid : string, credentials: {}, keyId: string, fileBaseName: string, namespaces: boolean, defaultLocaleName: string}} options
4
+ * @param {function} callback
5
+ */
6
+ const fs = require("fs");
7
+ const async = require("async");
8
+ const path = require("path");
9
+
10
+ module.exports = async function () {
11
+ const async= require('async')
12
+ const path = require('path');
13
+ const prompts = require('@inquirer/prompts');
14
+ const fs = require('fs');
15
+
16
+ const options = {
17
+ keyId: 'key',
18
+ gid: '0',
19
+ credentials: require('../test/data/google-test-access.json'),
20
+ fileBaseName: '',
21
+ namespaces: false,
22
+ translationFormat: 'locale_json',
23
+ defaultLocaleName: 'default',
24
+ namespaceSeparator: '-'
25
+ }
26
+
27
+ const TRANSLATION_FORMATS = require('./util/constraints').TRANSLATION_FORMATS
28
+
29
+ options.translationFormat = await prompts.select({
30
+ message: 'Select the translation format of your project',
31
+ choices: Object.values(TRANSLATION_FORMATS).map((format) => {
32
+ return {value: format};
33
+ })
34
+ });
35
+
36
+ const h = require('./handler');
37
+ const handler = h.getHandler(options.translationFormat);
38
+
39
+ if (options.translationFormat === TRANSLATION_FORMATS.PROPERTIES) {
40
+ options.namespaces = true;
41
+ options.namespaceSeparator = await prompts.input({
42
+ message: 'Please set the namespace separator',
43
+ default: options.namespaceSeparator
44
+ });
45
+ }
46
+
47
+ // get all the files
48
+ const folder = await prompts.input({
49
+ message: 'Folder of your translation files (relative to ' + process.cwd() + ')'
50
+ });
51
+
52
+ const files = fs.readdirSync(folder).map(file => {
53
+ return path.resolve(folder, file);
54
+ });
55
+
56
+ let data = {};
57
+
58
+ async.each(files, function (file, cb) {
59
+
60
+ const extension = path.extname(file);
61
+ const fileName = path.basename(file, extension);
62
+ let namespace = '';
63
+ let localeKey;
64
+
65
+ // namespace based parsing required?
66
+ if (options.namespaces) {
67
+ const regex = /^(\w*?)([\-_])([\w\-]{2,5})$/gi
68
+ const matches = regex.exec(fileName);
69
+
70
+ if (!matches) {
71
+ // we assume, that the whole filename is the namespace
72
+ localeKey = options.defaultLocaleName ? options.defaultLocaleName : 'default'
73
+ namespace = fileName;
74
+ } else {
75
+ namespace = matches[1];
76
+ localeKey = matches[3];
77
+ }
78
+
79
+ if (!data[namespace]) {
80
+ data[namespace] = {};
81
+ }
82
+
83
+ } else {
84
+ localeKey = fileName.substring(options.fileBaseName.length + (options.namespaceSeparator ? options.namespaceSeparator.length : 0));
85
+ }
86
+
87
+ handler.getTranslationKeys(file, function (tData) {
88
+ if (options.namespaces) {
89
+ data[namespace][localeKey] = tData;
90
+ } else {
91
+ data[localeKey] = tData;
92
+ }
93
+
94
+ cb();
95
+ });
96
+ }, async (err) => {
97
+
98
+ switch (await prompts.select({
99
+ message: 'What would you like to do?',
100
+ choices: [
101
+ {value: 'export_key', name: "Export a single key"}
102
+ ]
103
+ })) {
104
+ case 'export_key':
105
+
106
+ const key = await prompts.input({
107
+ message: 'Which key?'
108
+ });
109
+ let namespace = '';
110
+
111
+ if (options.namespaces) {
112
+ namespace = await prompts.select({
113
+ message: 'From which namespace?',
114
+ choices: Object.keys(data).map(value => {
115
+ return {value}
116
+ })
117
+ });
118
+
119
+ data = data[namespace];
120
+ }
121
+
122
+
123
+ const head = ['key'];
124
+ const line = [key];
125
+
126
+ Object.keys(data).forEach(locale => {
127
+ head.push(locale);
128
+ line.push(data[locale][key] || '')
129
+ })
130
+ // default export as Jira MD for now
131
+ console.log(`||${head.join('||')}||
132
+ |${line.join('|')}|
133
+ `)
134
+
135
+ break;
136
+ }
137
+
138
+
139
+
140
+ })
141
+
142
+
143
+
144
+ }
package/test/test.js CHANGED
@@ -11,9 +11,9 @@ const async = require('async');
11
11
 
12
12
  const testSheetId = '1ZJK1G_3wrEo9lnu1FenjOzSy3uoAi-RLWbph1cI6DWI'; // https://docs.google.com/spreadsheets/d/1ZJK1G_3wrEo9lnu1FenjOzSy3uoAi-RLWbph1cI6DWI/edit#gid=0
13
13
  const testWorksheetId = '1209225803';
14
- const testSheetId_gettext = '1CRvX4TCxUGCcs_MtKC5BdEViHYzYzLXdqtbuVaAXfKc';
14
+ const testSheetId_gettext = '1CRvX4TCxUGCcs_MtKC5BdEViHYzYzLXdqtbuVaAXfKc'; // https://docs.google.com/spreadsheets/d/1CRvX4TCxUGCcs_MtKC5BdEViHYzYzLXdqtbuVaAXfKc/edit#gid=0
15
15
  const testSheetId_properties = '1Z0Mpbf6lgdGiuiHlpb9DENVfKxkxSRwcfQEDYrgokEE'; // https://docs.google.com/spreadsheets/d/1Z0Mpbf6lgdGiuiHlpb9DENVfKxkxSRwcfQEDYrgokEE/edit#gid=0
16
- const testSheetId_yaml = '1Ml3jLK6RgbPQ4e7XZpILvB-XWMeziHpwdxS8r4AYNtE';
16
+ const testSheetId_yaml = '1Ml3jLK6RgbPQ4e7XZpILvB-XWMeziHpwdxS8r4AYNtE'; // https://docs.google.com/spreadsheets/d/1Ml3jLK6RgbPQ4e7XZpILvB-XWMeziHpwdxS8r4AYNtE/edit#gid=0
17
17
  const timeout = 20000;
18
18
 
19
19
  // preparations
@@ -21,10 +21,6 @@ const timeout = 20000;
21
21
  var tmpFile = tmp.fileSync({postfix: '.csv'});
22
22
 
23
23
  const csv = require('fast-csv');
24
- const fileUtils = require("../src/util/file-utils");
25
- const yamlHandler = require("../src/handlers/yaml");
26
- const rimraf = require("rimraf");
27
- const yaml = require("js-yaml");
28
24
  const testFile = tmpFile.name;
29
25
  const csvData = [
30
26
  ['key', 'default', 'de', 'it', 'fr', 'pl', 'hu'],
@@ -323,12 +319,15 @@ const tests = [
323
319
 
324
320
  if (!err) {
325
321
  const testFile = path.resolve(translationRoot + '/other.properties');
322
+ const testFilePl = path.resolve(translationRoot + '/other_pl.properties');
326
323
  const PropertiesReader = require('properties-reader');
327
324
 
328
325
  const props = PropertiesReader(testFile);
326
+ const propsPl = PropertiesReader(testFilePl);
329
327
 
330
328
  // expect(Object.keys(props.getAllProperties()).length).to.equal(csvData.length - 1); // without the header
331
329
  expect(props.get(csvData[2][0])).to.equal(csvData[2][1]);
330
+ expect(propsPl.get(csvData[1][0])).to.equal(csvData[1][5]);
332
331
  }
333
332
 
334
333
  done();
@@ -352,15 +351,18 @@ const tests = [
352
351
 
353
352
  const translationRoot = ensureFolder(path.resolve('./test/translations/' + options.translationFormat + '/'));
354
353
  const testFile = path.resolve(translationRoot + '/default.json');
354
+ const testFilePl = path.resolve(translationRoot + '/pl.json');
355
355
 
356
356
  app.importFromSpreadsheet(translationRoot, options, function (err) {
357
357
  expect(err).to.be.null
358
358
 
359
359
  if (!err) {
360
360
  const defaultKeys = require(testFile);
361
+ const plKeys = require(testFilePl);
361
362
 
362
363
  // expect(Object.keys(defaultKeys).length).to.equal(326);
363
364
  expect(defaultKeys[csvData[2][0]]).to.equal(csvData[2][1]);
365
+ expect(plKeys[csvData[1][0]]).to.equal(csvData[1][5]);
364
366
  }
365
367
 
366
368
  done();
@@ -385,6 +387,7 @@ const tests = [
385
387
 
386
388
  const translationRoot = ensureFolder(path.resolve('./test/translations/' + options.translationFormat + '/'));
387
389
  const testFile = path.resolve(translationRoot + '/' + baseName + '-en.po');
390
+ const testFilePl = path.resolve(translationRoot + '/' + baseName + '-pl.po');
388
391
 
389
392
  app.importFromSpreadsheet(translationRoot, options, function (err) {
390
393
  expect(err).to.be.null
@@ -395,9 +398,11 @@ const tests = [
395
398
  expect(fs.existsSync(testFile)).to.equal(true);
396
399
 
397
400
  const po = require('gettext-parser').po.parse(fs.readFileSync(testFile));
401
+ const poPl = require('gettext-parser').po.parse(fs.readFileSync(testFilePl));
398
402
  const translations = po.translations[''];
399
-
403
+ const translationsPl = poPl.translations[''];
400
404
  expect(translations.add_address.msgstr[0]).to.equal("Add new address");
405
+ expect(translationsPl['additional.news'].msgstr[0]).to.equal('czecz 2242');
401
406
  }
402
407
 
403
408
  done();