5

I have hundreds of files with different names that I want to translate into a different language.

Is there an application/CLI action that would allow me to copy all this names as in a list/table and then, after having translated them, to paste them back into the list/table, or that would allow a procedure somewhat similar to "Rename" in Thunar but with a more complex action closer to what I have described?

(I am in Lubuntu and I prefer not to use Nautilus due to unwanted interference with LXDE/pcmanfm desktop and LXPanel. If there is a solution in Nautilus please provide it but try to give an alternative if possible.)

1 Answers1

10

The script below, which was inspired by a different discussion here, uses translate-bin, the program featured in this related question, and translates and renames your files in one go. It is suitable for batch translating and renaming hundreds of files all in one go. First install the necessary programs from the repositories with:

sudo apt-get install libtranslate0 libtranslate-bin

The script is currently set to rename any files with the extension .txt, but it will work with anything you might want: just change all the instances of .txt to .jpg, .mp4 or to whatever files you wish when you want to translate them.

The script is currently set to translate file names from German (de) to French (fr), but you can choose any language that is supported by the service (-s) being used; and as that translation service is currently Google there are many language on offer.

Open your text editor, save the file and then make it executable with chmod u+x myscript.

#!/bin/bash

for i in *.txt
do 
    mv -i "$i" "$(echo ${i%.txt} | translate-bin -s google -f de -t fr).txt"; 
done

You can place the script in the folder with the target files and then execute it, or place it in your ~/bin folder and then use terminal to cd to the folder with the files in and call the script with the name you have given it. For more on the bin folder, see my answer here.

Here is an example where I open terminal and cd to the target folder:

Before the script runs, ls returns dog and cat in German:

Hund.txt  Katze.txt

and after the script has been executed in terminal with ./scriptname, the output of ls is dog and cat in French:

Chien.txt Chat.txt

It is probably best to backup your files before you use the script, but if an invalid translation target such as sgghdsgsh.txt is found, the script does nothing whatever languages are set. However, to prevent it overwriting files we can add -i to mv: if clicked on and run and the target file exists, nothing will happen, but if the script is run in terminal with ./script, you will then be asked to confirm:

mv: `Cat.txt' and `Cat.txt' are the same file                                  
mv: overwrite `Cat.txt'? n  

I don't think it's necessary to put any other checks in the script, so that should be satisfactory.

For more options for the program are available from man translate or the Ubuntu manpages online.

  • +1 for libtranslate-bin (did not know) – philshem Nov 30 '12 at 21:17
  • found why: it's one of the many bad deeds of pcmanfm. in thunar it works. so lets delete all these comments! the bounty its yours –  Nov 30 '12 at 22:17
  • 1
    @cipricus You can leave the bounty open for a while if you wish, to give anyone else a chance. –  Nov 30 '12 at 22:18
  • in case executing the script file does not work in pcmanfm, just use ./scriptname in terminal or try another file manager –  Nov 30 '12 at 22:25
  • So what happens if no translation is found? Might want to add safe check in case the transation returns empty string which could result in files being overwritten – Flint Dec 01 '12 at 11:10
  • @Flint Good point-see my edit, but I think that basic addition is enough; if it was a system script I might add tests to see if [ ] exists, but that's probably not necessary here. –  Dec 01 '12 at 11:44