Add translated strings support and norwegian translations

This commit is contained in:
Espen Kåsa Notodden 2014-05-24 02:26:08 +02:00
parent f355296c01
commit a9a023aceb
3 changed files with 51 additions and 12 deletions

View File

@ -66,8 +66,8 @@
<p><a class="button" href='/{{ slug }}/download'>{{ filename }} &#x25BC;</a></p>
<div class="metadata">
<p>File size: <strong>{{ filesize }} bytes</strong></p>
<p>SHA1 checksum: <strong>{{ filehash }}</strong></p>
<p>{{strings.filesize}}: <strong>{{ filesize }} bytes</strong></p>
<p>{{strings.sha1_checksum}}: <strong>{{ filehash }}</strong></p>
</div>
</body>
</html>

View File

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

23
strings.json Normal file
View File

@ -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"
}}