Skip to content

Commit 13af988

Browse files
dgaponovYaros1ove
authored andcommitted
ci: fix ai json translations (#497)
1 parent ecd54f3 commit 13af988

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

.github/workflows/update-translations.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ jobs:
138138
Do not translate the library name in the source file if it looks like the name of the library (the exception is Themer, it can be translated). \
139139
For example, don't translate @gravity/uikit or Axios Wrapper - they need to be left in place.
140140
141-
merge-pr:
141+
fix-ai-translations:
142142
needs:
143143
- create-pr
144144
- translations
@@ -148,19 +148,20 @@ jobs:
148148
- uses: actions/checkout@v4
149149
with:
150150
ref: refs/pull/${{ needs.create-pr.outputs.pr_number }}/head
151-
fetch-depth: 2
152-
- name: Merge PR
151+
fetch-depth: 0
152+
- uses: actions/setup-node@v3
153+
with:
154+
node-version: 20
155+
- name: Fix AI translations
153156
env:
154157
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
155158
run: |
156-
git checkout update-translations
159+
node scripts/fix-ai-translations.mjs
160+
157161
git config user.email ""
158162
git config user.name "Update translations action"
159-
git checkout -b update-translations
160163
rm -rf temporary-file.txt
161164
git add .
162165
163-
export COMMIT_MESSAGE="remove temporary file"
164-
git commit -m "$COMMIT_MESSAGE"
165-
git push --set-upstream origin update-translations --force
166-
gh pr merge ${{ needs.create-pr.outputs.pr_number }} --auto --squash
166+
git commit -m "remove temporary files and fix AI translations"
167+
git push origin HEAD

scripts/fix-ai-translations.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import {fileURLToPath} from 'url';
4+
5+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
6+
const localesDir = path.join(__dirname, '..', 'public', 'locales');
7+
8+
function getAllJsonFiles(dir) {
9+
let results = [];
10+
const list = fs.readdirSync(dir);
11+
12+
list.forEach((file) => {
13+
const filePath = path.join(dir, file);
14+
const stat = fs.statSync(filePath);
15+
16+
if (stat.isDirectory()) {
17+
results = results.concat(getAllJsonFiles(filePath));
18+
} else if (path.extname(file) === '.json') {
19+
results.push(filePath);
20+
}
21+
});
22+
23+
return results;
24+
}
25+
26+
function processJsonFile(filePath) {
27+
let content = fs.readFileSync(filePath, 'utf8').trim();
28+
let modified = false;
29+
30+
// Remove ```json at the beginning of the file
31+
if (content.startsWith('```json')) {
32+
content = content.substring(7);
33+
modified = true;
34+
}
35+
36+
// Remove ``` at the end of the file
37+
if (content.endsWith('```')) {
38+
content = content.substring(0, content.length - 3);
39+
modified = true;
40+
}
41+
42+
if (modified) {
43+
fs.writeFileSync(filePath, content.trim() + '\n', 'utf8');
44+
console.log(`File ${filePath} was corrected`);
45+
}
46+
}
47+
48+
function main() {
49+
console.log(`Fixing AI translations in directory: ${localesDir}`);
50+
51+
try {
52+
const jsonFiles = getAllJsonFiles(localesDir);
53+
jsonFiles.forEach(processJsonFile);
54+
console.log('Processing completed successfully');
55+
} catch (error) {
56+
console.error('An error occurred:', error);
57+
}
58+
}
59+
60+
main();

0 commit comments

Comments
 (0)