From 182d752ac689b80f82028ab340df6ede2fbd5566 Mon Sep 17 00:00:00 2001 From: Cranberry <20140992+cranberry3148@users.noreply.github.com> Date: Fri, 3 Jan 2025 00:06:06 +0100 Subject: [PATCH] add: chrome based translation for modal --- .../public/_locales/de/messages.json | 65 +++++++++++++++++++ .../public/_locales/en/messages.json | 65 +++++++++++++++++++ Extension-React/public/content.js | 62 +++++++++++------- Extension-React/public/manifest.json | 1 + 4 files changed, 168 insertions(+), 25 deletions(-) create mode 100644 Extension-React/public/_locales/de/messages.json create mode 100644 Extension-React/public/_locales/en/messages.json diff --git a/Extension-React/public/_locales/de/messages.json b/Extension-React/public/_locales/de/messages.json new file mode 100644 index 00000000..e1da0e66 --- /dev/null +++ b/Extension-React/public/_locales/de/messages.json @@ -0,0 +1,65 @@ +{ + "language_name": { + "message": "Deutsch" + }, + "apply": { + "message": "Anwenden" + }, + "ignore": { + "message": "Ignorieren" + }, + "syrup_logo": { + "message": "Syrup Logo" + }, + "show_extension": { + "message": "Erweiterung anzeigen" + }, + "no_auto_apply_setup": { + "message": "Keine Seitenvoreinstellung für diese Seite." + }, + "click_apply_to_try_them_all": { + "message": "Klicke auf \"Anwenden\", um alle zu scannen." + }, + "syrup_found_coupons": { + "message": "Syrup hat Gutscheine gefunden!" + }, + "got_it": { + "message": "Okay" + }, + "testing_coupons": { + "message": "Teste Gutscheine..." + }, + "testing_coupon_current_of_total": { + "message": "Teste Gutschein %currentIndex% von %total%" + }, + "cancel": { + "message": "Abbrechen" + }, + "use_best": { + "message": "Besten verwenden" + }, + "testing_cancelled": { + "message": "Scan abgebrochen." + }, + "scan_was_stopped_no_coupons_applied": { + "message": "Scan wurde abgebrochen. Keine Gutscheine angewendet." + }, + "we_found_the_best_coupon": { + "message": "Wir haben den besten Gutschein gefunden!" + }, + "applied_coupon_and_saved_savings_new_total": { + "message": "Gutschein \"%bestCoupon%\" angewendet und %savings% eingespart. Neuer Gesamtwert: %finalPrice%" + }, + "no_better_price_found": { + "message": "Kein besserer Preis gefunden." + }, + "all_coupons_tested_but_none_lowered_your_total": { + "message": "Alle Gutscheine getestet, aber kein Preis war besser." + }, + "now_trying_best_so_far": { + "message": "Teste nun \"%currentCoupon%\". Bester Preis bis jetzt: %bestPriceSoFar%" + }, + "best_price_so_far": { + "message": "Bester Preis bis jetzt: %bestPriceSoFar%" + } +} \ No newline at end of file diff --git a/Extension-React/public/_locales/en/messages.json b/Extension-React/public/_locales/en/messages.json new file mode 100644 index 00000000..2b942ef3 --- /dev/null +++ b/Extension-React/public/_locales/en/messages.json @@ -0,0 +1,65 @@ +{ + "language_name": { + "message": "English" + }, + "apply": { + "message": "Apply" + }, + "ignore": { + "message": "Ignore" + }, + "syrup_logo": { + "message": "Syrup Logo" + }, + "show_extension": { + "message": "Show Extension" + }, + "no_auto_apply_setup": { + "message": "No auto-apply setup for this site." + }, + "click_apply_to_try_them_all": { + "message": "Click Apply to try them all." + }, + "syrup_found_coupons": { + "message": "Syrup found coupons!" + }, + "got_it": { + "message": "Got it" + }, + "testing_coupons": { + "message": "Testing Coupons..." + }, + "testing_coupon_current_of_total": { + "message": "Testing coupon %currentIndex% of %total%" + }, + "cancel": { + "message": "Cancel" + }, + "use_best": { + "message": "Use Best" + }, + "testing_cancelled": { + "message": "Testing Cancelled." + }, + "scan_was_stopped_no_coupons_applied": { + "message": "Scan was stopped. No coupons applied." + }, + "we_found_the_best_coupon": { + "message": "We found the best coupon!" + }, + "applied_coupon_and_saved_savings_new_total": { + "message": "Applied coupon \"%bestCoupon%\" and saved %savings%. New total: %finalPrice%" + }, + "no_better_price_found": { + "message": "No better price found." + }, + "all_coupons_tested_but_none_lowered_your_total": { + "message": "All coupons tested, but none lowered your total." + }, + "now_trying_best_so_far": { + "message": "Now trying \"%currentCoupon%\". Best price so far: %bestPriceSoFar%" + }, + "best_price_so_far": { + "message": "Best price so far: $%bestPriceSoFar%" + } +} \ No newline at end of file diff --git a/Extension-React/public/content.js b/Extension-React/public/content.js index 5cfdff06..ea819b47 100644 --- a/Extension-React/public/content.js +++ b/Extension-React/public/content.js @@ -74,6 +74,15 @@ }); } + function getTranslation(key, data = {}) { + let translated = chrome.i18n.getMessage(key); + for (const [name, value] of Object.entries(data)) { + translated = translated.replace(new RegExp(`%${name}%`, "g"), value); + } + return translated; + } + const __ = getTranslation; + function isVisible(el) { if (!el) return false; return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length); @@ -87,7 +96,6 @@ function replaceValue(selector, value) { const el = document.querySelector(selector); - console.log("setting", el, "to", value); if (el) { el.value = value; // Fire typical events to ensure the page sees the input @@ -318,7 +326,7 @@ container.innerHTML = `

- Testing Coupons... + ${__("testing_coupons")}

@@ -333,7 +341,7 @@ cursor: pointer; margin-right: 8px; "> - Cancel + ${__("cancel")} `; @@ -380,12 +388,12 @@ const statusEl = container.querySelector("#syrup-test-status"); if (stepEl) { - stepEl.textContent = `Testing coupon ${currentIndex} of ${total}`; + stepEl.textContent = __(`testing_coupon_current_of_total`, { currentIndex, total }); } if (statusEl) { statusEl.textContent = currentCoupon - ? `Now trying "${currentCoupon}". Best price so far: $${bestPriceSoFar}` - : `Best price so far: $${bestPriceSoFar}`; + ? __(`now_trying_best_so_far`, { currentCoupon, bestPriceSoFar }) + : __(`best_price_so_far`, { bestPriceSoFar }); } } @@ -405,29 +413,33 @@ if (useBestBtn) useBestBtn.remove(); if (wasCancelled) { - if (stepEl) stepEl.textContent = "Testing Cancelled."; + if (stepEl) stepEl.textContent = __("testing_cancelled"); if (statusEl) { - statusEl.textContent = "Scan was stopped. No coupons applied."; + statusEl.textContent = __("scan_was_stopped_no_coupons_applied"); } } else if (bestCoupon) { - if (stepEl) stepEl.textContent = "We found the best coupon!"; + if (stepEl) stepEl.textContent = __("we_found_the_best_coupon"); if (statusEl) { - statusEl.textContent = `Applied coupon "${bestCoupon}" and saved $${savings.toFixed( + statusEl.textContent = __(`applied_coupon_and_saved_savings_new_total`, { + bestCoupon, + savings: savings.toFixed( 2 - )}. New total: $${finalPrice.toFixed(2)}`; + ), + finalPrice: finalPrice.toFixed(2) + }); } } else { - if (stepEl) stepEl.textContent = "No better price found."; + if (stepEl) stepEl.textContent = __("no_better_price_found"); if (statusEl) { statusEl.textContent = - "All coupons tested, but none lowered your total."; + __("all_coupons_tested_but_none_lowered_your_total"); } } // "Got it" button const gotItBtn = document.createElement("button"); gotItBtn.id = "syrup-got-it-btn"; - gotItBtn.textContent = "Got it"; + gotItBtn.textContent = __("got_it"); gotItBtn.style.marginTop = "15px"; gotItBtn.style.backgroundColor = "#28a745"; gotItBtn.style.color = "#fff"; @@ -463,10 +475,10 @@ font-family: Arial, sans-serif; ">
- Syrup Logo + ${__(
-

Syrup found coupons!

-

Click Apply to try them all.

+

${__("syrup_found_coupons")}

+

${__("click_apply_to_try_them_all")}

@@ -480,7 +492,7 @@ cursor: pointer; transition: background-color 0.2s ease; width: 100%; - ">Apply + ">${__("apply")} + ">${__("ignore")}
`; @@ -530,10 +542,10 @@ font-family: Arial, sans-serif; ">
- Syrup Logo + ${__(
-

Syrup found coupons!

-

No auto-apply setup for this site.

+

${__("syrup_found_coupons")}

+

${__("no_auto_apply_setup")}

@@ -547,7 +559,7 @@ cursor: pointer; transition: background-color 0.2s ease; width: 100%; - ">Show Extension + ">${__("show_extension")} + ">${__("ignore")}
`; diff --git a/Extension-React/public/manifest.json b/Extension-React/public/manifest.json index 5122052c..efccab07 100644 --- a/Extension-React/public/manifest.json +++ b/Extension-React/public/manifest.json @@ -4,6 +4,7 @@ "description": "A Honey alternative, find and apply the best coupons automatically.", "version": "0.3", "permissions": ["activeTab"], + "default_locale": "en", "background": { "service_worker": "background.js", "scripts": ["background.js"]