google-spreadsheet-translation-sync 1.3.11 → 1.3.13

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.11",
3
+ "version": "1.3.13",
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,146 @@
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
+ if (data[locale][key]) {
128
+ head.push(locale);
129
+ line.push(data[locale][key] || '')
130
+ }
131
+ })
132
+ // default export as Jira MD for now
133
+ console.log(`||${head.join('||')}||
134
+ |${line.join('|')}|
135
+ `)
136
+
137
+ break;
138
+ }
139
+
140
+
141
+
142
+ })
143
+
144
+
145
+
146
+ }