|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" |
| 5 | +cd "$SCRIPTS_DIR/.." |
| 6 | + |
| 7 | +# Directory containing .po files |
| 8 | +LOCALES_DIR="$PWD/src/frontend/src/lib/locales" |
| 9 | + |
| 10 | +INSTRUCTIONS="SYSTEM: You are a silent \`.po\` file translation bot. |
| 11 | +1. You will receive a \`.po\` file with missing translations. |
| 12 | +2. Analyze the existing translations in the file to determine the established tone (e.g., formal/informal 'you'), style, and terminology. |
| 13 | +3. Use these established decisions to translate *only* the entries where \`msgstr\` is empty. |
| 14 | +4. Ensure all variables (like {variable}) and tags (like <0>...</0>) are preserved in the translation. |
| 15 | +5. Apply the correct ICU plural categories required by the target language (e.g., one/other, one/few/many/other, or just other). |
| 16 | +6. Your output must be *only* the plain text \`.po\` entries (from msgid ... to msgstr \"...\") that you have just translated. |
| 17 | +7. DO NOT output any other text, greetings, explanations, or markdown. Your response must be *only* the plain text code snippet." |
| 18 | + |
| 19 | +# Make sure .po files are up to date |
| 20 | +echo "Updating .po files with latest changes" |
| 21 | +npm run extract > /dev/null # No need to output here |
| 22 | + |
| 23 | +# Loop through all .po files |
| 24 | +for po_file in "$LOCALES_DIR"/*.po; do |
| 25 | + if [[ -f "$po_file" ]]; then |
| 26 | + po_filename=$(basename "$po_file") |
| 27 | + |
| 28 | + # Skip source file |
| 29 | + if [[ "$po_filename" == "en.po" ]]; then |
| 30 | + echo -e "\nSkipping file: $po_filename" |
| 31 | + continue |
| 32 | + fi |
| 33 | + |
| 34 | + # Get entries without header |
| 35 | + po_content=$(<"$po_file") |
| 36 | + without_header=$(echo "$po_content" | awk ' |
| 37 | + BEGIN { skip=0; entry="" } |
| 38 | + /^msgid ""$/ { skip=1; entry="" ; next } # start skipping |
| 39 | + /^msgid / { if (!skip) { printf "%s", entry }; entry=$0 "\n"; skip=0; next } |
| 40 | + { entry = entry $0 "\n" } # accumulate lines |
| 41 | + END { if (!skip) printf "%s", entry } # print last entry if not skipped |
| 42 | + ') |
| 43 | + |
| 44 | + # Skip files without missing translations |
| 45 | + if ! grep -q '^msgstr ""$' <<< "$without_header"; then |
| 46 | + echo -e "\nSkipping file: $po_filename" |
| 47 | + continue; |
| 48 | + fi |
| 49 | + |
| 50 | + # Use npx to invoke the Gemini CLI to translate missing translations |
| 51 | + echo -e "\nTranslating file: $po_filename" |
| 52 | + translated_entries=$(npx https://github.com/google-gemini/gemini-cli -p "$INSTRUCTIONS\n\nThe $po_filename file content:\n$po_content") |
| 53 | + |
| 54 | + # Merge existing and new translations, while making sure that |
| 55 | + # existing translations take priority (remain unchanged). |
| 56 | + already_translated=$(echo "$without_header" | awk ' |
| 57 | + BEGIN {entry=""} |
| 58 | + { |
| 59 | + entry = entry $0 "\n" |
| 60 | + if ($0 ~ /^msgstr /) { |
| 61 | + if ($0 == "msgstr \"\"") { |
| 62 | + # Empty translation, skip entry |
| 63 | + entry = "" |
| 64 | + } else { |
| 65 | + # Non-empty msgstr, print entry |
| 66 | + printf "%s", entry |
| 67 | + entry = "" |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + ') |
| 72 | + merged_content="$po_content"$'\n'"$translated_entries"$'\n'"$already_translated" |
| 73 | + |
| 74 | + # Overwrite file with merged content |
| 75 | + echo "$merged_content" > "$po_file" |
| 76 | + fi |
| 77 | +done |
| 78 | + |
| 79 | +# Make sure .po files are cleaned up after adding the translations |
| 80 | +echo -e "\nCleaning up .po files with latest translations" |
| 81 | +npm run extract # We do not silence the output here so we can check if there are still missing translations |
0 commit comments