From 83eb4c39e5481ead373bc764dcdae0a7ee9a5872 Mon Sep 17 00:00:00 2001
From: nimbleghost <132819643+nimbleghost@users.noreply.github.com>
Date: Wed, 14 Jun 2023 20:58:58 +0200
Subject: [PATCH] Add i18n to service worker
---
web/public/static/langs/en.json | 7 ++++++-
web/public/sw.js | 12 +++++++-----
web/src/{components/i18n.jsx => app/i18n.js} | 0
web/src/components/App.jsx | 2 +-
4 files changed, 14 insertions(+), 7 deletions(-)
rename web/src/{components/i18n.jsx => app/i18n.js} (100%)
diff --git a/web/public/static/langs/en.json b/web/public/static/langs/en.json
index f6c8b881..a2ec2dea 100644
--- a/web/public/static/langs/en.json
+++ b/web/public/static/langs/en.json
@@ -90,6 +90,7 @@
"notifications_actions_open_url_title": "Go to {{url}}",
"notifications_actions_not_supported": "Action not supported in web app",
"notifications_actions_http_request_title": "Send HTTP {{method}} to {{url}}",
+ "notifications_actions_failed_notification": "Unsuccessful action",
"notifications_none_for_topic_title": "You haven't received any notifications for this topic yet.",
"notifications_none_for_topic_description": "To send notifications to this topic, simply PUT or POST to the topic URL.",
"notifications_none_for_any_title": "You haven't received any notifications.",
@@ -391,5 +392,9 @@
"error_boundary_stack_trace": "Stack trace",
"error_boundary_gathering_info": "Gather more info …",
"error_boundary_unsupported_indexeddb_title": "Private browsing not supported",
- "error_boundary_unsupported_indexeddb_description": "The ntfy web app needs IndexedDB to function, and your browser does not support IndexedDB in private browsing mode.
While this is unfortunate, it also doesn't really make a lot of sense to use the ntfy web app in private browsing mode anyway, because everything is stored in the browser storage. You can read more about it in this GitHub issue, or talk to us on Discord or Matrix."
+ "error_boundary_unsupported_indexeddb_description": "The ntfy web app needs IndexedDB to function, and your browser does not support IndexedDB in private browsing mode.
While this is unfortunate, it also doesn't really make a lot of sense to use the ntfy web app in private browsing mode anyway, because everything is stored in the browser storage. You can read more about it in this GitHub issue, or talk to us on Discord or Matrix.",
+ "web_push_subscription_expiring_title": "Notifications will be paused",
+ "web_push_subscription_expiring_body": "Open ntfy to continue receiving notifications",
+ "web_push_unknown_notification_title": "Unknown notification received from server",
+ "web_push_unknown_notification_body": "You may need to update ntfy by opening the web app"
}
diff --git a/web/public/sw.js b/web/public/sw.js
index ba70d01a..a29a2039 100644
--- a/web/public/sw.js
+++ b/web/public/sw.js
@@ -6,6 +6,8 @@ import { NetworkFirst } from "workbox-strategies";
import { dbAsync } from "../src/app/db";
import { formatMessage, formatTitleWithDefault } from "../src/app/notificationUtils";
+import i18n from "../src/app/i18n";
+
/**
* General docs for service workers and PWAs:
* https://vite-pwa-org.netlify.app/guide/
@@ -70,8 +72,8 @@ const showNotification = async (data) => {
*/
const handlePush = async (data) => {
if (data.event === "subscription_expiring") {
- await self.registration.showNotification("Notifications will be paused", {
- body: "Open ntfy to continue receiving notifications",
+ await self.registration.showNotification(i18n.t("web_push_subscription_expiring_title"), {
+ body: i18n.t("web_push_subscription_expiring_body"),
icon,
data,
});
@@ -85,8 +87,8 @@ const handlePush = async (data) => {
await showNotification(data);
} else {
// We can't ignore the push, since permission can be revoked by the browser
- await self.registration.showNotification("Unknown notification received from server", {
- body: "You may need to update ntfy by opening the web app",
+ await self.registration.showNotification(i18n.t("web_push_unknown_notification_title"), {
+ body: i18n.t("web_push_unknown_notification_body"),
icon,
data,
});
@@ -132,7 +134,7 @@ const handleClick = async (event) => {
}
} catch (e) {
console.error("[ServiceWorker] Error performing http action", e);
- self.registration.showNotification(`Unsuccessful action ${action.label} (${action.action})`, {
+ self.registration.showNotification(`${i18n.t('notifications_actions_failed_notification')}: ${action.label} (${action.action})`, {
body: e.message,
icon,
});
diff --git a/web/src/components/i18n.jsx b/web/src/app/i18n.js
similarity index 100%
rename from web/src/components/i18n.jsx
rename to web/src/app/i18n.js
diff --git a/web/src/components/App.jsx b/web/src/components/App.jsx
index 62b57f8c..4854fc85 100644
--- a/web/src/components/App.jsx
+++ b/web/src/components/App.jsx
@@ -18,10 +18,10 @@ import routes from "./routes";
import { useAccountListener, useBackgroundProcesses, useConnectionListeners } from "./hooks";
import PublishDialog from "./PublishDialog";
import Messaging from "./Messaging";
-import "./i18n"; // Translations!
import Login from "./Login";
import Signup from "./Signup";
import Account from "./Account";
+import "../app/i18n"; // Translations!
export const AccountContext = createContext(null);