通过Xcode构建阶段自动获取POEditor翻译的Bash脚本分享
I’ve been using POEditor to manage translations for my iOS apps for a while now, and one thing that bugged me was having to manually download the latest strings every time I wanted to test updated translations. So I built a Bash script that hooks into POEditor’s REST API to automatically pull fresh translations on every Xcode build. Here’s how it works:
The Script
This script handles authenticating with POEditor, fetching your project's active languages, downloading translations in Apple's .strings format, and placing them directly into your Xcode project's localization directories.
#!/bin/bash # Configuration - Update these with your POEditor details POEDITOR_API_TOKEN="your_api_token_here" POEDITOR_PROJECT_ID="your_project_id_here" # Path to your app's localization folder (adjust this to match your project structure) LOCALIZATION_DIR="./YourApp/Resources/Localizations" # Create the localization directory if it doesn't exist mkdir -p "$LOCALIZATION_DIR" # Check if jq is installed (required to parse JSON responses from POEditor) if ! command -v jq &> /dev/null; then echo "Error: jq is missing! Install it first with 'brew install jq' (macOS) or your system's package manager." exit 1 fi # Fetch the list of active languages in your POEditor project LANGUAGES=$(curl -s -X POST "https://api.poeditor.com/v2/languages/list" \ -d "api_token=$POEDITOR_API_TOKEN" \ -d "id=$POEDITOR_PROJECT_ID" | jq -r '.result.languages[].code') # Download and organize translations for each language for LANG in $LANGUAGES; do echo "Fetching translations for language: $LANG" # Download the .strings file from POEditor curl -s -X POST "https://api.poeditor.com/v2/projects/export" \ -d "api_token=$POEDITOR_API_TOKEN" \ -d "id=$POEDITOR_PROJECT_ID" \ -d "language=$LANG" \ -d "type=apple_strings" > "$LOCALIZATION_DIR/temp_$LANG.strings" # Create the language-specific .lproj directory (Xcode's expected structure) LANG_PROJ_DIR="$LOCALIZATION_DIR/$LANG.lproj" mkdir -p "$LANG_PROJ_DIR" # Move the downloaded file to the correct location and rename to Localizable.strings mv "$LOCALIZATION_DIR/temp_$LANG.strings" "$LANG_PROJ_DIR/Localizable.strings" done echo "✅ All translations successfully pulled from POEditor and ready for build!"
Setup Steps
- Add your POEditor credentials: Replace
your_api_token_hereandyour_project_id_herewith the values from your POEditor account (you can find these in your account's API settings). - Set the correct path: Update
LOCALIZATION_DIRto point to where your Xcode project stores its localization files (usually something like${SRCROOT}/YourAppName/Localizations). - Install jq: This script uses
jqto parse JSON responses from POEditor. On macOS, install it withbrew install jq; on Linux, use your distribution's package manager (e.g.,sudo apt install jq). - Add to Xcode as a Run Script Phase:
- Open your Xcode project, select your app target, and go to the Build Phases tab.
- Click the
+button at the top and select New Run Script Phase. - Paste the path to your script (e.g.,
${SRCROOT}/Scripts/fetch_poeditor_translations.sh) into the script field. - Drag this new run script phase before the "Copy Bundle Resources" phase so the fresh translations are included in your build.
Now every time you build your app in Xcode, it will automatically pull the latest translations from POEditor—no more manual downloads!
内容的提问来源于stack exchange,提问作者HixField

