Skip to content

Commit b003237

Browse files
committed
feat: added support for lastLocaleField
1 parent ee5de9e commit b003237

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ class Email {
9797
// pass a transport configuration object or a transport instance
9898
// (e.g. an instance is created via `nodemailer.createTransport`)
9999
// <https://nodemailer.com/transports/>
100-
transport: {}
100+
transport: {},
101+
// last locale field name (also used by @ladjs/i18n)
102+
lastLocaleField: 'last_locale'
101103
},
102104
config
103105
);
@@ -179,12 +181,24 @@ class Email {
179181
);
180182

181183
if (_.isObject(this.config.i18n)) {
184+
if (
185+
this.config.i18n.lastLocaleField &&
186+
this.config.lastLocaleField &&
187+
this.config.i18n.lastLocaleField !== this.config.lastLocaleField
188+
)
189+
throw new Error(
190+
`The 'lastLocaleField' (String) option for @ladjs/i18n and email-templates do not match, i18n value was ${this.config.i18n.lastLocaleField} and email-templates value was ${this.config.lastLocaleField}`
191+
);
192+
182193
const i18n = new I18N({ ...this.config.i18n, register: locals });
183194

184-
// support `locals.user.last_locale`
195+
// support `locals.user.last_locale` (variable based name lastLocaleField)
185196
// (e.g. for <https://lad.js.org>)
186-
if (_.isObject(locals.user) && _.isString(locals.user.last_locale))
187-
locals.locale = locals.user.last_locale;
197+
if (
198+
_.isObject(locals.user) &&
199+
_.isString(locals.user[this.config.lastLocaleField])
200+
)
201+
locals.locale = locals.user[this.config.lastLocaleField];
188202

189203
if (_.isString(locals.locale)) i18n.setLocale(locals.locale);
190204
}

0 commit comments

Comments
 (0)