Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Localized Templates): ability to register user and set language,… #22452

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 27 additions & 4 deletions api/src/services/mail/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type EmailOptions = SendMailOptions & {
template?: {
name: string;
data: Record<string, any>;
language?: string | null | undefined;
};
};

Expand Down Expand Up @@ -68,7 +69,7 @@ export class MailService {
...templateData,
};

html = await this.renderTemplate(template.name, templateData);
html = await this.renderTemplate(template.name, templateData, template.language);
}

if (typeof html === 'string') {
Expand All @@ -83,13 +84,35 @@ export class MailService {
return info;
}

private async renderTemplate(template: string, variables: Record<string, any>) {
private async getFirstExistingPath(paths: (string | null)[]): Promise<string | null> {
for (const path of paths) {
if (!path) {
continue;
}

if ((await fse.pathExists(path)) === true) {
return path;
}
}

return null;
}

private async renderTemplate(template: string, variables: Record<string, any>, language?: string | undefined | null) {
const localizedTemplatePath = language
? path.resolve(env['EMAIL_TEMPLATES_PATH'] as string, template + `.${language}.liquid`)
: null;
Comment on lines +102 to +104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation would mean that we would have to manually translate every single email template into every single language that we support in raw HTML. Imagine then needing to update a small thing like the styling - we would have to go through each template individually and update them all for all languages.

It'd be better if we could reuse our existing community translation integration so everyone benefits from our diverse community and would enable us to change styles/layouts without touching every single language. :)

That has been, amongst other things, mentioned yesterday here and another approach here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for response.
I understand that yet it might happen that for different languages especially for right to left you would like to have a but different views.

So with such solution we can have both, what you propose and different templates for different languages.

Also consider situation where for some languages you might want to have different colors of buttons or so.

Proposed implementation is not conflicting with your idea, just gives additional possibilities.

This comment was marked as duplicate.


const customTemplatePath = path.resolve(env['EMAIL_TEMPLATES_PATH'] as string, template + '.liquid');
const systemTemplatePath = path.join(__dirname, 'templates', template + '.liquid');

const templatePath = (await fse.pathExists(customTemplatePath)) ? customTemplatePath : systemTemplatePath;
const templatePath = await this.getFirstExistingPath([
localizedTemplatePath,
customTemplatePath,
systemTemplatePath,
]);

if ((await fse.pathExists(templatePath)) === false) {
if (!templatePath) {
throw new InvalidPayloadError({ reason: `Template "${template}" doesn't exist` });
}

Expand Down
9 changes: 7 additions & 2 deletions api/src/services/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ export class UsersService extends ItemsService {
*/
private async getUserByEmail(
email: string,
): Promise<{ id: string; role: string; status: string; password: string; email: string } | undefined> {
): Promise<
{ id: string; role: string; status: string; password: string; email: string; language: string | null } | undefined
> {
return await this.knex
.select('id', 'role', 'status', 'password', 'email')
.select('id', 'role', 'status', 'password', 'email', 'language')
.from('directus_users')
.whereRaw(`LOWER(??) = ?`, ['email', email.toLowerCase()])
.first();
Expand Down Expand Up @@ -416,6 +418,7 @@ export class UsersService extends ItemsService {
subject: subjectLine,
template: {
name: 'user-invitation',
language: user?.language,
data: {
url: this.inviteUrl(user?.email ?? email, url),
email: user?.email ?? email,
Expand Down Expand Up @@ -524,6 +527,7 @@ export class UsersService extends ItemsService {
subject: 'Verify your email address', // TODO: translate after theres support for internationalized emails
template: {
name: 'user-registration',
language: input.language,
data: {
url: verificationURL.toString(),
email: input.email,
Expand Down Expand Up @@ -595,6 +599,7 @@ export class UsersService extends ItemsService {
subject: subjectLine,
template: {
name: 'password-reset',
language: user.language,
data: {
url: acceptURL,
email: user.email,
Expand Down
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@
- the-other-dev
- magiudev
- burka
- rbochenski
1 change: 1 addition & 0 deletions packages/types/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ export type RegisterUserInput = {
password: NonNullable<User['password']>;
first_name?: User['first_name'];
last_name?: User['last_name'];
language?: User['language'];
};