86 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/env python3
 | 
						|
 | 
						|
# googletrans comes with a translate(1) command line application but I don't
 | 
						|
# like its UI.
 | 
						|
 | 
						|
import sys;
 | 
						|
 | 
						|
stdin = sys.stdin;
 | 
						|
stdout = sys.stdout;
 | 
						|
stderr = sys.stderr;
 | 
						|
 | 
						|
_exit = exit;
 | 
						|
exit = sys.exit;
 | 
						|
 | 
						|
def printf(s, *args):
 | 
						|
	return fprintf(stdout, s, *args);
 | 
						|
 | 
						|
def fprintf(f, s, *args):
 | 
						|
	printing = s if len(args) == 0 else (s % args);
 | 
						|
	return f.write(printing);
 | 
						|
 | 
						|
try:
 | 
						|
	import googletrans;
 | 
						|
except:
 | 
						|
	printf(
 | 
						|
		"%s: This Python script requires the \"googletrans\" library.\n",
 | 
						|
		sys.argv[0]
 | 
						|
	);
 | 
						|
 | 
						|
def usage(name):
 | 
						|
	fprintf(
 | 
						|
		stderr, "Usage: %s [source language] [destination language]\n",
 | 
						|
		name
 | 
						|
	);
 | 
						|
	exit(1);
 | 
						|
 | 
						|
def main(argc, argv):
 | 
						|
	accepted_languages = set(list(googletrans.LANGCODES)
 | 
						|
		+ list(googletrans.LANGUAGES) + [ "auto" ]);
 | 
						|
	good = True;
 | 
						|
	isatty = stdout.isatty();
 | 
						|
	translator = googletrans.Translator();
 | 
						|
 | 
						|
	if argc != 3:
 | 
						|
		usage(argv[0]);
 | 
						|
 | 
						|
	src = argv[1].lower();
 | 
						|
	dest = argv[2].lower();
 | 
						|
	for arg in [ src, dest ]:
 | 
						|
		if not(arg in accepted_languages):
 | 
						|
			good = False;
 | 
						|
			fprintf(stderr,
 | 
						|
				"%s: %s: Language not recognized.\n",
 | 
						|
				argv[0], arg);
 | 
						|
	if(not(good)):
 | 
						|
		fprintf(stderr, "The following languages and language codes are"
 | 
						|
			+ " recognized:\n");
 | 
						|
		print(googletrans.LANGCODES);
 | 
						|
		exit(1);
 | 
						|
 | 
						|
	try:
 | 
						|
		text = stdin.read().split('\n')[:-1]
 | 
						|
	except KeyboardInterrupt:
 | 
						|
		fprintf(stderr, "%s: Cancelled (keyboard interrupt).\n", argv[0]);
 | 
						|
		exit(1);
 | 
						|
	for line in text:
 | 
						|
		try:
 | 
						|
			translation = translator.translate(line, src=src, dest=dest);
 | 
						|
		except:
 | 
						|
			fprintf(stderr, "%s: %s ->%s\n\t%s\n",
 | 
						|
				argv[0], line, dest, "Error trying to translate.");
 | 
						|
		if isatty:
 | 
						|
			fprintf(stdout,
 | 
						|
				"%s -> %s\n"
 | 
						|
				+ "\t%s -> %s\n"
 | 
						|
				+ "\tGiven pronunciation: %s\n",
 | 
						|
				translation.origin, translation.text,
 | 
						|
				translation.src, translation.dest,
 | 
						|
				translation.pronunciation
 | 
						|
			);
 | 
						|
		else:
 | 
						|
			fprintf(stdout, "%s\n", translation.text);
 | 
						|
	return 0;
 | 
						|
 | 
						|
exit(main(len(sys.argv), sys.argv));
 |