From 3b514a389c59c7778fb50753e36a0452e13b66fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michelle=20=22MishManners=C2=AE=E2=84=A2=22=20Duke?= <36594527+mishmanners@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:38:55 +1000 Subject: [PATCH] Add mult-word translation support Supports the translation of multiple words at once. Added tests to check that this works and displays correctly. --- src/components/client/Search.jsx | 33 +++++++++++++++++-------- tests/translations.spec.js | 42 +++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/components/client/Search.jsx b/src/components/client/Search.jsx index 64ffe1a..618893a 100644 --- a/src/components/client/Search.jsx +++ b/src/components/client/Search.jsx @@ -54,19 +54,25 @@ export const Search = () => { const fromLanguage = formData.get('fromLanguage'); const toLanguage = formData.get('toLanguage'); - // Get the search query from the form input - const word = formData.get('wordSearch').trim().toLowerCase(); + // Split the search query on comma + const words = formData.get('wordSearch').split(',').map((word) => word.trim().toLowerCase()); if (fromLanguage && toLanguage) { // Initialize an object to store the translation results let translations = []; - // Retrieve translations for the selected languages - try { - translations.push([word, getTranslationsForLanguages(fromLanguage, toLanguage, word)]); - } catch(e) { - translations.push([word, toError(e)]); - } + // Iterate over each unique word and perform translation + new Set(words).forEach((word) => { + // Retrieve translations for the selected languages + try { + translations.push([ + word, + getTranslationsForLanguages(fromLanguage, toLanguage, word) + ]); + } catch(e) { + translations.push([word, toError(e)]); + } + }); // Display translations to the user as needed setTranslationResults(translations); @@ -104,8 +110,15 @@ export const Search = () => { {/* Display translation results */}