google-spreadsheet-translation-sync 1.3.7 → 1.3.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "google-spreadsheet-translation-sync",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "description": "A plugin to read and write i18n translationsfrom and to google spreadsheets ",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -3,6 +3,8 @@
3
3
  const fs = require('fs');
4
4
  const withoutError = require('../helpers').withoutError
5
5
  const constraints = require('../util/constraints');
6
+ const {resolveStructureToTree} = require("../util/structure-utils");
7
+
6
8
 
7
9
  module.exports.loadTranslationFile = function (filePath, callback) {
8
10
 
@@ -13,7 +15,13 @@ module.exports.loadTranslationFile = function (filePath, callback) {
13
15
  } else {
14
16
 
15
17
  fs.readFile(filePath, function (err, data) {
16
- callback(err ? {} : JSON.parse(data));
18
+
19
+ const existingTranslations = err ? {} : JSON.parse(data);
20
+ const cleanResult = {};
21
+
22
+ resolveStructureToTree(cleanResult, existingTranslations);
23
+
24
+ callback(cleanResult);
17
25
  })
18
26
  }
19
27
  });
@@ -72,24 +80,7 @@ module.exports.updateTranslations = function (translationData, translationRootFo
72
80
  mod.loadTranslationFile(file, function (translations) {
73
81
  const potentiallyUpdatedTranslations = translationData[locale][namespace];
74
82
 
75
- Object.keys(potentiallyUpdatedTranslations).forEach((key) => {
76
- const parts = key.split('.');
77
- let tree = translations;
78
-
79
- while (parts.length > 0) {
80
- const part = parts.shift();
81
-
82
- if (parts.length === 0) {
83
- tree[part] = potentiallyUpdatedTranslations[key];
84
- } else {
85
- if (!tree[part]) {
86
- tree[part] = {}
87
- }
88
-
89
- tree = tree[part];
90
- }
91
- }
92
- });
83
+ resolveStructureToTree(translations, potentiallyUpdatedTranslations);
93
84
 
94
85
  // now we write
95
86
  fs.writeFile(file, JSON.stringify(translations, null, 4), function (err) {
@@ -2,6 +2,7 @@ const fs = require("fs");
2
2
  const yaml = require('js-yaml');
3
3
  const constraints = require("../util/constraints");
4
4
  const {withoutError} = require("../helpers");
5
+ const {resolveStructureToTree} = require("../util/structure-utils");
5
6
 
6
7
  module.exports.loadTranslationFile = function (filePath, callback) {
7
8
  if (fs.existsSync(filePath)) {
@@ -65,24 +66,7 @@ module.exports.updateTranslations = function (translationData, translationRootFo
65
66
  mod.loadTranslationFile(file, function (translations) {
66
67
  const potentiallyUpdatedTranslations = translationData[locale][namespace];
67
68
 
68
- Object.keys(potentiallyUpdatedTranslations).forEach((key) => {
69
- const parts = key.split('.');
70
- let tree = translations;
71
-
72
- while (parts.length > 0) {
73
- const part = parts.shift();
74
-
75
- if (parts.length === 0) {
76
- tree[part] = potentiallyUpdatedTranslations[key];
77
- } else {
78
- if (!tree[part]) {
79
- tree[part] = {}
80
- }
81
-
82
- tree = tree[part];
83
- }
84
- }
85
- });
69
+ resolveStructureToTree(translations, potentiallyUpdatedTranslations);
86
70
 
87
71
  // now we write
88
72
  fs.writeFile(file, yaml.dump(translations, {indent: 4}), function (err) {
@@ -0,0 +1,27 @@
1
+ module.exports.resolveStructureToTree = (originalTree, potentialTreeUpdates) => {
2
+
3
+ Object.keys(potentialTreeUpdates).forEach((key) => {
4
+ const parts = key.split('.');
5
+ let tree = originalTree;
6
+
7
+ // remove the full key, if present
8
+ if (originalTree[key]) {
9
+ delete originalTree[key];
10
+ }
11
+
12
+ // add it again in a clean fassion
13
+ while (parts.length > 0) {
14
+ const part = parts.shift();
15
+
16
+ if (parts.length === 0) {
17
+ tree[part] = potentialTreeUpdates[key];
18
+ } else {
19
+ if (!tree[part]) {
20
+ tree[part] = {}
21
+ }
22
+
23
+ tree = tree[part];
24
+ }
25
+ }
26
+ })
27
+ }