Skip to content

Instantly share code, notes, and snippets.

@markshust
Created March 20, 2023 23:51
Show Gist options
  • Save markshust/fb78c1166791fe530d0d60bc00461ef3 to your computer and use it in GitHub Desktop.
Save markshust/fb78c1166791fe530d0d60bc00461ef3 to your computer and use it in GitHub Desktop.
Bash script to translate SRT files in the current directory into another language using Google Translate
#!/bin/bash
languageName='Spanish'
language='es'
for file in *.srt; do
if [[ $file == *.$language.srt ]]; then
# Skip processing of already translated files
continue
fi
if [ ! -f "${file%.*}.$language.srt" ]; then
# Read input file and translate if necessary
echo "Translating $file to $languageName..."
while read -r line; do
if [[ "$line" =~ [a-z] ]] ; then
translated=$(echo "$line" | trans -brief ":$language")
if [[ $? -ne 0 ]]; then
echo "Error: Translation failed for '$line'. Skipping."
else
echo "$translated" >> "${file%.*}.$language.srt"
fi
else
echo "$line" >> "${file%.*}.$language.srt"
fi
done < "$file"
fi
done
echo "Translation complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment