From 2d3779018619a8f742c1e0a54196d8d8845577ce Mon Sep 17 00:00:00 2001 From: cihatata Date: Tue, 11 Mar 2025 23:58:26 +0300 Subject: [PATCH] feat: upload translation to POEditor --- .github/scripts/upload-translations.js | 64 ++++++++++++++++ .github/workflows/README.md | 28 +++++++ .github/workflows/upload-poeditor.yml | 101 +++++++++++++++++++++++++ src/locales/tr.json | 8 +- 4 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 .github/scripts/upload-translations.js create mode 100644 .github/workflows/upload-poeditor.yml diff --git a/.github/scripts/upload-translations.js b/.github/scripts/upload-translations.js new file mode 100644 index 000000000..aa23cf35f --- /dev/null +++ b/.github/scripts/upload-translations.js @@ -0,0 +1,64 @@ +import axios from "axios"; +import FormData from "form-data"; +import fs from "fs-extra"; + +// POEditor API information +const API_TOKEN = process.env.POEDITOR_API; +const PROJECT_ID = process.env.POEDITOR_PROJECT_ID; +const FILE_PATH = process.env.FILE_PATH; +const LANGUAGE = process.env.LANGUAGE; + +// POEditor API endpoint +const API_URL = 'https://api.poeditor.com/v2'; + +// Function to upload translations +async function uploadTranslations() { + try { + console.log(`Uploading translations for ${LANGUAGE} language from ${FILE_PATH}... test1`); + + // Check if file exists + if (!await fs.pathExists(FILE_PATH)) { + throw new Error(`File not found: ${FILE_PATH}`); + } + + // Read file content + const fileContent = await fs.readFile(FILE_PATH, 'utf8'); + + // Validate JSON format + try { + JSON.parse(fileContent); + } catch (error) { + throw new Error(`Invalid JSON format in ${FILE_PATH}: ${error.message}`); + } + + // Create form data for upload + const formData = new FormData(); + formData.append('api_token', API_TOKEN); + formData.append('id', PROJECT_ID); + formData.append('language', LANGUAGE); + formData.append('updating', 'terms_translations'); + formData.append('file', fs.createReadStream(FILE_PATH)); + formData.append('overwrite', '1'); + formData.append('sync_terms', '1'); + + // Upload to POEditor + const response = await axios.post(`${API_URL}/projects/upload`, formData, { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + } + }); + + if (response.data.response.status !== 'success') { + throw new Error(`Failed to upload translations: ${JSON.stringify(response.data)}`); + } + + console.log(`Successfully uploaded translations for ${LANGUAGE} language.`); + console.log(`Statistics: ${JSON.stringify(response.data.result)}`); + } catch (error) { + console.error('An error occurred while uploading translations:', error); + process.exit(1); + } +} + +// Run script +uploadTranslations(); diff --git a/.github/workflows/README.md b/.github/workflows/README.md index 11b04d657..774a816b3 100644 --- a/.github/workflows/README.md +++ b/.github/workflows/README.md @@ -39,3 +39,31 @@ If the workflow fails: 1. Check the GitHub Actions logs 2. Make sure your POEditor API token and project ID are correct 3. Ensure that the languages you specified exist in your POEditor project + +# POEditor Upload Workflow + +## Summary of Implemented Translation Workflow + +We have successfully created a GitHub Actions workflow that automatically uploads translation files to POEditor when changes are merged to the develop branch. Here's a summary of what we've implemented: +### Created Files + +1. .github/scripts/upload-translations.js + +- A Node.js script that handles the upload of translation files to POEditor +- Uses the POEditor API to upload JSON translation files +- Validates file existence and JSON format before uploading +- Provides detailed logging of the upload process + +2. .github/workflows/poeditor-upload-on-merge.yml - A GitHub Actions workflow that triggers when PRs are merged to the develop branch - Only runs when changes are made to files in the src/locales directory - Detects which translation files were changed in the PR - Extracts language codes from filenames (e.g., tr.json → "tr") - Calls the upload script for each changed file + ### Workflow Process +1. When a PR is merged to the develop branch, the workflow checks if any files in src/locales were modified. + +1. If translation files were changed, the workflow: + +- Sets up the necessary Node.js environment +- Installs required dependencies +- Identifies which specific translation files were changed +- For each changed file, extracts the language code and uploads to POEditor +- Provides status notifications about the upload process + +This automated workflow ensures that your translations are always in sync between your codebase and POEditor, eliminating the need for manual uploads and reducing the risk of translation inconsistencies. diff --git a/.github/workflows/upload-poeditor.yml b/.github/workflows/upload-poeditor.yml new file mode 100644 index 000000000..aecd8ca03 --- /dev/null +++ b/.github/workflows/upload-poeditor.yml @@ -0,0 +1,101 @@ +name: Upload Translations to POEditor on PR Merge + +on: + pull_request: + types: [closed] + branches: + - develop + paths: + - "src/locales/**" + +jobs: + upload-translations: + # Only run if the PR was merged (not just closed) or manually triggered + if: github.event.pull_request.merged == true' + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 # Fetch all history to get changed files + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: "18" + + - name: Create package.json for scripts + run: | + mkdir -p .github/scripts + cat > .github/scripts/package.json << EOF + { + "name": "poeditor-scripts", + "version": "1.0.0", + "private": true, + "type": "module", + "dependencies": { + "axios": "^1.6.0", + "fs-extra": "^11.1.1", + "form-data": "^4.0.0" + } + } + EOF + + - name: Install dependencies + run: | + cd .github/scripts + npm install + + - name: Get changed locale files + id: changed-files + if: github.event_name == 'pull_request' + run: | + # Get list of changed files in src/locales directory + CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- src/locales/*.json) + echo "Changed files: $CHANGED_FILES" + + # Create a JSON array of changed files with language codes + echo "CHANGED_FILES<> $GITHUB_ENV + echo "$CHANGED_FILES" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + + - name: Upload changed translations to POEditor + if: env.CHANGED_FILES != '' + env: + POEDITOR_API: ${{ secrets.POEDITOR_API }} + POEDITOR_PROJECT_ID: ${{ secrets.POEDITOR_PROJECT_ID }} + run: | + # Process each changed file + for FILE in $CHANGED_FILES; do + if [[ -f "$FILE" ]]; then + # Extract language code from filename (e.g., src/locales/en.json -> en) + FILENAME=$(basename "$FILE") + + # Special case: map gb.json to en language code + if [ "$FILENAME" == "gb.json" ]; then + LANG="en" + echo "Found gb.json, mapping to language code 'en'" + else + LANG=$(basename "$FILE" .json) + fi + + echo "Processing $FILE for language $LANG" + + # Upload to POEditor + LANGUAGE=$LANG FILE_PATH=$FILE node .github/scripts/upload-translations.js + fi + done + + - name: Notify on success + if: success() && env.CHANGED_FILES != '' + run: | + echo "Successfully uploaded translation files to POEditor." + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then + echo "Manual trigger comment: ${{ github.event.inputs.comment }}" + fi + + - name: Notify on no changes + if: env.CHANGED_FILES == '' + run: | + echo "No translation files were found to upload." diff --git a/src/locales/tr.json b/src/locales/tr.json index d93083871..82905727a 100644 --- a/src/locales/tr.json +++ b/src/locales/tr.json @@ -60,7 +60,6 @@ "distributedStatusSubHeaderText": "Dünya çapında milyonlarca cihaz tarafından desteklenen sistem performansını küresel bölgeye, ülkeye veya şehre göre görüntüleyin", "distributedRightCatagoryTitle": "Monitör", "distributedRightCatagoryDescription": "Mainnet Beta Kümesi", - "settingsGeneralSettings": "Genel ayarlar", "settingsDisplayTimezone": "Görüntüleme saat dilimi", "settingsDisplayTimezoneDescription": "Herkese açık olarak görüntülediğiniz kontrol panelinin saat dilimi.", @@ -102,6 +101,7 @@ "settingsFailedToAddDemoMonitors": "Demo monitörler eklenemedi", "settingsMonitorsDeleted": "Tüm monitörler başarıyla silindi", "settingsFailedToDeleteMonitors": "Monitörler silinemedi", - "starPromptTitle": "Checkmate yıldızla değerlendirin", - "starPromptDescription": "En son sürümleri görün ve GitHub'daki topluluğun büyümesine yardımcı olun" -} + "starPromptTitle": "Checkmate yıldızla değerlendirin", + "starPromptDescription": "En son sürümleri görün ve GitHub'daki topluluğun büyümesine yardımcı olun", + "testLocale": "TEST1211 UPLOAD" +} \ No newline at end of file