From 82f1868db36f886f5989d7caaaef05ecda27ecb2 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 28 May 2021 11:21:24 -0400 Subject: [PATCH] Add setLocale support --- app/static/app/js/main.jsx | 4 ++++ app/static/app/js/translations/functions.js | 20 ++++++++++++++++++++ app/templates/app/dev_tools.html | 9 ++++++++- app/urls.py | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 app/static/app/js/translations/functions.js diff --git a/app/static/app/js/main.jsx b/app/static/app/js/main.jsx index 9e73d999..445205a6 100644 --- a/app/static/app/js/main.jsx +++ b/app/static/app/js/main.jsx @@ -4,6 +4,7 @@ import ReactDOM from 'react-dom'; import React from 'react'; import $ from 'jquery'; import PluginsAPI from './classes/plugins/API'; +import { setLocale } from './translations/functions'; // Main is always executed first in the page @@ -12,6 +13,9 @@ import PluginsAPI from './classes/plugins/API'; window.ReactDOM = ReactDOM; window.React = React; +// Expose set locale function globally +window.setLocale = setLocale; + $(function(){ PluginsAPI.App.triggerReady(); }); diff --git a/app/static/app/js/translations/functions.js b/app/static/app/js/translations/functions.js new file mode 100644 index 00000000..fedb86cf --- /dev/null +++ b/app/static/app/js/translations/functions.js @@ -0,0 +1,20 @@ +import $ from 'jquery'; + +function setLocale(locale){ + var fd = new FormData(); + fd.append('language', locale); + + $.ajax({ + url: `/i18n/setlang/`, + contentType: false, + processData: false, + data: fd, + type: 'POST' + }).done(function(){ + location.reload(true); + }).fail(function(e){ + console.error("Cannot set locale", e); + }); +} + +export { setLocale }; \ No newline at end of file diff --git a/app/templates/app/dev_tools.html b/app/templates/app/dev_tools.html index e6bd8dfb..0d7943e1 100644 --- a/app/templates/app/dev_tools.html +++ b/app/templates/app/dev_tools.html @@ -12,7 +12,7 @@
{% trans 'Current Locale:' %} {{ current_locale }}
- {% trans "To change locale, modify your browser's locale settings, then refresh the page." %} + {% trans "To change locale, modify your browser's locale settings, then refresh the page, or use the set locale button below." %}

@@ -22,6 +22,13 @@
+
+
+ + + +
+
diff --git a/app/urls.py b/app/urls.py index 462e0559..c0a70bbd 100644 --- a/app/urls.py +++ b/app/urls.py @@ -44,6 +44,7 @@ urlpatterns = [ # TODO: add caching: https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#note-on-performance url(r'^jsi18n/', JavaScriptCatalog.as_view(packages=['app']), name='javascript-catalog'), + url(r'^i18n/', include('django.conf.urls.i18n')), ] handler404 = app_views.handler404