From ff9d0ace316563973c0a9a95718cab7f683cb7bb Mon Sep 17 00:00:00 2001 From: Fioddor Superconcentrado Date: Fri, 9 Oct 2020 00:10:27 +0200 Subject: [PATCH] networks: i18n: Mark various strings for translation Helps: #1938. - Translate various states passed in by Network Manager: - Device state - Device state reason - Device type - Firewall zone - IPv4/IPv6 connection method - Wireless mode - forms.py: Move zones list to network.py, reuse in views.py. Testing: - Yapf applied. - Flake8 without errors or warnings for changed files. - (Unit) tests run without errors. - Screnshots attached to #1938. Signed-off-by: Fioddor Superconcentrado [sunil: Add strings for many more states] [sunil: Don't allow None to be selected as firewall zone] [sunil: Drop forced_literals.py as it reduces modularity of the code] Signed-off-by: Sunil Mohan Adapa Reviewed-by: Sunil Mohan Adapa --- plinth/modules/networks/forms.py | 3 +- .../networks/templates/connection_show.html | 18 +-- plinth/modules/networks/views.py | 110 ++++++++++++++++++ plinth/network.py | 3 + 4 files changed, 123 insertions(+), 11 deletions(-) diff --git a/plinth/modules/networks/forms.py b/plinth/modules/networks/forms.py index 07a11d034..69eba6af9 100644 --- a/plinth/modules/networks/forms.py +++ b/plinth/modules/networks/forms.py @@ -35,8 +35,7 @@ class ConnectionForm(forms.Form): label=_('Firewall Zone'), help_text=_('The firewall zone will control which services are ' 'available over this interfaces. Select Internal only ' - 'for trusted networks.'), - choices=[('external', _('External')), ('internal', _('Internal'))]) + 'for trusted networks.'), choices=network.ZONES) ipv4_method = forms.ChoiceField( label=_('IPv4 Addressing Method'), help_text=format_lazy( _('"Automatic" method will make {box_name} acquire ' diff --git a/plinth/modules/networks/templates/connection_show.html b/plinth/modules/networks/templates/connection_show.html index e0a35a81f..11bc00b23 100644 --- a/plinth/modules/networks/templates/connection_show.html +++ b/plinth/modules/networks/templates/connection_show.html @@ -71,17 +71,17 @@
{% trans "State" %} - {{ device.state }} + {{ device.state_string }}
{% if device.state_reason != 'none' %}
{% trans "State reason" %} - {{ device.state_reason }} + {{ device.state_reason_string }}
{% endif %}
{% trans "Type" %} - {{ device.type }} + {{ device.type_string }}
{% trans "MAC address" %} @@ -140,7 +140,7 @@
{% trans "Mode" %} - {{ device.wireless.mode }} + {{ device.wireless.mode_string }}
{% endif %} {% if access_point.channel %} @@ -169,7 +169,7 @@ {% if connection.ipv4.method %}
{% trans "Method" %} - {{ connection.ipv4.method }} + {{ connection.ipv4.method_string }}
{% endif %} @@ -210,7 +210,7 @@ {% if connection.ipv6.method %}
{% trans "Method" %} - {{ connection.ipv6.method }} + {{ connection.ipv6.method_string }}
{% endif %} @@ -255,7 +255,7 @@
{% trans "Firewall zone" %}
- {{ connection.zone }} + {{ connection.zone_string }}
@@ -275,7 +275,7 @@
{% trans "Firewall zone" %}
- {{ connection.zone }} + {{ connection.zone_string }}
@@ -294,7 +294,7 @@
{% trans "Firewall zone" %}
- external + {% trans "External" %}
diff --git a/plinth/modules/networks/views.py b/plinth/modules/networks/views.py index 939e2d638..8f0b1a67d 100644 --- a/plinth/modules/networks/views.py +++ b/plinth/modules/networks/views.py @@ -8,6 +8,7 @@ from django.shortcuts import redirect from django.template.response import TemplateResponse from django.urls import reverse_lazy from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext_lazy from django.views.decorators.http import require_POST from django.views.generic.edit import FormView @@ -20,6 +21,98 @@ from .forms import (ConnectionTypeSelectForm, EthernetForm, GenericForm, logger = logging.getLogger(__name__) +# i18n for device.state +# https://developer.gnome.org/libnm/1.29/libnm-nm-dbus-interface.html#NMDeviceState +CONNECTION_METHOD_STRINGS = { + 'disabled': ugettext_lazy('disabled'), + 'auto': ugettext_lazy('automatic'), + 'manual': ugettext_lazy('manual'), + 'shared': ugettext_lazy('shared'), + 'link-local': ugettext_lazy('link-local'), +} + +# i18n for device.state +# https://developer.gnome.org/libnm/1.29/libnm-nm-dbus-interface.html#NMDeviceState +DEVICE_STATE_STRINGS = { + 'unknown': ugettext_lazy('unknown'), + 'unmanaged': ugettext_lazy('unmanaged'), + 'unavailable': ugettext_lazy('unavailable'), + 'disconnected': ugettext_lazy('disconnected'), + 'prepare': ugettext_lazy('preparing'), + 'config': ugettext_lazy('connecting'), + 'need-auth': ugettext_lazy('needs authentication'), + 'ip-config': ugettext_lazy('requesting address'), + 'ip-check': ugettext_lazy('checking'), + 'secondaries': ugettext_lazy('waiting for secondary'), + 'activated': ugettext_lazy('activated'), + 'deactivating': ugettext_lazy('deactivating'), + 'failed': ugettext_lazy('failed'), +} + +# i18n for device.state_reason +# https://developer.gnome.org/libnm/1.29/libnm-nm-dbus-interface.html#NMDeviceStateReason +DEVICE_STATE_REASON_STRINGS = { + 'none': + ugettext_lazy('no reason'), + 'unknown': + ugettext_lazy('unknown error'), + 'now-managed': + ugettext_lazy('device is now managed'), + 'now-unmanaged': + ugettext_lazy('device is now unmanaged'), + 'config-failed': + ugettext_lazy('configuration failed'), + 'no-secrets': + ugettext_lazy('secrets required'), + 'dhcp-start-failed': + ugettext_lazy('DHCP client failed to start'), + 'dhcp-error': + ugettext_lazy('DHCP client error'), + 'dhcp-failed': + ugettext_lazy('DHCP client failed'), + 'shared-start-failed': + ugettext_lazy('shared connection service failed to start'), + 'shared-failed': + ugettext_lazy('shared connection service failed'), + 'removed': + ugettext_lazy('device was removed'), + 'user-requested': + ugettext_lazy('device disconnected by user'), + 'dependency-failed': + ugettext_lazy('a dependency of the connection failed'), + 'ssid-not-found': + ugettext_lazy('Wi-Fi network not found'), + 'secondary-connection-failed': + ugettext_lazy('a secondary connection failed'), + 'new-activation': + ugettext_lazy('new connection activation was enqueued'), + 'ip-address-duplicate': + ugettext_lazy('a duplicate IP address was detected'), + 'ip-method-unsupported': + ugettext_lazy('selected IP method is not supported'), +} + +# i18n for device.type +# https://developer.gnome.org/libnm/1.29/libnm-nm-dbus-interface.html#NMDeviceType +DEVICE_TYPE_STRINGS = { + 'unknown': ugettext_lazy('unknown'), + 'ethernet': ugettext_lazy('Ethernet'), + 'wifi': ugettext_lazy('Wi-Fi'), + 'generic': ugettext_lazy('generic'), + 'tun': ugettext_lazy('TUN or TAP interface'), + 'wireguard': ugettext_lazy('WireGuard'), +} + +# i18n for wireless.mode +# https://developer.gnome.org/libnm/1.29/libnm-nm-dbus-interface.html#NM80211Mode +WIRELESS_MODE_STRINGS = { + 'unknown': ugettext_lazy('unknown'), + 'adhoc': ugettext_lazy('ad-hoc'), + 'infra': ugettext_lazy('infrastructure'), + 'ap': ugettext_lazy('access point'), + 'mesh': ugettext_lazy('mesh point'), +} + def index(request): """Show connection list.""" @@ -52,6 +145,14 @@ def show(request, uuid): # Connection status connection_status = network.get_status_from_connection(connection) + connection_status['zone_string'] = dict(network.ZONES).get( + connection_status['zone'], connection_status['zone']) + connection_status['ipv4']['method_string'] = CONNECTION_METHOD_STRINGS.get( + connection_status['ipv4']['method'], + connection_status['ipv4']['method']) + connection_status['ipv6']['method_string'] = CONNECTION_METHOD_STRINGS.get( + connection_status['ipv6']['method'], + connection_status['ipv6']['method']) # Active connection status try: @@ -72,12 +173,21 @@ def show(request, uuid): device = network.get_device_by_interface_name(interface_name) device_status = network.get_status_from_device(device) + device_status['state_string'] = DEVICE_STATE_STRINGS.get( + device_status['state'], device_status['state']) + device_status['state_reason_string'] = DEVICE_STATE_REASON_STRINGS.get( + device_status['state_reason'], device_status['state_reason']) + device_status['type_string'] = DEVICE_TYPE_STRINGS.get( + device_status['type'], device_status['type']) # Access point status access_point_status = None if connection_status['type'] == '802-11-wireless': access_point_status = network.get_status_from_wifi_access_point( device, connection_status['wireless']['ssid']) + connection_status['wireless'][ + 'mode_string'] = WIRELESS_MODE_STRINGS.get( + connection['wireless']['mode'], connection['wireless']['mode']) return TemplateResponse( request, 'connection_show.html', { diff --git a/plinth/network.py b/plinth/network.py index 7e8600408..fd13a3625 100644 --- a/plinth/network.py +++ b/plinth/network.py @@ -21,6 +21,8 @@ logger = logging.getLogger(__name__) _client = None +ZONES = [('external', _('External')), ('internal', _('Internal'))] + CONNECTION_TYPE_NAMES = collections.OrderedDict([ ('802-3-ethernet', _('Ethernet')), ('802-11-wireless', _('Wi-Fi')), @@ -51,6 +53,7 @@ def ipv4_int_to_string(address_int): def init(): """Create and keep a network manager client instance.""" + def new_callback(source_object, result, user_data): """Called when new() operation is complete.""" global _client