check_localization 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. set +o pipefail
  3. github_step_summary() {
  4. [ "" == "$GITHUB_STEP_SUMMARY" ] || echo "$1" >> "$GITHUB_STEP_SUMMARY"
  5. }
  6. output() {
  7. echo "$1"
  8. github_step_summary "$1"
  9. }
  10. if [ $# != 1 ]; then
  11. cat <<-EOF
  12. Usage: check_localization <localizable.strings>
  13. Check the localizable.strings file has all keys in base
  14. EOF
  15. exit 1
  16. fi
  17. # Get localization keys
  18. basefile=CutBox/Localization/en.lproj/Localizable.strings
  19. keys=$(mktemp)
  20. rg -o '^"[^"]*"' "$basefile" > "$keys"
  21. file="$1"
  22. errors=()
  23. # Check file has all basekeys
  24. while IFS= read -r line; do
  25. if ! rg -q "$line" "$file"; then
  26. # collect errors
  27. errors+=("/* Missing in $file: */")
  28. errors+=("$line = \"TODO\";")
  29. errors+=("/* English: $(rg -IN "$line" "$basefile") */")
  30. fi
  31. done < "$keys"
  32. rg -o '^"[^"]*"' "$file" > "$keys"
  33. # Check file has no additional keys
  34. while IFS= read -r line; do
  35. if ! rg -q "$line" "$basefile"; then
  36. # collect errors
  37. errors+=("remove: $line")
  38. fi
  39. done < "$keys"
  40. if [ ${#errors[@]} -gt 0 ]; then
  41. output "- [✗] Error in $file"
  42. github_step_summary "\`\`\`"
  43. for error in "${errors[@]}"; do
  44. output "$error"
  45. done
  46. github_step_summary "\`\`\`"
  47. output ""
  48. exit 1
  49. else
  50. output "- [✓] $file"
  51. fi
  52. rm "$keys"