Skip to content

Commit

Permalink
fix(json): \n replace to \n in
Browse files Browse the repository at this point in the history
  • Loading branch information
seungwoohong committed Jan 4, 2024
1 parent 72d9c91 commit 4c203b1
Show file tree
Hide file tree
Showing 5 changed files with 1,819 additions and 9 deletions.
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ const makeJSON = (rows, locale, separator = ".") => {
if (!str) return;

if (keys.length === 1) {
json[`${key}`] = str;
json[prune(key)] = prune(str);
} else {
let nested = json;
const lastIndex = keys.length - 1;

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

if (!nested[currentKey]) {
nested[currentKey] = {};
}
nested = nested[currentKey];
}
nested[keys[lastIndex]] = str;
nested[prune(keys[lastIndex])] = prune(str);
}
});

Expand All @@ -93,6 +93,13 @@ const writeJSON = (source, path) => {
);
};

/**
* @description Remove invalid strings generated during encoding
*/
const prune = (string) => {
return string.replaceAll("\\n", "\n");
};

const generate = async (
doc,
locales,
Expand All @@ -103,12 +110,9 @@ const generate = async (
locales.forEach(async (locale) => {
const sheetRows = await readSheet(doc, sheetTitle);
const json = makeJSON(sheetRows, locale, depthSeparator);
const source = JSON.stringify(json, null, 2);

writeJSON(
JSON.stringify(json, null, 2),
`${output}/${locale}.json`,
locale
);
writeJSON(source, `${output}/${locale}.json`, locale);
});
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "translation-gen",
"version": "0.1.4",
"version": "0.1.5",
"description": "Generate translate json by google sheet automatically",
"main": "index.js",
"type": "module",
Expand Down
Loading

0 comments on commit 4c203b1

Please sign in to comment.