Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sw): use fully typed locales #14470

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/backend/src/models/json-schema/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { ACHIEVEMENT_TYPES } from '@/core/AchievementService.js';
import { notificationTypes } from '@/types.js';

const baseSchema = {
Expand Down Expand Up @@ -294,6 +295,7 @@ export const packedNotificationSchema = {
achievement: {
type: 'string',
optional: false, nullable: false,
enum: ACHIEVEMENT_TYPES,
},
},
}, {
Expand Down
49 changes: 0 additions & 49 deletions packages/frontend/src/scripts/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export class I18n<T extends ILocale> {
return this.tsxCache = new Proxy(this.locale, new Handler()) as unknown as Tsx<T>;
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (this.tsxCache) {
return this.tsxCache;
}
Expand Down Expand Up @@ -244,51 +243,3 @@ export class I18n<T extends ILocale> {
return str;
}
}

if (import.meta.vitest) {
const { describe, expect, it } = import.meta.vitest;

describe('i18n', () => {
it('t', () => {
const i18n = new I18n({
foo: 'foo',
bar: {
baz: 'baz',
qux: 'qux {0}' as unknown as ParameterizedString<'0'>,
quux: 'quux {0} {1}' as unknown as ParameterizedString<'0' | '1'>,
},
});

expect(i18n.t('foo')).toBe('foo');
expect(i18n.t('bar.baz')).toBe('baz');
expect(i18n.tsx.bar.qux({ 0: 'hoge' })).toBe('qux hoge');
expect(i18n.tsx.bar.quux({ 0: 'hoge', 1: 'fuga' })).toBe('quux hoge fuga');
});
it('ts', () => {
const i18n = new I18n({
foo: 'foo',
bar: {
baz: 'baz',
qux: 'qux {0}' as unknown as ParameterizedString<'0'>,
quux: 'quux {0} {1}' as unknown as ParameterizedString<'0' | '1'>,
},
});

expect(i18n.ts.foo).toBe('foo');
expect(i18n.ts.bar.baz).toBe('baz');
});
it('tsx', () => {
const i18n = new I18n({
foo: 'foo',
bar: {
baz: 'baz',
qux: 'qux {0}' as unknown as ParameterizedString<'0'>,
quux: 'quux {0} {1}' as unknown as ParameterizedString<'0' | '1'>,
},
});

expect(i18n.tsx.bar.qux({ 0: 'hoge' })).toBe('qux hoge');
expect(i18n.tsx.bar.quux({ 0: 'hoge', 1: 'fuga' })).toBe('quux hoge fuga');
});
});
}
52 changes: 52 additions & 0 deletions packages/frontend/test/i18n.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { describe, expect, it } from 'vitest';
import { I18n } from '@/scripts/i18n.js';
import { ParameterizedString } from '../../../locales/index.js';

describe('i18n', () => {
it('t', () => {
const i18n = new I18n({
foo: 'foo',
bar: {
baz: 'baz',
qux: 'qux {0}' as unknown as ParameterizedString<'0'>,
quux: 'quux {0} {1}' as unknown as ParameterizedString<'0' | '1'>,
},
});

expect(i18n.t('foo')).toBe('foo');
expect(i18n.t('bar.baz')).toBe('baz');
expect(i18n.tsx.bar.qux({ 0: 'hoge' })).toBe('qux hoge');
expect(i18n.tsx.bar.quux({ 0: 'hoge', 1: 'fuga' })).toBe('quux hoge fuga');
});
it('ts', () => {
const i18n = new I18n({
foo: 'foo',
bar: {
baz: 'baz',
qux: 'qux {0}' as unknown as ParameterizedString<'0'>,
quux: 'quux {0} {1}' as unknown as ParameterizedString<'0' | '1'>,
},
});

expect(i18n.ts.foo).toBe('foo');
expect(i18n.ts.bar.baz).toBe('baz');
});
it('tsx', () => {
const i18n = new I18n({
foo: 'foo',
bar: {
baz: 'baz',
qux: 'qux {0}' as unknown as ParameterizedString<'0'>,
quux: 'quux {0} {1}' as unknown as ParameterizedString<'0' | '1'>,
},
});

expect(i18n.tsx.bar.qux({ 0: 'hoge' })).toBe('qux hoge');
expect(i18n.tsx.bar.quux({ 0: 'hoge', 1: 'fuga' })).toBe('quux hoge fuga');
});
});
7 changes: 4 additions & 3 deletions packages/misskey-js/src/autogen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4245,15 +4245,16 @@ export type components = {
/** @enum {string} */
type: 'roleAssigned';
role: components['schemas']['Role'];
} | {
} | ({
/** Format: id */
id: string;
/** Format: date-time */
createdAt: string;
/** @enum {string} */
type: 'achievementEarned';
achievement: string;
} | {
/** @enum {string} */
achievement: 'notes1' | 'notes10' | 'notes100' | 'notes500' | 'notes1000' | 'notes5000' | 'notes10000' | 'notes20000' | 'notes30000' | 'notes40000' | 'notes50000' | 'notes60000' | 'notes70000' | 'notes80000' | 'notes90000' | 'notes100000' | 'login3' | 'login7' | 'login15' | 'login30' | 'login60' | 'login100' | 'login200' | 'login300' | 'login400' | 'login500' | 'login600' | 'login700' | 'login800' | 'login900' | 'login1000' | 'passedSinceAccountCreated1' | 'passedSinceAccountCreated2' | 'passedSinceAccountCreated3' | 'loggedInOnBirthday' | 'loggedInOnNewYearsDay' | 'noteClipped1' | 'noteFavorited1' | 'myNoteFavorited1' | 'profileFilled' | 'markedAsCat' | 'following1' | 'following10' | 'following50' | 'following100' | 'following300' | 'followers1' | 'followers10' | 'followers50' | 'followers100' | 'followers300' | 'followers500' | 'followers1000' | 'collectAchievements30' | 'viewAchievements3min' | 'iLoveMisskey' | 'foundTreasure' | 'client30min' | 'client60min' | 'noteDeletedWithin1min' | 'postedAtLateNight' | 'postedAt0min0sec' | 'selfQuote' | 'htl20npm' | 'viewInstanceChart' | 'outputHelloWorldOnScratchpad' | 'open3windows' | 'driveFolderCircularReference' | 'reactWithoutRead' | 'clickedClickHere' | 'justPlainLucky' | 'setNameToSyuilo' | 'cookieClicked' | 'brainDiver' | 'smashTestNotificationButton' | 'tutorialCompleted' | 'bubbleGameExplodingHead' | 'bubbleGameDoubleExplodingHead';
}) | {
/** Format: id */
id: string;
/** Format: date-time */
Expand Down
50 changes: 24 additions & 26 deletions packages/sw/src/scripts/create-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ export async function createNotification<K extends keyof PushNotificationDataMap

async function composeNotification(data: PushNotificationDataMap[keyof PushNotificationDataMap]): Promise<[string, NotificationOptions] | null> {
const i18n = await (swLang.i18n ?? swLang.fetchLocale());
const { t } = i18n;
switch (data.type) {
/*
case 'driveFileCreated': // TODO (Server Side)
return [t('_notification.fileUploaded'), {
return [i18n.ts._notification.fileUploaded, {
body: body.name,
icon: body.url,
data
Expand All @@ -58,50 +57,50 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
const account = await getAccountFromId(data.userId);
if (!account) return null;
const userDetail = await cli.request('users/show', { userId: data.body.userId }, account.token);
return [t('_notification.youWereFollowed'), {
return [i18n.ts._notification.youWereFollowed, {
body: getUserName(data.body.user),
icon: data.body.user.avatarUrl,
badge: iconUrl('user-plus'),
data,
actions: userDetail.isFollowing ? [] : [
{
action: 'follow',
title: t('_notification._actions.followBack'),
title: i18n.ts._notification._actions.followBack,
},
],
}];
}

case 'mention':
return [t('_notification.youGotMention', { name: getUserName(data.body.user) }), {
return [i18n.tsx._notification.youGotMention({ name: getUserName(data.body.user) }), {
body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl,
badge: iconUrl('at'),
data,
actions: [
{
action: 'reply',
title: t('_notification._actions.reply'),
title: i18n.ts._notification._actions.reply,
},
],
}];

case 'reply':
return [t('_notification.youGotReply', { name: getUserName(data.body.user) }), {
return [i18n.tsx._notification.youGotReply({ name: getUserName(data.body.user) }), {
body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl,
badge: iconUrl('arrow-back-up'),
data,
actions: [
{
action: 'reply',
title: t('_notification._actions.reply'),
title: i18n.ts._notification._actions.reply,
},
],
}];

case 'renote':
return [t('_notification.youRenoted', { name: getUserName(data.body.user) }), {
return [i18n.tsx._notification.youRenoted({ name: getUserName(data.body.user) }), {
body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl,
badge: iconUrl('repeat'),
Expand All @@ -115,27 +114,27 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
}];

case 'quote':
return [t('_notification.youGotQuote', { name: getUserName(data.body.user) }), {
return [i18n.tsx._notification.youGotQuote({ name: getUserName(data.body.user) }), {
body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl,
badge: iconUrl('quote'),
data,
actions: [
{
action: 'reply',
title: t('_notification._actions.reply'),
title: i18n.ts._notification._actions.reply,
},
...((data.body.note.visibility === 'public' || data.body.note.visibility === 'home') ? [
{
action: 'renote',
title: t('_notification._actions.renote'),
title: i18n.ts._notification._actions.renote,
},
] : []),
],
}];

case 'note':
return [t('_notification.newNote') + ': ' + getUserName(data.body.user), {
return [i18n.ts._notification.newNote + ': ' + getUserName(data.body.user), {
body: data.body.note.text ?? '',
icon: data.body.user.avatarUrl,
data,
Expand Down Expand Up @@ -178,41 +177,41 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
}

case 'receiveFollowRequest':
return [t('_notification.youReceivedFollowRequest'), {
return [i18n.ts._notification.youReceivedFollowRequest, {
body: getUserName(data.body.user),
icon: data.body.user.avatarUrl,
badge: iconUrl('user-plus'),
data,
actions: [
{
action: 'accept',
title: t('accept'),
title: i18n.ts.accept,
},
{
action: 'reject',
title: t('reject'),
title: i18n.ts.reject,
},
],
}];

case 'followRequestAccepted':
return [t('_notification.yourFollowRequestAccepted'), {
return [i18n.ts._notification.yourFollowRequestAccepted, {
body: getUserName(data.body.user),
icon: data.body.user.avatarUrl,
badge: iconUrl('circle-check'),
data,
}];

case 'achievementEarned':
return [t('_notification.achievementEarned'), {
body: t(`_achievements._types._${data.body.achievement}.title`),
return [i18n.ts._notification.achievementEarned, {
body: i18n.ts._achievements._types[`_${data.body.achievement}`].title,
badge: iconUrl('medal'),
data,
tag: `achievement:${data.body.achievement}`,
}];

case 'pollEnded':
return [t('_notification.pollEnded'), {
return [i18n.ts._notification.pollEnded, {
body: data.body.note.text ?? '',
badge: iconUrl('chart-arrows'),
data,
Expand All @@ -226,8 +225,8 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
}];

case 'test':
return [t('_notification.testNotification'), {
body: t('_notification.notificationWillBeDisplayedLikeThis'),
return [i18n.ts._notification.testNotification, {
body: i18n.ts._notification.notificationWillBeDisplayedLikeThis,
badge: iconUrl('bell'),
data,
}];
Expand All @@ -236,7 +235,7 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
return null;
}
case 'unreadAntennaNote':
return [t('_notification.unreadAntennaNote', { name: data.body.antenna.name }), {
return [i18n.tsx._notification.unreadAntennaNote({ name: data.body.antenna.name }), {
body: `${getUserName(data.body.note.user)}: ${data.body.note.text ?? ''}`,
icon: data.body.note.user.avatarUrl,
badge: iconUrl('antenna'),
Expand All @@ -252,7 +251,6 @@ async function composeNotification(data: PushNotificationDataMap[keyof PushNotif
export async function createEmptyNotification(): Promise<void> {
return new Promise<void>(async res => {
const i18n = await (swLang.i18n ?? swLang.fetchLocale());
const { t } = i18n;

await globalThis.registration.showNotification(
(new URL(origin)).host,
Expand All @@ -264,11 +262,11 @@ export async function createEmptyNotification(): Promise<void> {
actions: [
{
action: 'markAllAsRead',
title: t('markAllAsRead'),
title: i18n.ts.markAllAsRead,
},
{
action: 'settings',
title: t('notificationSettings'),
title: i18n.ts.notificationSettings,
},
],
data: {},
Expand Down
37 changes: 0 additions & 37 deletions packages/sw/src/scripts/i18n.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/sw/src/scripts/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
* Language manager for SW
*/
import { get, set } from 'idb-keyval';
import { I18n, type Locale } from '@/scripts/i18n.js';
import { I18n } from '../../../frontend/src/scripts/i18n.js';
import type { Locale } from '../../../../locales/index.js';

class SwLang {
public cacheName = `mk-cache-${_VERSION_}`;
Expand All @@ -23,7 +24,7 @@ class SwLang {
return this.fetchLocale();
}

public i18n: Promise<I18n> | null = null;
public i18n: Promise<I18n<Locale>> | null = null;

public fetchLocale(): Promise<I18n<Locale>> {
return (this.i18n = this._fetch());
Expand Down
Loading
Loading