-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(i18n): remove unnecessary po files
- Loading branch information
1 parent
663d9ad
commit bf7f586
Showing
23 changed files
with
86 additions
and
12,025 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,4 @@ | |
. "$(dirname "$0")/_/husky.sh" | ||
|
||
npx lint-staged | ||
npm run translations:extract | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* eslint-disable no-continue */ | ||
import gettextParser from "gettext-parser"; | ||
|
||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
const translateText = async (input, language) => { | ||
const response = await fetch( | ||
`https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${language}&dt=t&q=${encodeURI( | ||
input | ||
)}` | ||
); | ||
|
||
if (!response.ok) { | ||
const error = await response.json(); | ||
throw new Error(`Failed to translate text: ${JSON.stringify(error)}`); | ||
} | ||
|
||
return (await response.json())[0][0][0]; | ||
}; | ||
|
||
const getFileMessages = (folderPath, file) => { | ||
const poContent = fs.readFileSync(path.join(folderPath, file), "utf8"); | ||
const po = gettextParser.po.parse(poContent); | ||
|
||
return [ | ||
po, | ||
Object.values(po.translations[""]).map((message) => ({ | ||
id: message.msgid, | ||
translation: message.msgstr[0], | ||
})), | ||
]; | ||
}; | ||
|
||
async function translatePoFiles(folderPath) { | ||
const files = fs.readdirSync(folderPath); | ||
|
||
for (const file of files) { | ||
if (path.extname(file) !== ".po") { | ||
continue; | ||
} | ||
|
||
const language = file.split(".")[0]; | ||
|
||
if (language === "pseudo") { | ||
continue; | ||
} | ||
|
||
const [po, fileMessages] = getFileMessages(folderPath, file); | ||
|
||
for (const message of fileMessages) { | ||
if (message.translation) { | ||
continue; | ||
} | ||
|
||
if (!message.id) { | ||
continue; | ||
} | ||
|
||
const translation = await translateText(message.id, language); | ||
|
||
console.log(`${language}: ${message.id} => ${translation}`); | ||
|
||
po.translations[""][message.id] = { | ||
msgid: message.id, | ||
msgstr: [translation], | ||
}; | ||
} | ||
|
||
fs.writeFileSync(path.join(folderPath, file), gettextParser.po.compile(po)); | ||
} | ||
} | ||
|
||
translatePoFiles("./src/translations/locales"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.