Situation
You have a client who loves to give his files very descriptive names. That’s understandable as mostly the files you get to translate from him are lessons, lectures, howtos, manuals and so on. It makes sense to distribute them digitally with localized filenames.
Problem
What you need is a way to translate filenames in OmegaT thus keeping consistency with the contents of the translated files and past/future files from the same client.
Solution
GNU/Linux
On Linux you can use a bit of bash and whole bunch of OmegaT to do just what is needed. Now, with the Scripting Plugin it’s not all that complicated. The first part of this recipe is described here, when we talked about external scripts. So, here’s what you do:
- Make sure you have bash_opener.groovy among your OmegaT Scripting Plugin scripts (from previous recipe).
- Double-check that there’s
otopener
in your $PATH (from the previous recipe). - In your current OmegaT config directory (
$HOME/.omegat
by default) there should be a subdirectoryscript/openers
. Inside that directory among other scripts there should be the one that does the renaming according to names that you translated in OmegaT. - Here in this recipe this automagical renamer is included in the script
perform Misc. Actions
(made executable).
Below is its listing.#!/bin/bash #Setting different variables and functions listname=filenames.txt projroot="$1" source_dir="$3" target_dir="$4" source_list="$source_dir""$listname" target_list="$target_dir""$listname" renamed_dir="$projroot""`basename "$target_dir"`_${12}" OM_PATH=`cat $HOME/.omegat/ompath` ic="--window-icon="$OM_PATH/images/OmegaT.png"" createlist() { cd "$source_dir" ls -R|grep -v './'|sed '/\.\:/d'|sed '/^$/d' > $listname } ### #Creating dialog to ask what needs to be done action=`zenity --list --radiolist $ic --title "Misc. actions" \ --text "What do you want to do\?" \ --width="350" \ --column="Select" --column="Action" \ "TRUE" "Rename target files (with translation)" \ "" "Make Coffee" \ ` if [ $? -eq "1" ]; then zenity --error --title="Canceled" --text="Canceled" --timeout=1 exit 0 fi ### #Performing selected action case $action in "Rename target files (with translation)" ) if [ ! -e "$source_dir/$listname" ]; then createlist zenity --info --title="Further steps needed" \ --text="Reload your OmegaT project.\n\ Translate \"$listname\" in your OmegaT \ project\'s source directory.\n\ Then create translated documents in OmegaT and rerun this script." exit 11 fi zenity --question --title="Translated Documents Created?" \ --text="Did you translate the list of filenames and \ create translated documents (Ctrl+D)\?\n\ If so, press \"Yes\". If not, you can either press \"No\" and rerun \ the script later, or switch back to OmegaT, make sure that you have \ created translated documents, \ switch back to this window and press \"Yes\"." if [ $? -eq "1" ]; then zenity --error --title="Canceled" --text="Canceled" --timeout=1 exit 12 else if [ ! -e  "$target_dir/$listname" ]; then zenity --error --text="There is no \"$listname\" in \ your OmegaT project\'s target directory, can\'t rename files. \ Be sure to create translated documents in OmegaT, \ then rerun this script." exit 13 fi if [ -d "$renamed_dir" ]; then rm -R "$renamed_dir" sleep 2 fi cp -R "$target_dir" "$renamed_dir" target_dir="$renamed_dir" cd "$target_dir" while read -r -u3 line_src; read -r -u4 line_targ; do cd -- "$(find . -name "$line_src" -type f -printf '%h' -quit)" mv "$line_src" "$line_targ" cd "$target_dir" done 3< "$source_list" 4< "$target_list" fi xdg-open "$target_dir" ;; "Make Coffee" ) echo espresso >/dev/coffee sleep 180 notify-send "Coffee" "Don't forget to grab your cup" ;; esac
There are a couple steps that you need to make before you get nicely translated target files:
- The target files created by OmegaT are supposed to be named absolutely the same as source files, i.e., no additional suffixes/prefixes etc. It’s configured in files filters settings, and there the “Translated Filename Pattern” should read
${filename}
. - Upon the first run the scripts creates a file named
filenames.txt
(line 3 of the above listing is the place to change it to a more noble name). Then you must reload the project to make OmegaT aware of a new file in the source directory, open this file in the Editor and translate it. Once it’s done, save and create target documents (Ctrl+S, Ctrl+D). - Run the script again. It will double check if there’s a
filenames.txt
in the target directory, and if there is, you’ll get your target filenames renamed according to your careful translation. In fact, your actual target directory will remain intact, all the renaming will be done in a copied directory named the same as target, plus _LANG (two-letters target language code with preceding underscore). For your maximum satisfaction upon completion it will open a file browser window with this directory.
Let the fun begin!
Good luck!
One thought on “File Renamer (Bash from within OmegaT)”