From a9a023aceb750d815d784dc9c0194a0cf7cd0cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20K=C3=A5sa=20Notodden?= Date: Sat, 24 May 2014 02:26:08 +0200 Subject: [PATCH] Add translated strings support and norwegian translations --- index.html | 4 ++-- onionshare.py | 36 ++++++++++++++++++++++++++---------- strings.json | 23 +++++++++++++++++++++++ 3 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 strings.json diff --git a/index.html b/index.html index 7f85ca45..0513278d 100644 --- a/index.html +++ b/index.html @@ -66,8 +66,8 @@

{{ filename }} ▼

-

File size: {{ filesize }} bytes

-

SHA1 checksum: {{ filehash }}

+

{{strings.filesize}}: {{ filesize }} bytes

+

{{strings.sha1_checksum}}: {{ filehash }}

diff --git a/onionshare.py b/onionshare.py index d9095c99..6d413fc1 100755 --- a/onionshare.py +++ b/onionshare.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -import os, sys, subprocess, time, hashlib, platform +import os, sys, subprocess, time, hashlib, platform, json, locale from random import randint from functools import wraps @@ -12,6 +12,8 @@ from stem import SocketError from flask import Flask, Markup, Response, request, make_response, send_from_directory, render_template_string app = Flask(__name__) +strings = {} + # generate an unguessable string slug = os.urandom(16).encode('hex') @@ -22,7 +24,7 @@ filename = filehash = filesize = '' def index(): global filename, filesize, filehash, slug return render_template_string(open('{0}/index.html'.format(os.path.dirname(__file__))).read(), - slug=slug, filename=os.path.basename(filename), filehash=filehash, filesize=filesize) + slug=slug, filename=os.path.basename(filename), filehash=filehash, filesize=filesize, strings=strings) @app.route("/{0}/download".format(slug)) def download(): @@ -57,24 +59,38 @@ def get_hidden_service_hostname(port): def tails_open_port(port): if get_platform() == 'Tails': - print 'Punching a hole in the firewall' + print strings["punching_a_hole"] subprocess.call(['/sbin/iptables', '-I', 'OUTPUT', '-o', 'lo', '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT']) def tails_close_port(port): if get_platform() == 'Tails': - print 'Closing hole in firewall' + print strings["closing_hole"] subprocess.call(['/sbin/iptables', '-I', 'OUTPUT', '-o', 'lo', '-p', 'tcp', '--dport', str(port), '-j', 'REJECT']) + +def load_strings(default="en"): + global strings + translated = json.loads(open('{0}/strings.json'.format( + os.path.dirname(__file__))).read()) + strings = translated[default] + return + lc, enc = locale.getdefaultlocale() + if lc: + lang = lc[:2] + if lang in translated: + strings = translated[lang] + if __name__ == '__main__': + load_strings() # validate filename if len(sys.argv) != 2: sys.exit('Usage: {0} [filename]'.format(sys.argv[0])); filename = sys.argv[1] if not os.path.isfile(filename): - sys.exit('{0} is not a file'.format(filename)) + sys.exit(strings["not_a_file"].format(filename)) # calculate filehash, file size - print 'Calculate sha1 checksum' + print strings["calculating_sha1"] BLOCKSIZE = 65536 hasher = hashlib.sha1() with open(filename, 'rb') as f: @@ -89,7 +105,7 @@ if __name__ == '__main__': port = randint(1025, 65535) # connect to the tor controlport - print 'Connecting to Tor control port to set up hidden service on port {0}'.format(port) + print strings["connecting_ctrlport"].format(port) controlports = [9051, 9151] controller = False for controlport in controlports: @@ -98,7 +114,7 @@ if __name__ == '__main__': except SocketError: pass if not controller: - sys.exit('Cannot connect to Tor control port on ports {0}. Is Tor running?'.format(controlports)) + sys.exit(strings["cant_connect_ctrlport"].format(controlports)) controller.authenticate() # set up hidden service @@ -112,10 +128,10 @@ if __name__ == '__main__': tails_open_port(port) # instructions - print '\nGive this URL to the person you\'re sending the file to:' + print '\n' + strings["give_this_url"] print 'http://{0}/{1}'.format(onion_host, slug) print '' - print 'Press Ctrl-C to stop server\n' + print strings["ctrlc_to_stop"] # start the web server app.run(port=port) diff --git a/strings.json b/strings.json new file mode 100644 index 00000000..80512d1f --- /dev/null +++ b/strings.json @@ -0,0 +1,23 @@ +{ "en": { + "punching_a_hole": "Punching a hole in the firewall.", + "closing_hole": "Closing hole in firewall.", + "calculating_sha1": "Calculating SHA1 checksum.", + "connecting_ctrlport": "Connecting to Tor control port to set up hidden service on port {0}.", + "cant_connect_ctrlport": "Cannot connect to Tor control port on ports {0}. Is Tor running?", + "give_this_url": "Give this URL to the person you're sending the file to:", + "ctrlc_to_stop": "Press Ctrl-C to stop server", + "not_a_file": "{0} is not a file.", + "filesize": "File size", + "sha1_checksum": "SHA1 checksum" +}, "no": { + "punching_a_hole": "Åpner port i brannmuren.", + "closing_hole": "Lukker port i brannmuren.", + "calculating_sha1": "Kalkulerer SHA1 sjekksum.", + "connecting_ctrlport": "Kobler til Tors kontroll-port for å sette opp en gjemt tjeneste på port {0}.", + "cant_connect_ctrlport": "Klarte ikke å koble til Tors kontroll-porter {0}. Sjekk at Tor kjører.", + "give_this_url": "Gi personen du vil sende filen til denne URL-en:", + "ctrlc_to_stop": "Trykk Ctrl+C for å stoppe serveren.", + "not_a_file": "{0} er ikke en fil.", + "filesize": "Filstørrelse", + "sha1_checksum": "SHA1 sjekksum" +}}