From 731d8129f3f9b4724d0bd52bdec649fe85ca656e Mon Sep 17 00:00:00 2001 From: Jens Kuerschner Date: Sun, 12 Jan 2025 11:30:55 +0100 Subject: [PATCH 1/2] Enable custom default lang subject and default languages other than English --- README.md | 7 +++++-- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 28 +++++++++++++++++++++------- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ba284a5..4484ef0 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,12 @@ pnpm install directus-extension-system-email-i18n
-## How it works +## How to use * The extension is a hook, which filters all emails sent by Directus. * When we are dealing with a system email, it would check for the user's language. -* If this language is not English, it would look for an email template with a language suffix and a subject in a respective environment variable `I18N_EMAIL_SUBJECTS`. +* If this language is not the default language, it would look for an email template with a language suffix. +* Additionally (in all cases), it looks for a subject in a respective environment variable `I18N_EMAIL_SUBJECTS`.
@@ -86,6 +87,8 @@ If you would now add Spanish: 1. add a template `password-reset-es` 2. adjust the env var to something like `{"de":{"password-reset": "Passwort zurücksetzen"}, "es":{"password-reset": "Restablecer contraseña"}}` +**Bonus:** You could still also add an alternative English block to the env var in order to change the default subject. +
--- diff --git a/package-lock.json b/package-lock.json index 2e90a24..3ffc0a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "directus-extension-system-email-i18n", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "directus-extension-system-email-i18n", - "version": "1.0.0", + "version": "1.1.0", "license": "MIT", "dependencies": { "ip": "^2.0.1" diff --git a/package.json b/package.json index 63b9aea..f8db905 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "directus-extension-system-email-i18n", - "version": "1.0.0", + "version": "1.1.0", "description": "Directus CMS extension to translate system emails (password reset, user registration and invitation)", "author": { "email": "code@jekuer.com", diff --git a/src/index.ts b/src/index.ts index 075e364..ef39900 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,13 +8,24 @@ export default defineHook(({ filter }, { services, getSchema, env }) => { filter('email.send', async (input: any) => { if (['password-reset', 'user-invitation', 'user-registration'].includes(input.template.name)) { - // pull in any subject translation form the environment variable I18N_EMAIL_SUBJECTS (scheme: {"de": {"password-reset": "Passwort zurücksetzen", ...}, ...}) - const i18nEmailSubjects = JSON.parse(env.I18N_EMAIL_SUBJECTS); + // take IP from the server this script is running on const systemIp = ip.address(); + + // pull in any subject translation form the environment variable I18N_EMAIL_SUBJECTS (scheme: {"de": {"password-reset": "Passwort zurücksetzen", ...}, ...}) + // TODO: Could also become a setting somewhere in the Directus app as an alternative to the environment variable + const i18nEmailSubjects = JSON.parse(env.I18N_EMAIL_SUBJECTS); + // preparing the accountability object to be able searching directus users with admin rights // this includes a lot of empty props, which is necessary to match the type of the accountability object const adminAccountability:Accountability = { role: null, roles: [], user: null, admin: true, app: false, ip: systemIp, origin: 'user language lookup by extension' }; + + // get the default language + const settings = new ItemsService('directus_settings', { schema: await getSchema(), accountability: adminAccountability }); + const settingsResponse = await settings.readSingleton({ + fields: ['default_language'], + }); + const defaultLang = settingsResponse.default_language?.split('-')[0] || 'en'; // get the language from searching and reading the user const users = new ItemsService('directus_users', { schema: await getSchema(), accountability: adminAccountability }); const response = await users.readByQuery({ @@ -26,12 +37,15 @@ export default defineHook(({ filter }, { services, getSchema, env }) => { fields: ['language'], limit: 1 }); - const lang = response[0].language?.split('-')[0] ? response[0].language.split('-')[0] : 'en'; - // override for non-English languages - if (lang !== 'en') { - input.subject = i18nEmailSubjects[lang][input.template.name] || input.subject; + const lang = response[0].language?.split('-')[0] ? response[0].language.split('-')[0] : defaultLang; + + // override the subject with the translation from the environment variable (if available) + input.subject = i18nEmailSubjects[lang][input.template.name] || input.subject; + // override template for non-default languages + if (lang !== defaultLang) { input.template.name = input.template.name + '-' + lang; - } + } + } return input From 21fe31ae36790502940610186f7e794e1b0137b6 Mon Sep 17 00:00:00 2001 From: Jens Kuerschner Date: Sun, 12 Jan 2025 11:37:33 +0100 Subject: [PATCH 2/2] fix --- src/index.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index ef39900..a40fc1b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,14 +7,16 @@ export default defineHook(({ filter }, { services, getSchema, env }) => { filter('email.send', async (input: any) => { - if (['password-reset', 'user-invitation', 'user-registration'].includes(input.template.name)) { + const templateName = input.template.name; + + if (['password-reset', 'user-invitation', 'user-registration'].includes(templateName)) { // take IP from the server this script is running on const systemIp = ip.address(); // pull in any subject translation form the environment variable I18N_EMAIL_SUBJECTS (scheme: {"de": {"password-reset": "Passwort zurücksetzen", ...}, ...}) // TODO: Could also become a setting somewhere in the Directus app as an alternative to the environment variable - const i18nEmailSubjects = JSON.parse(env.I18N_EMAIL_SUBJECTS); + const i18nEmailSubjects = JSON.parse(env.I18N_EMAIL_SUBJECTS) || {}; // preparing the accountability object to be able searching directus users with admin rights // this includes a lot of empty props, which is necessary to match the type of the accountability object @@ -40,10 +42,10 @@ export default defineHook(({ filter }, { services, getSchema, env }) => { const lang = response[0].language?.split('-')[0] ? response[0].language.split('-')[0] : defaultLang; // override the subject with the translation from the environment variable (if available) - input.subject = i18nEmailSubjects[lang][input.template.name] || input.subject; + input.subject = i18nEmailSubjects[lang] && i18nEmailSubjects[lang][templateName] ? i18nEmailSubjects[lang][templateName] : input.subject; // override template for non-default languages if (lang !== defaultLang) { - input.template.name = input.template.name + '-' + lang; + input.template.name = templateName + '-' + lang; } }