|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to pull translations from Transifex and clean up empty files/directories |
| 4 | +# |
| 5 | +# Note: Uses --mode onlytranslated to only download translated strings, which may result |
| 6 | +# in empty files for incomplete translations that will be cleaned up by this script. |
| 7 | + |
| 8 | +set -e |
| 9 | + |
| 10 | +# Validate script is run from project root |
| 11 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 12 | +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 13 | +LOCALIZATION_DIR="$PROJECT_ROOT/Bitkit/Resources/Localization" |
| 14 | + |
| 15 | +if [ ! -d "$LOCALIZATION_DIR" ]; then |
| 16 | + echo "Error: Localization directory not found: $LOCALIZATION_DIR" |
| 17 | + echo "Please run this script from the project root directory." |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +if [ ! -f "$PROJECT_ROOT/.tx/config" ]; then |
| 22 | + echo "Error: Transifex config not found: $PROJECT_ROOT/.tx/config" |
| 23 | + echo "Please ensure Transifex is configured for this project." |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +# Helper function to rename or merge directories |
| 28 | +rename_or_merge() { |
| 29 | + local src="$1" |
| 30 | + local dst="$2" |
| 31 | + local src_name=$(basename "$src") |
| 32 | + local dst_name=$(basename "$dst") |
| 33 | + local merge_errors=0 |
| 34 | + |
| 35 | + if [ ! -d "$src" ]; then |
| 36 | + echo " Warning: Source directory does not exist: $src_name" |
| 37 | + return 1 |
| 38 | + fi |
| 39 | + |
| 40 | + if [ ! -d "$dst" ]; then |
| 41 | + echo " Renaming: $src_name -> $dst_name" |
| 42 | + if mv "$src" "$dst"; then |
| 43 | + return 0 |
| 44 | + else |
| 45 | + echo " Error: Failed to rename $src_name to $dst_name" |
| 46 | + return 1 |
| 47 | + fi |
| 48 | + else |
| 49 | + echo " Merging: $src_name -> $dst_name" |
| 50 | + while IFS= read -r -d '' item; do |
| 51 | + if ! mv "$item" "$dst/" 2>/dev/null; then |
| 52 | + echo " Warning: Failed to move $(basename "$item") from $src_name" |
| 53 | + merge_errors=$((merge_errors + 1)) |
| 54 | + fi |
| 55 | + done < <(find "$src" -mindepth 1 -maxdepth 1 -print0 2>/dev/null) |
| 56 | + |
| 57 | + if [ "$merge_errors" -eq 0 ]; then |
| 58 | + if rmdir "$src" 2>/dev/null; then |
| 59 | + return 0 |
| 60 | + else |
| 61 | + echo " Warning: Could not remove empty directory: $src_name" |
| 62 | + return 1 |
| 63 | + fi |
| 64 | + else |
| 65 | + echo " Error: Some files could not be merged from $src_name" |
| 66 | + return 1 |
| 67 | + fi |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +# Validate .strings file has basic structure |
| 72 | +validate_strings_file() { |
| 73 | + local file="$1" |
| 74 | + # Basic validation: check file exists and is readable |
| 75 | + if [ ! -r "$file" ]; then |
| 76 | + echo " Warning: $file is not readable, skipping normalization" |
| 77 | + return 1 |
| 78 | + fi |
| 79 | + # Check if file has at least one translation entry (basic format check) |
| 80 | + if ! grep -q '^"[^"]*"[[:space:]]*=' "$file" 2>/dev/null && [ -s "$file" ]; then |
| 81 | + echo " Warning: $file does not appear to be a valid .strings file, skipping normalization" |
| 82 | + return 1 |
| 83 | + fi |
| 84 | + return 0 |
| 85 | +} |
| 86 | + |
| 87 | +echo "Pulling translations from Transifex..." |
| 88 | + |
| 89 | +# Check if tx command is available |
| 90 | +if ! command -v tx &> /dev/null; then |
| 91 | + echo "Error: Transifex CLI (tx) is not installed or not in PATH" |
| 92 | + echo "Please install it: https://developers.transifex.com/docs/cli" |
| 93 | + exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +# Run tx pull and check for errors |
| 97 | +set +e |
| 98 | +tx pull -a --mode onlytranslated |
| 99 | +TX_EXIT_CODE=$? |
| 100 | +set -e |
| 101 | + |
| 102 | +if [ "$TX_EXIT_CODE" -ne 0 ]; then |
| 103 | + echo "Error: Transifex pull failed with exit code $TX_EXIT_CODE" |
| 104 | + echo "Please check your Transifex configuration and authentication." |
| 105 | + exit 1 |
| 106 | +fi |
| 107 | + |
| 108 | +echo "" |
| 109 | +echo "Renaming and cleaning up directories..." |
| 110 | + |
| 111 | +RENAMED_COUNT=0 |
| 112 | + |
| 113 | +# Process all .lproj directories |
| 114 | +while IFS= read -r dir; do |
| 115 | + dir_name=$(basename "$dir") |
| 116 | + dir_path=$(dirname "$dir") |
| 117 | + |
| 118 | + case "$dir_name" in |
| 119 | + arb.lproj) |
| 120 | + rename_or_merge "$dir" "$dir_path/ar.lproj" && RENAMED_COUNT=$((RENAMED_COUNT + 1)) |
| 121 | + ;; |
| 122 | + es_419.lproj) |
| 123 | + rename_or_merge "$dir" "$dir_path/es-419.lproj" && RENAMED_COUNT=$((RENAMED_COUNT + 1)) |
| 124 | + ;; |
| 125 | + es_ES.lproj) |
| 126 | + rename_or_merge "$dir" "$dir_path/es.lproj" && RENAMED_COUNT=$((RENAMED_COUNT + 1)) |
| 127 | + ;; |
| 128 | + pt_PT.lproj|pt-PT.lproj) |
| 129 | + rename_or_merge "$dir" "$dir_path/pt.lproj" && RENAMED_COUNT=$((RENAMED_COUNT + 1)) |
| 130 | + ;; |
| 131 | + pt_BR.lproj) |
| 132 | + rename_or_merge "$dir" "$dir_path/pt-BR.lproj" && RENAMED_COUNT=$((RENAMED_COUNT + 1)) |
| 133 | + ;; |
| 134 | + *_*.lproj) |
| 135 | + new_name=$(echo "$dir_name" | sed 's/_/-/g') |
| 136 | + [ "$new_name" != "$dir_name" ] && rename_or_merge "$dir" "$dir_path/$new_name" && RENAMED_COUNT=$((RENAMED_COUNT + 1)) |
| 137 | + ;; |
| 138 | + esac |
| 139 | +done < <(find "$LOCALIZATION_DIR" -type d -name "*.lproj" 2>/dev/null | sort) |
| 140 | + |
| 141 | +echo " Renamed $RENAMED_COUNT directories" |
| 142 | + |
| 143 | +echo "" |
| 144 | +echo "Normalizing .strings files..." |
| 145 | + |
| 146 | +# Normalize .strings files: remove trailing whitespace and empty string entries |
| 147 | +NORMALIZED_COUNT=0 |
| 148 | +while IFS= read -r file; do |
| 149 | + if ! validate_strings_file "$file"; then |
| 150 | + continue |
| 151 | + fi |
| 152 | + |
| 153 | + if awk '{ |
| 154 | + gsub(/[[:space:]]+$/, "") |
| 155 | + if ($0 ~ /^"[^"]*"[[:space:]]*=[[:space:]]*"";[[:space:]]*$/) next |
| 156 | + print |
| 157 | + }' "$file" > "$file.tmp"; then |
| 158 | + if mv "$file.tmp" "$file" 2>/dev/null; then |
| 159 | + NORMALIZED_COUNT=$((NORMALIZED_COUNT + 1)) |
| 160 | + else |
| 161 | + echo " Warning: Failed to replace $file" |
| 162 | + rm -f "$file.tmp" |
| 163 | + fi |
| 164 | + else |
| 165 | + echo " Warning: Failed to normalize $file" |
| 166 | + rm -f "$file.tmp" |
| 167 | + fi |
| 168 | +done < <(find "$LOCALIZATION_DIR" -type f -name "*.strings" 2>/dev/null) |
| 169 | + |
| 170 | +echo " Normalized $NORMALIZED_COUNT files" |
| 171 | + |
| 172 | +echo "" |
| 173 | +echo "Cleaning up empty files and directories..." |
| 174 | + |
| 175 | +EMPTY_COUNT=0 |
| 176 | +DELETED_DIRS=0 |
| 177 | +declare -a dirs_to_check |
| 178 | + |
| 179 | +# Delete empty .strings files and collect their directories |
| 180 | +set +e |
| 181 | +while IFS= read -r file; do |
| 182 | + line_count=$(wc -l < "$file" 2>/dev/null | tr -d ' ' || echo "0") |
| 183 | + translation_count=$(grep -c '^"[^"]*" = ' "$file" 2>/dev/null || echo "0") |
| 184 | + |
| 185 | + if [ "$line_count" -le 2 ] || [ "$translation_count" -eq 0 ]; then |
| 186 | + echo " Deleting empty file: $file" |
| 187 | + dirs_to_check+=("$(dirname "$file")") |
| 188 | + rm -f "$file" |
| 189 | + EMPTY_COUNT=$((EMPTY_COUNT + 1)) |
| 190 | + fi |
| 191 | +done < <(find "$LOCALIZATION_DIR" -type f -name "*.strings" 2>/dev/null) |
| 192 | +set -e |
| 193 | + |
| 194 | +# Remove empty .lproj directories (process depth-first by reverse sorting paths) |
| 195 | +for dir in $(printf '%s\n' "${dirs_to_check[@]}" | sort -u | sort -r); do |
| 196 | + [ -d "$dir" ] || continue |
| 197 | + set +e |
| 198 | + file_count=$(find "$dir" -type f ! -name '.DS_Store' 2>/dev/null | wc -l | tr -d ' ') |
| 199 | + set -e |
| 200 | + if [ "$file_count" -eq 0 ]; then |
| 201 | + echo " Deleting empty directory: $dir" |
| 202 | + if rmdir "$dir" 2>/dev/null; then |
| 203 | + DELETED_DIRS=$((DELETED_DIRS + 1)) |
| 204 | + fi |
| 205 | + fi |
| 206 | +done |
| 207 | + |
| 208 | +echo "" |
| 209 | +echo "Complete!" |
| 210 | +echo " Renamed: $RENAMED_COUNT" |
| 211 | +echo " Normalized: $NORMALIZED_COUNT" |
| 212 | +echo " Deleted files: $EMPTY_COUNT, Deleted dirs: $DELETED_DIRS" |
0 commit comments