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>> {
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<string, any>;
//#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, any>): 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)) {

View file

@ -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: {

View file

@ -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: {

View file

@ -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: {

View file

@ -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)})`);
*/