mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
feat: i18n for emails (#1442)
## Description Support setting a document language that will control the language used for sending emails to recipients. Additional work has been done to convert all emails to using our i18n implementation so we can later add controls for sending other kinds of emails in a users target language. ## Related Issue N/A ## Changes Made - Added `<Trans>` and `msg` macros to emails - Introduced a new `renderEmailWithI18N` utility in the lib package - Updated all emails to use the `<Tailwind>` component at the top level due to rendering constraints - Updated the `i18n.server.tsx` file to not use a top level await ## Testing Performed - Configured document language and verified emails were sent in the expected language - Created a document from a template and verified that the templates language was transferred to the document
This commit is contained in:
@ -7,12 +7,17 @@ import { setupI18n } from '@lingui/core';
|
||||
import { setI18n } from '@lingui/react/server';
|
||||
|
||||
import { IS_APP_WEB } from '../../constants/app';
|
||||
import { SUPPORTED_LANGUAGE_CODES } from '../../constants/i18n';
|
||||
import {
|
||||
APP_I18N_OPTIONS,
|
||||
SUPPORTED_LANGUAGE_CODES,
|
||||
isValidLanguageCode,
|
||||
} from '../../constants/i18n';
|
||||
import { extractLocaleData } from '../../utils/i18n';
|
||||
import { remember } from '../../utils/remember';
|
||||
|
||||
type SupportedLanguages = (typeof SUPPORTED_LANGUAGE_CODES)[number];
|
||||
|
||||
async function loadCatalog(lang: SupportedLanguages): Promise<{
|
||||
export async function loadCatalog(lang: SupportedLanguages): Promise<{
|
||||
[k: string]: Messages;
|
||||
}> {
|
||||
const extension = process.env.NODE_ENV === 'development' ? 'po' : 'js';
|
||||
@ -20,14 +25,12 @@ async function loadCatalog(lang: SupportedLanguages): Promise<{
|
||||
|
||||
let { messages } = await import(`../../translations/${lang}/${context}.${extension}`);
|
||||
|
||||
// Dirty way to load common messages for development since it's not compiled.
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
const commonMessages = await import(`../../translations/${lang}/common.${extension}`);
|
||||
if (extension === 'po') {
|
||||
const { messages: commonMessages } = await import(
|
||||
`../../translations/${lang}/common.${extension}`
|
||||
);
|
||||
|
||||
messages = {
|
||||
...messages,
|
||||
...commonMessages.messages,
|
||||
};
|
||||
messages = { ...messages, ...commonMessages };
|
||||
}
|
||||
|
||||
return {
|
||||
@ -35,40 +38,63 @@ async function loadCatalog(lang: SupportedLanguages): Promise<{
|
||||
};
|
||||
}
|
||||
|
||||
const catalogs = await Promise.all(SUPPORTED_LANGUAGE_CODES.map(loadCatalog));
|
||||
const catalogs = Promise.all(SUPPORTED_LANGUAGE_CODES.map(loadCatalog));
|
||||
|
||||
// transform array of catalogs into a single object
|
||||
export const allMessages = catalogs.reduce((acc, oneCatalog) => {
|
||||
return { ...acc, ...oneCatalog };
|
||||
}, {});
|
||||
const allMessages = async () => {
|
||||
return await catalogs.then((catalogs) =>
|
||||
catalogs.reduce((acc, oneCatalog) => {
|
||||
return {
|
||||
...acc,
|
||||
...oneCatalog,
|
||||
};
|
||||
}, {}),
|
||||
);
|
||||
};
|
||||
|
||||
type AllI18nInstances = { [K in SupportedLanguages]: I18n };
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
export const allI18nInstances = SUPPORTED_LANGUAGE_CODES.reduce((acc, lang) => {
|
||||
const messages = allMessages[lang] ?? {};
|
||||
export const allI18nInstances = remember('i18n.allI18nInstances', async () => {
|
||||
const loadedMessages = await allMessages();
|
||||
|
||||
const i18n = setupI18n({
|
||||
locale: lang,
|
||||
messages: { [lang]: messages },
|
||||
});
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
||||
return SUPPORTED_LANGUAGE_CODES.reduce((acc, lang) => {
|
||||
const messages = loadedMessages[lang] ?? {};
|
||||
|
||||
return { ...acc, [lang]: i18n };
|
||||
}, {}) as AllI18nInstances;
|
||||
const i18n = setupI18n({
|
||||
locale: lang,
|
||||
messages: { [lang]: messages },
|
||||
});
|
||||
|
||||
return { ...acc, [lang]: i18n };
|
||||
}, {}) as AllI18nInstances;
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export const getI18nInstance = async (lang?: SupportedLanguages | (string & {})) => {
|
||||
const instances = await allI18nInstances;
|
||||
|
||||
if (!isValidLanguageCode(lang)) {
|
||||
return instances[APP_I18N_OPTIONS.sourceLang];
|
||||
}
|
||||
|
||||
return instances[lang] ?? instances[APP_I18N_OPTIONS.sourceLang];
|
||||
};
|
||||
|
||||
/**
|
||||
* This needs to be run in all layouts and page server components that require i18n.
|
||||
*
|
||||
* https://lingui.dev/tutorials/react-rsc#pages-layouts-and-lingui
|
||||
*/
|
||||
export const setupI18nSSR = () => {
|
||||
export const setupI18nSSR = async () => {
|
||||
const { lang, locales } = extractLocaleData({
|
||||
cookies: cookies(),
|
||||
headers: headers(),
|
||||
});
|
||||
|
||||
// Get and set a ready-made i18n instance for the given language.
|
||||
const i18n = allI18nInstances[lang];
|
||||
const i18n = await getI18nInstance(lang);
|
||||
|
||||
// Reactivate the i18n instance with the locale for date and number formatting.
|
||||
i18n.activate(lang, locales);
|
||||
|
||||
Reference in New Issue
Block a user