feat: upload translation to POEditor

This commit is contained in:
cihatata
2025-03-11 23:58:26 +03:00
parent 243b76b5dc
commit 2d37790186
4 changed files with 197 additions and 4 deletions

64
.github/scripts/upload-translations.js vendored Normal file
View File

@@ -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();

View File

@@ -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.

101
.github/workflows/upload-poeditor.yml vendored Normal file
View File

@@ -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<<EOF" >> $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."