diff --git a/packages/backend/src/misc/i18n.ts b/packages/backend/src/misc/i18n.ts index 4fa398763..25f059bb6 100644 --- a/packages/backend/src/misc/i18n.ts +++ b/packages/backend/src/misc/i18n.ts @@ -1,19 +1,18 @@ -export class I18n> { - public locale: T; +const locales = import('../../../../locales/index.js').then(mod => mod.default); - constructor(locale: T) { - this.locale = locale; +export class I18n { + public ts: Record; - //#region BIND + constructor(locale: string) { + this.ts = locales[locale]; this.t = this.t.bind(this); - //#endregion } // string にしているのは、ドット区切りでのパス指定を許可するため // なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも public t(key: string, args?: Record): string { try { - let str = key.split('.').reduce((o, i) => o[i], this.locale) as string; + let str = key.split('.').reduce((o, i) => o[i], this.ts) as string; if (args) { for (const [k, v] of Object.entries(args)) { diff --git a/packages/backend/src/server/api/service/discord.ts b/packages/backend/src/server/api/service/discord.ts index 2f6e9fb01..c9b8e03b9 100644 --- a/packages/backend/src/server/api/service/discord.ts +++ b/packages/backend/src/server/api/service/discord.ts @@ -27,8 +27,6 @@ function compareOrigin(ctx: Koa.BaseContext): boolean { return (normalizeUrl(referer) === normalizeUrl(config.url)); } -const locales = await import('../../../../../../locales/index.js').then(mod => mod.default); - // Init router const router = new Router(); @@ -50,8 +48,7 @@ router.get('/disconnect/discord', async ctx => { }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); - const locale = locales[profile.lang || 'en-US']; - const i18n = new I18n(locale); + const i18n = new I18n(profile.lang ?? 'en-US'); delete profile.integrations.discord; @@ -264,8 +261,7 @@ router.get('/dc/cb', async ctx => { }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); - const locale = locales[profile.lang || 'en-US']; - const i18n = new I18n(locale); + const i18n = new I18n(profile.lang ?? 'en-US'); await UserProfiles.update(user.id, { integrations: { diff --git a/packages/backend/src/server/api/service/github.ts b/packages/backend/src/server/api/service/github.ts index da4716bc3..5e78130e7 100644 --- a/packages/backend/src/server/api/service/github.ts +++ b/packages/backend/src/server/api/service/github.ts @@ -27,8 +27,6 @@ function compareOrigin(ctx: Koa.BaseContext): boolean { return (normalizeUrl(referer) === normalizeUrl(config.url)); } -const locales = await import('../../../../../../locales/index.js').then(mod => mod.default); - // Init router const router = new Router(); @@ -50,8 +48,7 @@ router.get('/disconnect/github', async ctx => { }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); - const locale = locales[profile.lang || 'en-US']; - const i18n = new I18n(locale); + const i18n = new I18n(profile.lang ?? 'en-US'); delete profile.integrations.github; @@ -239,8 +236,7 @@ router.get('/gh/cb', async ctx => { }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); - const locale = locales[profile.lang || 'en-US']; - const i18n = new I18n(locale); + const i18n = new I18n(profile.lang ?? 'en-US'); await UserProfiles.update(user.id, { integrations: { diff --git a/packages/backend/src/server/api/service/twitter.ts b/packages/backend/src/server/api/service/twitter.ts index 309bd922e..59da0f6ba 100644 --- a/packages/backend/src/server/api/service/twitter.ts +++ b/packages/backend/src/server/api/service/twitter.ts @@ -26,8 +26,6 @@ function compareOrigin(ctx: Koa.BaseContext): boolean { return (normalizeUrl(referer) === normalizeUrl(config.url)); } -const locales = await import('../../../../../../locales/index.js').then(mod => mod.default); - // Init router const router = new Router(); @@ -49,8 +47,7 @@ router.get('/disconnect/twitter', async ctx => { }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); - const locale = locales[profile.lang || 'en-US']; - const i18n = new I18n(locale); + const i18n = new I18n(profile.lang ?? 'en-US'); delete profile.integrations.twitter; @@ -180,8 +177,7 @@ router.get('/tw/cb', async ctx => { }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); - const locale = locales[profile.lang || 'en-US']; - const i18n = new I18n(locale); + const i18n = new I18n(profile.lang ?? 'en-US'); await UserProfiles.update(user.id, { integrations: { diff --git a/packages/backend/src/services/send-email-notification.ts b/packages/backend/src/services/send-email-notification.ts index 740eacfe6..cd3c670ac 100644 --- a/packages/backend/src/services/send-email-notification.ts +++ b/packages/backend/src/services/send-email-notification.ts @@ -3,8 +3,6 @@ import { User } from '@/models/entities/user.js'; import { I18n } from '@/misc/i18n.js'; import * as Acct from '@/misc/acct.js'; import { sendEmail } from './send-email.js'; -// TODO -//const locales = await import('../../../../locales/index.js'); // TODO: locale ファイルをクライアント用とサーバー用で分けたい @@ -12,8 +10,7 @@ async function follow(userId: User['id'], follower: User) { /* const userProfile = await UserProfiles.findOneByOrFail({ userId: userId }); if (!userProfile.email || !userProfile.emailNotificationTypes.includes('follow')) return; - const locale = locales[userProfile.lang || 'en-US']; - const i18n = new I18n(locale); + const i18n = new I18n(userProfile.lang ?? 'en-US'); // TODO: render user information html sendEmail(userProfile.email, i18n.t('_email._follow.title'), `${follower.name} (@${Acct.toString(follower)})`, `${follower.name} (@${Acct.toString(follower)})`); */ @@ -23,8 +20,7 @@ async function receiveFollowRequest(userId: User['id'], follower: User) { /* const userProfile = await UserProfiles.findOneByOrFail({ userId: userId }); if (!userProfile.email || !userProfile.emailNotificationTypes.includes('receiveFollowRequest')) return; - const locale = locales[userProfile.lang || 'en-US']; - const i18n = new I18n(locale); + const i18n = new I18n(userProfile.lang ?? 'en-US'); // TODO: render user information html sendEmail(userProfile.email, i18n.t('_email._receiveFollowRequest.title'), `${follower.name} (@${Acct.toString(follower)})`, `${follower.name} (@${Acct.toString(follower)})`); */