From 6e6f8caca807979be4889a5c1292aa15309a6a3a Mon Sep 17 00:00:00 2001
From: Marco Rodolfi <marco.rodolfi@tuta.io>
Date: Sat, 27 May 2023 13:55:26 +0200
Subject: Unified translation classes, fixed missing toaster translation and
 improved the error styling report.

---
 frontend/src/utils/TranslationHelper.tsx | 59 ++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 frontend/src/utils/TranslationHelper.tsx

(limited to 'frontend/src/utils/TranslationHelper.tsx')

diff --git a/frontend/src/utils/TranslationHelper.tsx b/frontend/src/utils/TranslationHelper.tsx
new file mode 100644
index 00000000..457a3159
--- /dev/null
+++ b/frontend/src/utils/TranslationHelper.tsx
@@ -0,0 +1,59 @@
+import { FC } from 'react';
+import { Translation } from 'react-i18next';
+
+import Logger from '../logger';
+import { InstallType } from '../plugin';
+
+export enum TranslationClass {
+  PLUGIN_LOADER = 'PluginLoader',
+  PLUGIN_INSTALL_MODAL = 'PluginInstallModal',
+}
+
+interface TranslationHelperProps {
+  trans_class: TranslationClass;
+  trans_text: string;
+  i18n_args?: {};
+  install_type?: number;
+}
+
+const logger = new Logger('TranslationHelper');
+
+const TranslationHelper: FC<TranslationHelperProps> = ({
+  trans_class,
+  trans_text,
+  i18n_args = null,
+  install_type = 0,
+}) => {
+  return (
+    <Translation>
+      {(t, {}) => {
+        switch (trans_class) {
+          case TranslationClass.PLUGIN_LOADER:
+            return i18n_args
+              ? t(TranslationClass.PLUGIN_LOADER + '.' + trans_text, i18n_args)
+              : t(TranslationClass.PLUGIN_LOADER + '.' + trans_text);
+          case TranslationClass.PLUGIN_INSTALL_MODAL:
+            switch (install_type) {
+              case InstallType.INSTALL:
+                return i18n_args
+                  ? t(TranslationClass.PLUGIN_INSTALL_MODAL + '.install.' + trans_text, i18n_args)
+                  : t(TranslationClass.PLUGIN_INSTALL_MODAL + '.install.' + trans_text);
+              case InstallType.REINSTALL:
+                return i18n_args
+                  ? t(TranslationClass.PLUGIN_INSTALL_MODAL + '.reinstall.' + trans_text, i18n_args)
+                  : t(TranslationClass.PLUGIN_INSTALL_MODAL + '.reinstall.' + trans_text);
+              case InstallType.UPDATE:
+                return i18n_args
+                  ? t(TranslationClass.PLUGIN_INSTALL_MODAL + '.update.' + trans_text, i18n_args)
+                  : t(TranslationClass.PLUGIN_INSTALL_MODAL + '.update.' + trans_text);
+            }
+          default:
+            logger.error('We should never fall in the default case!');
+            return '';
+        }
+      }}
+    </Translation>
+  );
+};
+
+export default TranslationHelper;
-- 
cgit v1.2.3

