From 13cd887d978bf9e19bb603e29513864a0115545d Mon Sep 17 00:00:00 2001 From: lutinglt Date: Tue, 2 Dec 2025 17:33:57 +0800 Subject: [PATCH] add translate --- .github/workflows/build.yml | 14 +++++++-- .github/workflows/release.yml | 7 +++-- package.json | 7 +++-- scripts/translate.cjs | 56 +++++++++++++++++++++++++++++++++++ scripts/translate.ts | 5 ---- 5 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 scripts/translate.cjs delete mode 100644 scripts/translate.ts diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c63dc67..7507546 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,8 +28,6 @@ jobs: run: | npm install npm run build - env: - GH_TOKEN: ${{ github.token }} - name: Upload css assets if: steps.build.outcome == 'success' @@ -44,3 +42,15 @@ jobs: with: name: templates path: templates + + - name: Build locales + id: locales + if: steps.build.outcome =='success' + run: npm run tr + + - name: Upload locales assets + if: steps.locales.outcome =='success' + uses: actions/upload-artifact@v4 + with: + name: locales + path: dist/options/locale \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4e5514f..ef36801 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,7 +14,7 @@ jobs: - name: Build theme run: | npm install - npm run build + npm run release - name: Create release run: | tar -zcf dist/theme-github-base.tar.gz --remove-files \ @@ -29,8 +29,9 @@ jobs: tar -zcf dist/theme-github-extra-pink.tar.gz --remove-files \ dist/theme-github-pink-auto.css dist/theme-github-pink-light.css dist/theme-github-pink-dark.css dist/theme-github-pink-soft-dark.css - tar -zcf dist/theme-github-templates.tar.gz templates options + tar -zcf dist/theme-github-templates.tar.gz templates + tar -zcf dist/theme-github-translations.tar.gz --remove-files dist/options TAG="v$(npm run -s version)" - gh release create "$TAG" dist/* --notes-file CHANGELOG.md --draft -t $TAG + gh release create "$TAG" dist/*.tar.gz --notes-file CHANGELOG.md --draft -t $TAG env: GH_TOKEN: ${{ github.token }} diff --git a/package.json b/package.json index 93373e1..ba5921c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gitea-github-theme", - "version": "1.25.3", + "version": "1.25.3.rc", "type": "module", "scripts": { "dev": "vite build --mode dev", @@ -9,8 +9,9 @@ "format": "prettier --write .", "commit": "npm run lint && npm run format && npm run build", "version": "node scripts/version.cjs", - "install:clean": "npm cache clean --force && rm -rf node_modules package-lock.json && npm install", - "tr": "node scripts/translate.ts" + "tr": "node scripts/translate.cjs", + "release": "npm run build && npm run tr", + "install:clean": "npm cache clean --force && rm -rf node_modules package-lock.json && npm install" }, "devDependencies": { "@babel/preset-typescript": "^7.28.4", diff --git a/scripts/translate.cjs b/scripts/translate.cjs new file mode 100644 index 0000000..76a183b --- /dev/null +++ b/scripts/translate.cjs @@ -0,0 +1,56 @@ +const fs = require("fs"); +const path = require("path"); +const child_process = require("child_process"); +const dotenv = require("dotenv"); + +dotenv.config({ quiet: true }); +const rootDir = path.join(__dirname, ".."); + +const pkgPath = path.join(rootDir, "package.json"); +const pkg = JSON.parse(fs.readFileSync(pkgPath)); + +const githubSite = "https://raw.githubusercontent.com"; +const giteaRepo = "go-gitea/gitea"; +const githubBranchPath = "refs/heads/release"; +const githubTagPath = "refs/tags"; +const localePath = "options/locale"; + +const [major, minor, patch, tag = ""] = pkg.version.split("."); + +console.log("Version: ", pkg.version); +let versionPath = ""; +if (tag.includes("rc") || patch.includes("latest")) { + versionPath = `${githubBranchPath}/v${major}.${minor}`; +} else { + versionPath = `${githubTagPath}/v${major}.${minor}.${patch}`; +} + +const githubUrl = `${githubSite}/${giteaRepo}/${versionPath}/${localePath}`; + +const locales = fs.readdirSync(path.join(rootDir, localePath)).filter(file => file.endsWith(".ini")); + +// 使用立即执行异步函数 +(async () => { + for (const locale of locales) { + const localUrl = `${githubUrl}/${locale}`; + console.log("LocaleUrl: ", localUrl); + const themeLocale = fs.readFileSync(path.join(rootDir, localePath, locale), "utf-8"); + + const response = await fetch(localUrl); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + let content = await response.text(); + if (locale.includes("zh-CN")) { + content = content.replaceAll("工单", "议题").replaceAll("合并请求", "拉取请求"); + } + fs.mkdirSync(path.join(rootDir, "dist", localePath), { recursive: true }); + fs.writeFileSync(path.join(rootDir, "dist", localePath, locale), content + themeLocale); + } + + if (process.env.SSH_SERVER && process.env.GITEA_PATH && process.env.SSH_USER) { + const cmd = `scp -r dist/options ${process.env.SSH_USER}@${process.env.SSH_SERVER}:${process.env.GITEA_PATH}`; + console.log("[translate] exec:", cmd); + child_process.execSync(cmd, { stdio: "inherit" }); + } +})(); diff --git a/scripts/translate.ts b/scripts/translate.ts deleted file mode 100644 index db10d00..0000000 --- a/scripts/translate.ts +++ /dev/null @@ -1,5 +0,0 @@ -import * as dotenv from "dotenv"; - -dotenv.config({ quiet: true }); -// -console.log(process.env.GITEA_PATH);