From 6f7c5c50f7db3974f659a050cc74bda0b32b4ef2 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 4 Jul 2024 17:39:40 +0200 Subject: [PATCH] Poll WIP (#231): * frontend message localization --- conversejs/custom/plugins/poll/index.js | 25 ++++++++++++++++++++++- conversejs/loc.keys.js | 5 ++++- languages/en.yml | 5 ++++- languages/fr.yml | 7 +++++++ prosody-modules/mod_muc_poll/README.md | 2 +- prosody-modules/mod_muc_poll/poll.lib.lua | 2 +- server/lib/loc.ts | 4 ++++ server/lib/prosody/config/content.ts | 4 ++++ 8 files changed, 49 insertions(+), 5 deletions(-) diff --git a/conversejs/custom/plugins/poll/index.js b/conversejs/custom/plugins/poll/index.js index b87f804c..1f4953b7 100644 --- a/conversejs/custom/plugins/poll/index.js +++ b/conversejs/custom/plugins/poll/index.js @@ -5,6 +5,7 @@ import { _converse, converse } from '../../../src/headless/core.js' import { getHeadingButtons } from './utils.js' import { POLL_MESSAGE_TAG, POLL_QUESTION_TAG, POLL_CHOICE_TAG } from './constants.js' +import { __ } from 'i18n' import './modals/poll-form.js' import './components/poll-view.js' import './components/poll-form-view.js' @@ -24,6 +25,23 @@ converse.plugins.add('livechat-converse-poll', { _converse.api.listen.on('getHeadingButtons', getHeadingButtons) _converse.api.listen.on('parseMUCMessage', (stanza, attrs) => { + // Localizing specific error messages + if (attrs.is_error) { + // eslint-disable-next-line no-undef, camelcase + if (attrs.error_text === LOC_poll_is_over) { + // eslint-disable-next-line no-undef + attrs.error_text = __(LOC_poll_is_over) + // eslint-disable-next-line no-undef, camelcase + } else if (attrs.error_text === LOC_poll_choice_invalid) { + // eslint-disable-next-line no-undef + attrs.error_text = __(LOC_poll_choice_invalid) + // eslint-disable-next-line no-undef, camelcase + } else if (attrs.error_text === LOC_poll_anonymous_vote_ok) { + // eslint-disable-next-line no-undef + attrs.error_text = __(LOC_poll_anonymous_vote_ok) + } + } + // Checking if there is any poll data in the message. const poll = sizzle(POLL_MESSAGE_TAG, stanza)?.[0] if (!poll) { @@ -55,10 +73,15 @@ converse.plugins.add('livechat-converse-poll', { }) } + // We will also translate some strings here. + // eslint-disable-next-line no-undef + const body = (attrs.body ?? '').replace(LOC_poll_is_over, __(LOC_poll_is_over)) + return Object.assign( attrs, { - current_poll: currentPoll + current_poll: currentPoll, + body } ) }) diff --git a/conversejs/loc.keys.js b/conversejs/loc.keys.js index a29e0088..c209e67d 100644 --- a/conversejs/loc.keys.js +++ b/conversejs/loc.keys.js @@ -45,7 +45,10 @@ const locKeys = [ 'poll_instructions', 'poll_end', 'poll', - 'poll_vote_instructions' + 'poll_vote_instructions', + 'poll_is_over', + 'poll_choice_invalid', + 'poll_anonymous_vote_ok' ] module.exports = locKeys diff --git a/languages/en.yml b/languages/en.yml index 4807bdb5..9dde54d3 100644 --- a/languages/en.yml +++ b/languages/en.yml @@ -574,4 +574,7 @@ poll_anonymous_results: Anonymous results poll_choice_n: 'Choice {{N}}:' poll_end: 'Poll ends at:' poll_vote_instructions: | - To vote, click on your choice or send a message with an exclamation mark followed by your choice number (Example: !1) + To vote, click on your choice or send a message with an exclamation mark followed by your choice number (Example: !1). +poll_is_over: This poll is now over. +poll_choice_invalid: This choice is not valid. +poll_anonymous_vote_ok: Your vote is taken into account. Votes are anonymous, they will not be shown to other participants. diff --git a/languages/fr.yml b/languages/fr.yml index 1c163a62..9d67dc75 100644 --- a/languages/fr.yml +++ b/languages/fr.yml @@ -554,3 +554,10 @@ poll_question: Question poll_duration: Durée du sondage (en minutes) poll_anonymous_results: Résultats anonymes poll_choice_n: 'Choix {{N}} :' +poll_end: 'Fin du sondage :' +poll_vote_instructions: | + Pour voter, cliquez sur votre choix, ou envoyez un message avec un point d'exclamation suivi de votre choix (Exemple: !1). + +poll_is_over: Ce sondage est à présent terminé. +poll_choice_invalid: Ce choix n'est pas valide. +poll_anonymous_vote_ok: Votre vote a été pris en compte. Les votes sont anonymes, ils ne seront pas montrés aux autres participant⋅es. diff --git a/prosody-modules/mod_muc_poll/README.md b/prosody-modules/mod_muc_poll/README.md index 4dda4fe7..b27311b2 100644 --- a/prosody-modules/mod_muc_poll/README.md +++ b/prosody-modules/mod_muc_poll/README.md @@ -30,4 +30,4 @@ Here are the existing strings and default values: * poll_string_over: This poll is now over. * poll_string_vote_instructions: Send a message with an exclamation mark followed by your choice number to vote. Example: !1 * poll_string_invalid_choice: This choice is not valid. -* poll_string_anonymous_vote_ok: Your vote is taken into account. Votes are anonymous, it will not be shown to other participants. +* poll_string_anonymous_vote_ok: Your vote is taken into account. Votes are anonymous, they will not be shown to other participants. diff --git a/prosody-modules/mod_muc_poll/poll.lib.lua b/prosody-modules/mod_muc_poll/poll.lib.lua index a546ffd1..471d17cc 100644 --- a/prosody-modules/mod_muc_poll/poll.lib.lua +++ b/prosody-modules/mod_muc_poll/poll.lib.lua @@ -12,7 +12,7 @@ local poll_end_message = module:require("message").poll_end_message; local schedule_poll_update_message = module:require("message").schedule_poll_update_message; local string_poll_invalid_choice = module:get_option_string("poll_string_invalid_choice") or "This choice is not valid."; -local string_poll_anonymous_vote_ok = module:get_option_string("poll_string_anonymous_vote_ok") or "Your vote is taken into account. Votes are anonymous, it will not be shown to other participants."; +local string_poll_anonymous_vote_ok = module:get_option_string("poll_string_anonymous_vote_ok") or "Your vote is taken into account. Votes are anonymous, they will not be shown to other participants."; local string_poll_over = module:get_option_string("poll_string_over") or "This poll is now over."; local scheduled_end = {}; diff --git a/server/lib/loc.ts b/server/lib/loc.ts index 67eb0ecf..f44d0548 100644 --- a/server/lib/loc.ts +++ b/server/lib/loc.ts @@ -17,6 +17,10 @@ const locContent: Map = new Map() * - We are using keys to identify strings * - the `loc` function gets the english segment for the key * - the build-languages.js script builds all needed files. + * + * The loc function is also used to customize some labels on Prosody backend. + * The front-end will then replace english strings by their translation. + * (see mod_muc_poll for example). * @param key The key to translate */ function loc (key: string): string { diff --git a/server/lib/prosody/config/content.ts b/server/lib/prosody/config/content.ts index ca629d98..a95b3b38 100644 --- a/server/lib/prosody/config/content.ts +++ b/server/lib/prosody/config/content.ts @@ -5,6 +5,7 @@ import type { ProsodyFilePaths } from './paths' import type { ExternalComponent } from './components' import { BotConfiguration } from '../../configuration/bot' +import { loc } from '../../loc' import { userInfo } from 'os' /** @@ -536,6 +537,9 @@ class ProsodyConfigContent { usePoll (): void { this.muc.add('modules_enabled', 'muc_poll') this.muc.set('poll_groupchat_votes_priority', 1000) + this.muc.set('poll_string_over', loc('poll_is_over')) + this.muc.set('poll_string_invalid_choice', loc('poll_choice_invalid')) + this.muc.set('poll_string_anonymous_vote_ok', loc('poll_anonymous_vote_ok')) } addMucAdmins (jids: string[]): void {