Allow for localization for "Share via OnionShare" string in the nautilus extension

This commit is contained in:
Micah Lee 2017-12-01 12:57:18 -08:00
parent 3ed0c38205
commit e54836a69a
3 changed files with 57 additions and 7 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/python2
import os
import sys
import json
import locale
import subprocess
import urllib
import gi
@ -12,7 +13,55 @@ from gi.repository import GObject
# Put me in /usr/share/nautilus-python/extensions/
class OnionShareExtension(GObject.GObject, Nautilus.MenuProvider):
def __init__(self):
pass
# Get the localized string for "Share via OnionShare" label
self.label = None
default_label = 'Share via OnionShare'
try:
# Re-implement localization in python2
default_locale = 'en'
locale_dir = os.path.join(sys.prefix, 'share/onionshare/locale')
if os.path.exists(locale_dir):
# Load all translations
strings = {}
translations = {}
for filename in os.listdir(locale_dir):
abs_filename = os.path.join(locale_dir, filename)
lang, ext = os.path.splitext(filename)
if ext == '.json':
with open(abs_filename) as f:
translations[lang] = json.load(f)
strings = translations[default_locale]
lc, enc = locale.getdefaultlocale()
if lc:
lang = lc[:2]
if lang in translations:
# if a string doesn't exist, fallback to English
for key in translations[default_locale]:
if key in translations[lang]:
strings[key] = translations[lang][key]
self.label = strings['share_via_onionshare']
except:
self.label = default_label
if not self.label:
self.label = default_label
"""
# This more elegant solution will only work if nautilus is using python3, and onionshare is installed system-wide.
# But nautilus is using python2, so this is commented out.
try:
import onionshare
onionshare.strings.load_strings(onionshare.common)
self.label = onionshare.strings._('share_via_onionshare')
except:
import sys
print('python version: {}').format(sys.version)
self.label = 'Share via OnionShare'
"""
def url2path(self,url):
file_uri = url.get_activation_uri()
@ -31,7 +80,7 @@ class OnionShareExtension(GObject.GObject, Nautilus.MenuProvider):
def get_file_items(self, window, files):
menuitem = Nautilus.MenuItem(name='OnionShare::Nautilus',
label='Share via OnionShare',
label=self.label,
tip='',
icon='')
menu = Nautilus.Menu()

View File

@ -72,8 +72,8 @@ def get_resource_path(filename):
# While running tests during stdeb bdist_deb, look 3 directories up for the share folder
prefix = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(prefix)))), 'share')
elif p == 'Linux' and sys.argv and sys.argv[0].startswith(sys.prefix):
# OnionShare is installed systemwide in Linux
elif p == 'Linux':
# Assume OnionShare is installed systemwide in Linux, since we're not running in dev mode
prefix = os.path.join(sys.prefix, 'share/onionshare')
elif getattr(sys, 'frozen', False):

View File

@ -111,5 +111,6 @@
"gui_tor_connection_ask": "Would you like to open OnionShare settings to troubleshoot connecting to Tor?",
"gui_tor_connection_ask_open_settings": "Open Settings",
"gui_tor_connection_ask_quit": "Quit",
"gui_tor_connection_error_settings": "Try adjusting how OnionShare connects to the Tor network in Settings."
"gui_tor_connection_error_settings": "Try adjusting how OnionShare connects to the Tor network in Settings.",
"share_via_onionshare": "Share via OnionShare"
}