From c2b7c2d2fd0094c4c135a80e59d9afc15799da4e Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Wed, 1 Nov 2023 10:06:01 +0200 Subject: [PATCH] internalization --- files/frontend_translations/__init__.py | 28 ++++++++ files/frontend_translations/de.py | 37 +++++++++++ files/frontend_translations/el.py | 37 +++++++++++ files/frontend_translations/en.py | 37 +++++++++++ files/frontend_translations/fr.py | 37 +++++++++++ .../src/static/js/utils/helpers/translate.js | 9 +++ templates/cms/set_language.html | 65 +++++++++++++++++++ .../config/installation/translations.html | 57 ++++++++++++++++ 8 files changed, 307 insertions(+) create mode 100644 files/frontend_translations/__init__.py create mode 100644 files/frontend_translations/de.py create mode 100644 files/frontend_translations/el.py create mode 100644 files/frontend_translations/en.py create mode 100644 files/frontend_translations/fr.py create mode 100644 frontend/src/static/js/utils/helpers/translate.js create mode 100644 templates/cms/set_language.html create mode 100644 templates/config/installation/translations.html diff --git a/files/frontend_translations/__init__.py b/files/frontend_translations/__init__.py new file mode 100644 index 0000000..aabdbc6 --- /dev/null +++ b/files/frontend_translations/__init__.py @@ -0,0 +1,28 @@ +from django.conf import settings + +from .de import translations as de_translations +from .el import translations as el_translations +from .fr import translations as fr_translations + +translations = {} +translations['el'] = el_translations +translations['fr'] = fr_translations +translations['de'] = de_translations + + +def get_frontend_translations(language_code): + if language_code not in [pair[0] for pair in settings.LANGUAGES]: + return {} + + if language_code in ['en', 'en-us', 'en-gb']: + return {} + + translation = translations[language_code] + + # replace any keys from translation that contains a space with an underscore + # do not keep initial keys that contain a space + for key in list(translation.keys()): + if ' ' in key: + translation[key.replace(' ', '_')] = translation.pop(key) + + return translation diff --git a/files/frontend_translations/de.py b/files/frontend_translations/de.py new file mode 100644 index 0000000..b3460e1 --- /dev/null +++ b/files/frontend_translations/de.py @@ -0,0 +1,37 @@ +# German translation for strings in frontend app + +translations = { + "Home": "Startseite", + "Featured": "Empfohlen", + "Latest": "Neueste", + "Recommended": "Empfehlungen", + "Recent uploads": "Kürzlich hochgeladen", + "Tags": "Tags", + "Categories": "Kategorien", + "Members": "Mitglieder", + "Upload": "Hochladen", + "Upload media": "Medien hochladen", + "My media": "Meine Medien", + "My playlists": "Meine Wiedergabelisten", + "History": "Verlauf", + "Liked media": "Gefällt mir Medien", + "About": "Über", + "Terms": "Nutzungsbedingungen", + "Contact": "Kontakt", + "Language": "Sprache", + "Manage media": "Medien verwalten", + "Manage users": "Benutzer verwalten", + "Manage comments": "Kommentare verwalten", + "View all": "Alle anzeigen", + "VIEW ALL": "ALLE ANZEIGEN", + "view": "Ansicht", + "views": "Ansichten", + "Search": "Suchen", + "Up next": "Als Nächstes", + "AUTOPLAY": "AUTOPLAY", + "Sign out": "Abmelden", + "Sign in": "Anmelden", + "Register": "Registrieren", + "Edit profile": "Profil bearbeiten", + "Change password": "Passwort ändern", +} diff --git a/files/frontend_translations/el.py b/files/frontend_translations/el.py new file mode 100644 index 0000000..1ef4b62 --- /dev/null +++ b/files/frontend_translations/el.py @@ -0,0 +1,37 @@ +# Greek translation for strings in frontend app + +translations = { + "Home": "Αρχική", + "Featured": "Επιλεγμένα", + "Latest": "Πρόσφατα", + "Recommended": "Προτεινόμενα", + "Recent uploads": "Πρόσφατα αρχεία", + "Tags": "Ετικέτες", + "Categories": "Κατηγορίες", + "Members": "Μέλη", + "Upload": "Ανέβασμα αρχείου", + "Upload media": "Ανέβασμα αρχείων", + "My media": "Τα αρχεία μου", + "My playlists": "Οι λίστες μου", + "History": "Ιστορικό", + "Liked media": "Αγαπημένα", + "About": "Σχετικά", + "Terms": "Όροι", + "Contact": "Επικοινωνία", + "Language": "Γλώσσα", + "Manage media": "Διαχείριση αρχείων", + "Manage users": "Διαχείριση χρηστών", + "Manage comments": "Διαχείριση σχολίων", + "View all": "Δές τα όλα", + "VIEW ALL": "ΔΕΣ ΤΑ ΟΛΑ", + "view": "προβολή", + "views": "προβολές", + "Search": "Αναζήτηση", + "Up next": "Επόμενο", + "AUTOPLAY": "Αυτόματη αναπαραγωγή", + "Sign out": "Αποσύνδεση", + "Sign in": "Σύνδεση", + "Register": "Εγγραφή", + "Edit profile": "Επεξεργασία προφιλ", + "Change password": "Αλλαγή κωδικού", +} diff --git a/files/frontend_translations/en.py b/files/frontend_translations/en.py new file mode 100644 index 0000000..107d026 --- /dev/null +++ b/files/frontend_translations/en.py @@ -0,0 +1,37 @@ +# Translations listing + +translations = { + "Home": "", + "Featured": "", + "Latest": "", + "Recommended": "", + "Recent uploads": "", + "Tags": "", + "Categories": "", + "Members": "", + "Upload": "", + "Upload media": "", + "My media": "", + "My playlists": "", + "History": "", + "Liked media": "", + "About": "", + "Terms": "", + "Contact": "", + "Language": "", + "Manage media": "", + "Manage users": "", + "Manage comments": "", + "View all": "", + "VIEW ALL": "", + "view": "", + "views": "", + "Search": "", + "Up next": "", + "AUTOPLAY": "", + "Sign out": "", + "Sign in": "", + "Register": "", + "Edit profile": "", + "Change password": "", +} diff --git a/files/frontend_translations/fr.py b/files/frontend_translations/fr.py new file mode 100644 index 0000000..eb484d1 --- /dev/null +++ b/files/frontend_translations/fr.py @@ -0,0 +1,37 @@ +# French translation for strings in frontend app + +translations = { + "Home": "Accueil", + "Featured": "En vedette", + "Latest": "Dernier", + "Recommended": "Recommandé", + "Recent uploads": "Téléchargements récents", + "Tags": "Tags", + "Categories": "Catégories", + "Members": "Membres", + "Upload": "Télécharger", + "Upload media": "Télécharger un média", + "My media": "Mes médias", + "My playlists": "Mes listes de lecture", + "History": "Historique", + "Liked media": "Médias aimés", + "About": "À propos", + "Terms": "Conditions", + "Contact": "Contact", + "Language": "Langue", + "Manage media": "Gérer les médias", + "Manage users": "Gérer les utilisateurs", + "Manage comments": "Gérer les commentaires", + "View all": "Voir tout", + "VIEW ALL": "VOIR TOUT", + "view": "vue", + "views": "vues", + "Search": "Recherche", + "Up next": "À suivre", + "AUTOPLAY": "LECTURE AUTOMATIQUE", + "Sign out": "Se déconnecter", + "Sign in": "Se connecter", + "Register": "S'inscrire", + "Edit profile": "Modifier le profil", + "Change password": "Changer le mot de passe", +} diff --git a/frontend/src/static/js/utils/helpers/translate.js b/frontend/src/static/js/utils/helpers/translate.js new file mode 100644 index 0000000..21a34f8 --- /dev/null +++ b/frontend/src/static/js/utils/helpers/translate.js @@ -0,0 +1,9 @@ +// check templates/config/installation/translations.html for more + +export function translate_string(string) { + if (window.TRANSLATIONS && window.TRANSLATIONS[string]) { + return window.TRANSLATIONS[string]; + } else { + return string; + } +} diff --git a/templates/cms/set_language.html b/templates/cms/set_language.html new file mode 100644 index 0000000..880eb8b --- /dev/null +++ b/templates/cms/set_language.html @@ -0,0 +1,65 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block headtitle %}About - {{PORTAL_NAME}}{% endblock headtitle %} + +{% block headermeta %} + + + + + + + + + +{% endblock headermeta %} + +{% block innercontent %} +
+

Change Language

+
+

Select

+ +

+

{% csrf_token %} + + + +
+ +

+
+{% endblock %} diff --git a/templates/config/installation/translations.html b/templates/config/installation/translations.html new file mode 100644 index 0000000..724fcda --- /dev/null +++ b/templates/config/installation/translations.html @@ -0,0 +1,57 @@ +// These are the translations that will be used in frontend/src using the helper function +// translate_string (frontend/src/static/js/utils/helpers/translate.js) +// They are fetched from the backend, and specifically files/frontend_translations +// The Django backend is passing dictionary FRONTEND_TRANSLATIONS. +// This contains key values where keys do not have a space +// TRANSLATIONS object will be availaible as window.TRANSLATIONS + +// IMPORTANT: If you are starting the frontend app (localhost:8088), you are not getting at this +// POINT, so there are not translations. Translations can only be available and tested on the Django app +// and templates, that are loading React as well + +// TODO: pass the python dictionary on a way that can be parsed here and thus no need +// for this duplication taking place here + +var TRANSLATIONS = { + "Home": "{{FRONTEND_TRANSLATIONS.Home}}", + "Featured": "{{FRONTEND_TRANSLATIONS.Featured}}", + "View all": "{{FRONTEND_TRANSLATIONS.View_all}}", + "VIEW ALL": "{{FRONTEND_TRANSLATIONS.VIEW_ALL}}", + "Latest": "{{FRONTEND_TRANSLATIONS.Latest}}", + "Recommended": "{{FRONTEND_TRANSLATIONS.Recommended}}", + "Recent uploads": "{{FRONTEND_TRANSLATIONS.Recent_uploads}}", + "view": "{{FRONTEND_TRANSLATIONS.view}}", + "views": "{{FRONTEND_TRANSLATIONS.views}}", + "Search": "{{FRONTEND_TRANSLATIONS.Search}}", + + "Tags": "{{FRONTEND_TRANSLATIONS.Tags}}", + "Categories": "{{FRONTEND_TRANSLATIONS.Categories}}", + "Members": "{{FRONTEND_TRANSLATIONS.Members}}", + "Upload": "{{FRONTEND_TRANSLATIONS.Upload}}", + "Upload media": "{{FRONTEND_TRANSLATIONS.Upload_media}}", + + "My media": "{{FRONTEND_TRANSLATIONS.My_media}}", + "My playlists": "{{FRONTEND_TRANSLATIONS.My_playlists}}", + "History": "{{FRONTEND_TRANSLATIONS.History}}", + "Liked media": "{{FRONTEND_TRANSLATIONS.Liked_media}}", + "About": "{{FRONTEND_TRANSLATIONS.About}}", + "Terms": "{{FRONTEND_TRANSLATIONS.Terms}}", + "Contact": "{{FRONTEND_TRANSLATIONS.Contact}}", + "Language": "{{FRONTEND_TRANSLATIONS.Language}}", + "Manage media": "{{FRONTEND_TRANSLATIONS.Manage_media}}", + "Manage users": "{{FRONTEND_TRANSLATIONS.Manage_users}}", + "Manage comments": "{{FRONTEND_TRANSLATIONS.Manage_comments}}", + + "Up next": "{{FRONTEND_TRANSLATIONS.Up_next}}", + "AUTOPLAY": "{{FRONTEND_TRANSLATIONS.AUTOPLAY}}", + + "Sign out": "{{FRONTEND_TRANSLATIONS.Sign_out}}", + "Sign in": "{{FRONTEND_TRANSLATIONS.Sign_in}}", + "Register": "{{FRONTEND_TRANSLATIONS.Register}}", + "Edit profile": "{{FRONTEND_TRANSLATIONS.Edit_profile}}", + "Change password": "{{FRONTEND_TRANSLATIONS.Change_password}}", + +} + + +window.TRANSLATIONS = TRANSLATIONS; \ No newline at end of file