From 52391c922e5c6aaf9ec7ef49b6fa2189b37bd07b Mon Sep 17 00:00:00 2001 From: John Livingston Date: Mon, 1 Jul 2024 14:36:59 +0200 Subject: [PATCH] Poll WIP (#231): * front-end: translate the form fields labels --- .../plugins/poll/components/poll-form-view.js | 34 +++++++++++++++++-- .../custom/plugins/poll/modals/poll-form.js | 1 - conversejs/loc.keys.js | 8 ++++- languages/en.yml | 6 ++++ languages/fr.yml | 9 +++++ 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/conversejs/custom/plugins/poll/components/poll-form-view.js b/conversejs/custom/plugins/poll/components/poll-form-view.js index 32110ab7..a0fba934 100644 --- a/conversejs/custom/plugins/poll/components/poll-form-view.js +++ b/conversejs/custom/plugins/poll/components/poll-form-view.js @@ -25,6 +25,8 @@ export default class MUCPollFormView extends CustomElement { } } + _fieldTranslationMap = new Map() + async initialize () { this.alert_message = undefined if (!this.model) { @@ -32,6 +34,7 @@ export default class MUCPollFormView extends CustomElement { return } try { + this._initFieldTranslations() const stanza = await this._fetchPollForm() const query = stanza.querySelector('query') const xform = sizzle(`x[xmlns="${Strophe.NS.XFORM}"]`, query)[0] @@ -39,9 +42,12 @@ export default class MUCPollFormView extends CustomElement { throw Error('Missing xform in stanza') } - this.title = xform.querySelector('title')?.textContent ?? '' - this.instructions = xform.querySelector('instructions')?.textContent ?? '' + // eslint-disable-next-line no-undef + this.title = __(LOC_poll_title) // xform.querySelector('title')?.textContent ?? '' + // eslint-disable-next-line no-undef + this.instructions = __(LOC_poll_instructions) // xform.querySelector('instructions')?.textContent ?? '' this.form_fields = Array.from(xform.querySelectorAll('field')).map(field => { + this._translateField(field) return u.xForm2TemplateResult(field, stanza) }) } catch (err) { @@ -63,6 +69,30 @@ export default class MUCPollFormView extends CustomElement { ) } + _initFieldTranslations () { + // eslint-disable-next-line no-undef + this._fieldTranslationMap.set('muc#roompoll_question', __(LOC_poll_question)) + // eslint-disable-next-line no-undef + this._fieldTranslationMap.set('muc#roompoll_duration', __(LOC_poll_duration)) + // eslint-disable-next-line no-undef + this._fieldTranslationMap.set('muc#roompoll_anonymous_results', __(LOC_poll_anonymous_results)) + for (let i = 1; i <= 10; i++) { + this._fieldTranslationMap.set( + 'muc#roompoll_choice' + i.toString(), + // eslint-disable-next-line no-undef + __(LOC_poll_choice_n).replace('{{N}}', i.toString()) + ) + } + } + + _translateField (field) { + const v = field.getAttribute('var') + const label = this._fieldTranslationMap.get(v) + if (label) { + field.setAttribute('label', label) + } + } + async formSubmit (ev) { ev.preventDefault() try { diff --git a/conversejs/custom/plugins/poll/modals/poll-form.js b/conversejs/custom/plugins/poll/modals/poll-form.js index 137e65d0..038e5c44 100644 --- a/conversejs/custom/plugins/poll/modals/poll-form.js +++ b/conversejs/custom/plugins/poll/modals/poll-form.js @@ -7,7 +7,6 @@ import BaseModal from 'plugins/modal/modal.js' import { api } from '@converse/headless/core' import { modal_close_button as ModalCloseButton } from 'plugins/modal/templates/buttons.js' import { html } from 'lit' -import 'livechat-external-login-content.js' class PollFormModal extends BaseModal { initialize () { diff --git a/conversejs/loc.keys.js b/conversejs/loc.keys.js index ba601544..6d167438 100644 --- a/conversejs/loc.keys.js +++ b/conversejs/loc.keys.js @@ -36,7 +36,13 @@ const locKeys = [ 'task_list_pick_empty', 'task_list_pick_message', 'muted_anonymous_message', - 'new_poll' + 'new_poll', + 'poll_question', + 'poll_duration', + 'poll_anonymous_results', + 'poll_choice_n', + 'poll_title', + 'poll_instructions' ] module.exports = locKeys diff --git a/languages/en.yml b/languages/en.yml index b29d1491..99f53d50 100644 --- a/languages/en.yml +++ b/languages/en.yml @@ -565,3 +565,9 @@ livechat_configuration_channel_terms_desc: | You can configure a "terms & conditions" message that will be shown to users joining your chatrooms. new_poll: Create a new poll +poll_title: New poll +poll_instructions: Complete and submit this form to create a new poll. This will end and replace any existing poll. +poll_question: Question +poll_duration: Poll duration (in minutes) +poll_anonymous_results: Anonymous results +poll_choice_n: Choice {{N}} diff --git a/languages/fr.yml b/languages/fr.yml index 4d15375e..bfe4a7ea 100644 --- a/languages/fr.yml +++ b/languages/fr.yml @@ -545,3 +545,12 @@ livechat_configuration_channel_terms_label: Conditions d'utilisation tchat de la livechat_configuration_channel_terms_desc: "Vous pouvez configurer un message de \"\ conditions d'utilisation\" qui sera affiché aux utilisateur⋅rices qui rejoignent vos salons de discussion.\n" + + +new_poll: Créer un nouveau sondage +poll_title: Nouveau sondage +poll_instructions: Complétez et soumettez ce formulaire pour créer un nouveau sondage. Ceci mettra fin au sondage précédent le cas échéant. +poll_question: Question +poll_duration: Durée du sondage (en minutes) +poll_anonymous_results: Résultats anonymes +poll_choice_n: Choix {{N}}