Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.)

share|improve this question

1 Answer 1

up vote 9 down vote accepted
+100

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.

share|improve this answer
    
+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 –  cipricus 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. –  user76204 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 –  cipricus 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 1 '12 at 11:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.