server refactor: centrally load locale

To reduce code duplication, the locales are loaded in @/misc/i18n.ts
directly instead of importing it in each file using it separately.
This commit is contained in:
Johann150 2022-10-19 12:30:23 +02:00
parent 507dede6da
commit fbf7ea07c9
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
5 changed files with 14 additions and 31 deletions

View file

@ -1,19 +1,18 @@
export class I18n<T extends Record<string, any>> { const locales = import('../../../../locales/index.js').then(mod => mod.default);
public locale: T;
constructor(locale: T) { export class I18n {
this.locale = locale; public ts: Record<string, any>;
//#region BIND constructor(locale: string) {
this.ts = locales[locale];
this.t = this.t.bind(this); this.t = this.t.bind(this);
//#endregion
} }
// string にしているのは、ドット区切りでのパス指定を許可するため // string にしているのは、ドット区切りでのパス指定を許可するため
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも // なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
public t(key: string, args?: Record<string, any>): string { public t(key: string, args?: Record<string, any>): string {
try { 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) { if (args) {
for (const [k, v] of Object.entries(args)) { for (const [k, v] of Object.entries(args)) {

View file

@ -27,8 +27,6 @@ function compareOrigin(ctx: Koa.BaseContext): boolean {
return (normalizeUrl(referer) === normalizeUrl(config.url)); return (normalizeUrl(referer) === normalizeUrl(config.url));
} }
const locales = await import('../../../../../../locales/index.js').then(mod => mod.default);
// Init router // Init router
const router = new Router(); const router = new Router();
@ -50,8 +48,7 @@ router.get('/disconnect/discord', async ctx => {
}); });
const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const locale = locales[profile.lang || 'en-US']; const i18n = new I18n(profile.lang ?? 'en-US');
const i18n = new I18n(locale);
delete profile.integrations.discord; delete profile.integrations.discord;
@ -264,8 +261,7 @@ router.get('/dc/cb', async ctx => {
}); });
const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const locale = locales[profile.lang || 'en-US']; const i18n = new I18n(profile.lang ?? 'en-US');
const i18n = new I18n(locale);
await UserProfiles.update(user.id, { await UserProfiles.update(user.id, {
integrations: { integrations: {

View file

@ -27,8 +27,6 @@ function compareOrigin(ctx: Koa.BaseContext): boolean {
return (normalizeUrl(referer) === normalizeUrl(config.url)); return (normalizeUrl(referer) === normalizeUrl(config.url));
} }
const locales = await import('../../../../../../locales/index.js').then(mod => mod.default);
// Init router // Init router
const router = new Router(); const router = new Router();
@ -50,8 +48,7 @@ router.get('/disconnect/github', async ctx => {
}); });
const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const locale = locales[profile.lang || 'en-US']; const i18n = new I18n(profile.lang ?? 'en-US');
const i18n = new I18n(locale);
delete profile.integrations.github; delete profile.integrations.github;
@ -239,8 +236,7 @@ router.get('/gh/cb', async ctx => {
}); });
const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const locale = locales[profile.lang || 'en-US']; const i18n = new I18n(profile.lang ?? 'en-US');
const i18n = new I18n(locale);
await UserProfiles.update(user.id, { await UserProfiles.update(user.id, {
integrations: { integrations: {

View file

@ -26,8 +26,6 @@ function compareOrigin(ctx: Koa.BaseContext): boolean {
return (normalizeUrl(referer) === normalizeUrl(config.url)); return (normalizeUrl(referer) === normalizeUrl(config.url));
} }
const locales = await import('../../../../../../locales/index.js').then(mod => mod.default);
// Init router // Init router
const router = new Router(); const router = new Router();
@ -49,8 +47,7 @@ router.get('/disconnect/twitter', async ctx => {
}); });
const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const locale = locales[profile.lang || 'en-US']; const i18n = new I18n(profile.lang ?? 'en-US');
const i18n = new I18n(locale);
delete profile.integrations.twitter; delete profile.integrations.twitter;
@ -180,8 +177,7 @@ router.get('/tw/cb', async ctx => {
}); });
const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const locale = locales[profile.lang || 'en-US']; const i18n = new I18n(profile.lang ?? 'en-US');
const i18n = new I18n(locale);
await UserProfiles.update(user.id, { await UserProfiles.update(user.id, {
integrations: { integrations: {

View file

@ -3,8 +3,6 @@ import { User } from '@/models/entities/user.js';
import { I18n } from '@/misc/i18n.js'; import { I18n } from '@/misc/i18n.js';
import * as Acct from '@/misc/acct.js'; import * as Acct from '@/misc/acct.js';
import { sendEmail } from './send-email.js'; import { sendEmail } from './send-email.js';
// TODO
//const locales = await import('../../../../locales/index.js');
// TODO: locale ファイルをクライアント用とサーバー用で分けたい // TODO: locale ファイルをクライアント用とサーバー用で分けたい
@ -12,8 +10,7 @@ async function follow(userId: User['id'], follower: User) {
/* /*
const userProfile = await UserProfiles.findOneByOrFail({ userId: userId }); const userProfile = await UserProfiles.findOneByOrFail({ userId: userId });
if (!userProfile.email || !userProfile.emailNotificationTypes.includes('follow')) return; if (!userProfile.email || !userProfile.emailNotificationTypes.includes('follow')) return;
const locale = locales[userProfile.lang || 'en-US']; const i18n = new I18n(userProfile.lang ?? 'en-US');
const i18n = new I18n(locale);
// TODO: render user information html // TODO: render user information html
sendEmail(userProfile.email, i18n.t('_email._follow.title'), `${follower.name} (@${Acct.toString(follower)})`, `${follower.name} (@${Acct.toString(follower)})`); 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 }); const userProfile = await UserProfiles.findOneByOrFail({ userId: userId });
if (!userProfile.email || !userProfile.emailNotificationTypes.includes('receiveFollowRequest')) return; if (!userProfile.email || !userProfile.emailNotificationTypes.includes('receiveFollowRequest')) return;
const locale = locales[userProfile.lang || 'en-US']; const i18n = new I18n(userProfile.lang ?? 'en-US');
const i18n = new I18n(locale);
// TODO: render user information html // TODO: render user information html
sendEmail(userProfile.email, i18n.t('_email._receiveFollowRequest.title'), `${follower.name} (@${Acct.toString(follower)})`, `${follower.name} (@${Acct.toString(follower)})`); sendEmail(userProfile.email, i18n.t('_email._receiveFollowRequest.title'), `${follower.name} (@${Acct.toString(follower)})`, `${follower.name} (@${Acct.toString(follower)})`);
*/ */