Skip to content

Commit 4c203b1

Browse files
committed
fix(json): \n replace to \n in
1 parent 72d9c91 commit 4c203b1

File tree

5 files changed

+1819
-9
lines changed

5 files changed

+1819
-9
lines changed

index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,20 @@ const makeJSON = (rows, locale, separator = ".") => {
5656
if (!str) return;
5757

5858
if (keys.length === 1) {
59-
json[`${key}`] = str;
59+
json[prune(key)] = prune(str);
6060
} else {
6161
let nested = json;
6262
const lastIndex = keys.length - 1;
6363

6464
for (let i = 0; i < keys.length - 1; i++) {
65-
const currentKey = keys[i];
65+
const currentKey = prune(keys[i]);
6666

6767
if (!nested[currentKey]) {
6868
nested[currentKey] = {};
6969
}
7070
nested = nested[currentKey];
7171
}
72-
nested[keys[lastIndex]] = str;
72+
nested[prune(keys[lastIndex])] = prune(str);
7373
}
7474
});
7575

@@ -93,6 +93,13 @@ const writeJSON = (source, path) => {
9393
);
9494
};
9595

96+
/**
97+
* @description Remove invalid strings generated during encoding
98+
*/
99+
const prune = (string) => {
100+
return string.replaceAll("\\n", "\n");
101+
};
102+
96103
const generate = async (
97104
doc,
98105
locales,
@@ -103,12 +110,9 @@ const generate = async (
103110
locales.forEach(async (locale) => {
104111
const sheetRows = await readSheet(doc, sheetTitle);
105112
const json = makeJSON(sheetRows, locale, depthSeparator);
113+
const source = JSON.stringify(json, null, 2);
106114

107-
writeJSON(
108-
JSON.stringify(json, null, 2),
109-
`${output}/${locale}.json`,
110-
locale
111-
);
115+
writeJSON(source, `${output}/${locale}.json`, locale);
112116
});
113117
};
114118

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "translation-gen",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "Generate translate json by google sheet automatically",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)