Skip to content

Commit

Permalink
Merge pull request #1 from jekuer/dev
Browse files Browse the repository at this point in the history
Enable custom default lang subject and default languages other than E…
  • Loading branch information
jekuer authored Jan 12, 2025
2 parents b383ea7 + 21fe31a commit ffc5ff2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ pnpm install directus-extension-system-email-i18n

<br />

## 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`.

<br />

Expand Down Expand Up @@ -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.

<br />

---
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
Expand Down
34 changes: 25 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@ 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);
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) || {};

// 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({
Expand All @@ -26,12 +39,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;
input.template.name = input.template.name + '-' + lang;
}
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] && i18nEmailSubjects[lang][templateName] ? i18nEmailSubjects[lang][templateName] : input.subject;
// override template for non-default languages
if (lang !== defaultLang) {
input.template.name = templateName + '-' + lang;
}

}

return input
Expand Down

0 comments on commit ffc5ff2

Please sign in to comment.