From e507b1b8882eea3e406869650df0f1af5f1d3f39 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Tue, 13 Sep 2022 13:12:42 -0400 Subject: [PATCH] backend: localize strings for service integrations Currently only the 'connected' and 'disconnected' strings are translated. --- locales/en-US.yml | 10 ++++++++++ locales/ja-JP.yml | 10 ++++++++++ .../backend/src/server/api/service/discord.ts | 15 +++++++++++++-- packages/backend/src/server/api/service/github.ts | 14 ++++++++++++-- .../backend/src/server/api/service/twitter.ts | 14 ++++++++++++-- packages/backend/tsconfig.json | 4 ++-- 6 files changed, 59 insertions(+), 8 deletions(-) diff --git a/locales/en-US.yml b/locales/en-US.yml index 3187acba8..83a8485b4 100644 --- a/locales/en-US.yml +++ b/locales/en-US.yml @@ -1409,3 +1409,13 @@ _deck: list: "List" mentions: "Mentions" direct: "Direct notes" +_services: + _discord: + connected: "Discord: @{username}#{discriminator} connected to FoundKey: @{mkUsername}!" + disconnected: "Discord linkage has been removed :v:" + _twitter: + connected: "Twitter: @{twitterUserName} connected to FoundKey: @{userName}!" + disconnected: "Twitter linkage has been removed :v:" + _github: + connected: "GitHub: @{login} connected to FoundKey: @{userName}!" + disconnected: "GitHub linkage has been removed :v:" diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml index 4f37a67e7..8a6d40d5d 100644 --- a/locales/ja-JP.yml +++ b/locales/ja-JP.yml @@ -1452,3 +1452,13 @@ _deck: list: "リスト" mentions: "あなた宛て" direct: "ダイレクト" +_services: + _discord: + connected: "Discord: @{username}#{discriminator} を、FoundKey: @{mkUsername} に接続しました!" + disconnected: "Discordの連携を解除しました :v:" + _twitter: + connected: "Twitter: @{twitterUserName} を、FoundKey: @{userName} に接続しました!" + disconnected: "Twitterの連携を解除しました :v:" + _github: + connected: "GitHub: @{login} を、FoundKey: @{userName} に接続しました!" + disconnected: "GitHubの連携を解除しました :v:" diff --git a/packages/backend/src/server/api/service/discord.ts b/packages/backend/src/server/api/service/discord.ts index 9d0cc47f4..2f6e9fb01 100644 --- a/packages/backend/src/server/api/service/discord.ts +++ b/packages/backend/src/server/api/service/discord.ts @@ -10,6 +10,7 @@ import { fetchMeta } from '@/misc/fetch-meta.js'; import { Users, UserProfiles } from '@/models/index.js'; import { ILocalUser } from '@/models/entities/user.js'; import { redisClient } from '@/db/redis.js'; +import { I18n } from '@/misc/i18n.js'; import signin from '../common/signin.js'; function getUserToken(ctx: Koa.BaseContext): string | null { @@ -26,6 +27,8 @@ 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(); @@ -47,6 +50,8 @@ 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); delete profile.integrations.discord; @@ -54,7 +59,7 @@ router.get('/disconnect/discord', async ctx => { integrations: profile.integrations, }); - ctx.body = 'Discordの連携を解除しました :v:'; + ctx.body = i18n.t('_services._discord.disconnected'); // Publish i updated event publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, { @@ -259,6 +264,8 @@ 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); await UserProfiles.update(user.id, { integrations: { @@ -274,7 +281,11 @@ router.get('/dc/cb', async ctx => { }, }); - ctx.body = `Discord: @${username}#${discriminator} connected to FoundKey: @${user.username}!`; + ctx.body = i18n.t('_services._discord.connected', { + username, + discriminator, + mkUsername: user.username, + }); // Publish i updated event publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, { diff --git a/packages/backend/src/server/api/service/github.ts b/packages/backend/src/server/api/service/github.ts index 640bbf82c..da4716bc3 100644 --- a/packages/backend/src/server/api/service/github.ts +++ b/packages/backend/src/server/api/service/github.ts @@ -11,6 +11,7 @@ import { Users, UserProfiles } from '@/models/index.js'; import { ILocalUser } from '@/models/entities/user.js'; import { redisClient } from '@/db/redis.js'; import signin from '../common/signin.js'; +import { I18n } from '@/misc/i18n.js'; function getUserToken(ctx: Koa.BaseContext): string | null { return ((ctx.headers['cookie'] || '').match(/igi=(\w+)/) || [null, null])[1]; @@ -26,6 +27,8 @@ 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(); @@ -47,6 +50,8 @@ 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); delete profile.integrations.github; @@ -54,7 +59,7 @@ router.get('/disconnect/github', async ctx => { integrations: profile.integrations, }); - ctx.body = 'GitHubの連携を解除しました :v:'; + ctx.body = i18n.t('_services._github.disconnected'); // Publish i updated event publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, { @@ -234,6 +239,8 @@ 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); await UserProfiles.update(user.id, { integrations: { @@ -246,7 +253,10 @@ router.get('/gh/cb', async ctx => { }, }); - ctx.body = `GitHub: @${login} connected to FoundKey: @${user.username}!`; + ctx.body = i18n.t('_services._github.connected', { + login, + userName: user.username, + }); // Publish i updated event publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, { diff --git a/packages/backend/src/server/api/service/twitter.ts b/packages/backend/src/server/api/service/twitter.ts index 5594206f1..309bd922e 100644 --- a/packages/backend/src/server/api/service/twitter.ts +++ b/packages/backend/src/server/api/service/twitter.ts @@ -10,6 +10,7 @@ import { Users, UserProfiles } from '@/models/index.js'; import { ILocalUser } from '@/models/entities/user.js'; import { redisClient } from '@/db/redis.js'; import signin from '../common/signin.js'; +import { I18n } from '@/misc/i18n.js'; function getUserToken(ctx: Koa.BaseContext): string | null { return ((ctx.headers['cookie'] || '').match(/igi=(\w+)/) || [null, null])[1]; @@ -25,6 +26,8 @@ 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(); @@ -46,6 +49,8 @@ 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); delete profile.integrations.twitter; @@ -53,7 +58,7 @@ router.get('/disconnect/twitter', async ctx => { integrations: profile.integrations, }); - ctx.body = 'Twitter linkage has been removed :v:'; + ctx.body = i18n.t('_services._twitter.disconnected'); // Publish i updated event publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, { @@ -175,6 +180,8 @@ 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); await UserProfiles.update(user.id, { integrations: { @@ -188,7 +195,10 @@ router.get('/tw/cb', async ctx => { }, }); - ctx.body = `Twitter: @${result.screenName} connected to FoundKey: @${user.username}!`; + ctx.body = i18n.t('_services._twitter.connected', { + twitterUserName: result.screenName, + userName: user.username, + }); // Publish i updated event publishMainStream(user.id, 'meUpdated', await Users.pack(user, user, { diff --git a/packages/backend/tsconfig.json b/packages/backend/tsconfig.json index 0702607e3..6e17dad7a 100644 --- a/packages/backend/tsconfig.json +++ b/packages/backend/tsconfig.json @@ -10,8 +10,8 @@ "noFallthroughCasesInSwitch": true, "declaration": false, "sourceMap": false, - "target": "es2017", - "module": "es2020", + "target": "ES2021", + "module": "esnext", "moduleResolution": "node", "allowSyntheticDefaultImports": true, "removeComments": false,