feat(i18n): static type on i18n (#2225)

This commit is contained in:
Himself65
2023-05-04 00:35:09 -05:00
committed by GitHub
parent 66c3b09c67
commit 3d43e61087
80 changed files with 585 additions and 444 deletions

View File

@@ -1,22 +1,22 @@
import { Empty } from '@affine/component';
import { useTranslation } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import React from 'react';
export const PageListEmpty = (props: { listType?: string }) => {
const { listType } = props;
const { t } = useTranslation();
const t = useAFFiNEI18N();
const getEmptyDescription = () => {
if (listType === 'all') {
return t('emptyAllPages');
return t['emptyAllPages']();
}
if (listType === 'favorite') {
return t('emptyFavorite');
return t['emptyFavorite']();
}
if (listType === 'trash') {
return t('emptyTrash');
return t['emptyTrash']();
}
if (listType === 'shared') {
return t('emptySharedPages');
return t['emptySharedPages']();
}
};

View File

@@ -6,7 +6,7 @@ import {
MenuItem,
Tooltip,
} from '@affine/component';
import { useTranslation } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import {
DeletePermanentlyIcon,
FavoritedIcon,
@@ -44,7 +44,7 @@ export const OperationCell: React.FC<OperationCellProps> = ({
onToggleTrashPage,
}) => {
const { id, favorite, isPublic } = pageMeta;
const { t } = useTranslation();
const t = useAFFiNEI18N();
const [open, setOpen] = useState(false);
const [openDisableShared, setOpenDisableShared] = useState(false);
@@ -65,7 +65,7 @@ export const OperationCell: React.FC<OperationCellProps> = ({
onClick={() => {
onToggleFavoritePage(id);
toast(
favorite ? t('Removed from Favorites') : t('Added to Favorites')
favorite ? t['Removed from Favorites']() : t['Added to Favorites']()
);
}}
icon={
@@ -76,7 +76,7 @@ export const OperationCell: React.FC<OperationCellProps> = ({
)
}
>
{favorite ? t('Remove from favorites') : t('Add to Favorites')}
{favorite ? t['Remove from favorites']() : t['Add to Favorites']()}
</MenuItem>
{!environment.isDesktop && (
<MenuItem
@@ -85,7 +85,7 @@ export const OperationCell: React.FC<OperationCellProps> = ({
}}
icon={<OpenInNewIcon />}
>
{t('Open in new tab')}
{t['Open in new tab']()}
</MenuItem>
)}
{!pageMeta.isRootPinboard && (
@@ -117,7 +117,7 @@ export const OperationCell: React.FC<OperationCellProps> = ({
meta={pageMeta}
onConfirm={() => {
onToggleTrashPage(id, true);
toast(t('Deleted'));
toast(t['Moved to Trash']());
setOpen(false);
}}
onClose={() => {
@@ -151,22 +151,22 @@ export const TrashOperationCell: React.FC<TrashOperationCellProps> = ({
onRestorePage,
}) => {
const { id, title } = pageMeta;
const { t } = useTranslation();
const t = useAFFiNEI18N();
const [open, setOpen] = useState(false);
return (
<FlexWrapper>
<Tooltip content={t('Restore it')} placement="top-start">
<Tooltip content={t['Restore it']()} placement="top-start">
<IconButton
style={{ marginRight: '12px' }}
onClick={() => {
onRestorePage(id);
toast(t('restored', { title: title || 'Untitled' }));
toast(t['restored']({ title: title || 'Untitled' }));
}}
>
<ResetIcon />
</IconButton>
</Tooltip>
<Tooltip content={t('Delete permanently')} placement="top-start">
<Tooltip content={t['Delete permanently']()} placement="top-start">
<IconButton
onClick={() => {
setOpen(true);
@@ -176,14 +176,14 @@ export const TrashOperationCell: React.FC<TrashOperationCellProps> = ({
</IconButton>
</Tooltip>
<Confirm
title={t('Delete permanently?')}
content={t("Once deleted, you can't undo this action.")}
confirmText={t('Delete')}
title={t['Delete permanently?']()}
content={t['TrashButtonGroupDescription']()}
confirmText={t['Delete']()}
confirmType="danger"
open={open}
onConfirm={() => {
onPermanentlyDeletePage(id);
toast(t('Permanently deleted'));
toast(t['Permanently deleted']());
setOpen(false);
}}
onClose={() => {

View File

@@ -8,7 +8,7 @@ import {
TableRow,
Tooltip,
} from '@affine/component';
import { useTranslation } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import {
EdgelessIcon,
FavoritedIcon,
@@ -47,10 +47,10 @@ const FavoriteTag: React.FC<FavoriteTagProps> = ({
pageMeta: { favorite },
onClick,
}) => {
const { t } = useTranslation();
const t = useAFFiNEI18N();
return (
<Tooltip
content={favorite ? t('Favorited') : t('Favorite')}
content={favorite ? t['Favorited']() : t['Favorite']()}
placement="top-start"
>
<IconButton
@@ -59,7 +59,7 @@ const FavoriteTag: React.FC<FavoriteTagProps> = ({
e.stopPropagation();
onClick();
toast(
favorite ? t('Removed from Favorites') : t('Added to Favorites')
favorite ? t['Removed from Favorites']() : t['Added to Favorites']()
);
}}
style={{
@@ -106,7 +106,7 @@ export const PageList: React.FC<PageListProps> = ({
const helper = usePageMetaHelper(blockSuiteWorkspace);
const { removeToTrash, restoreFromTrash } =
useBlockSuiteMetaHelper(blockSuiteWorkspace);
const { t } = useTranslation();
const t = useAFFiNEI18N();
const theme = useTheme();
const matches = useMediaQuery(theme.breakpoints.up('sm'));
const isTrash = listType === 'trash';
@@ -130,14 +130,14 @@ export const PageList: React.FC<PageListProps> = ({
<TableRow>
{matches && (
<>
<TableCell proportion={0.5}>{t('Title')}</TableCell>
<TableCell proportion={0.2}>{t('Created')}</TableCell>
<TableCell proportion={0.5}>{t['Title']()}</TableCell>
<TableCell proportion={0.2}>{t['Created']()}</TableCell>
<TableCell proportion={0.2}>
{isTrash
? t('Moved to Trash')
? t['Moved to Trash']()
: isShared
? 'Shared'
: t('Updated')}
: t['Updated']()}
</TableCell>
<TableCell proportion={0.1}></TableCell>
</>
@@ -164,7 +164,7 @@ export const PageList: React.FC<PageListProps> = ({
<PageIcon />
)}
<Content ellipsis={true} color="inherit">
{pageMeta.title || t('Untitled')}
{pageMeta.title || t['Untitled']()}
</Content>
</StyledTitleLink>
{listType && !isTrash && (

View File

@@ -1,6 +1,6 @@
// fixme(himself65): refactor this file
import { FlexWrapper, IconButton, Menu, MenuItem } from '@affine/component';
import { useTranslation } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import {
EdgelessIcon,
FavoritedIcon,
@@ -57,7 +57,7 @@ const CommonMenu = () => {
);
};
const PageMenu = () => {
const { t } = useTranslation();
const t = useAFFiNEI18N();
// fixme(himself65): remove these hooks ASAP
const [workspace] = useCurrentWorkspace();
const [pageId] = useCurrentPageId();
@@ -83,7 +83,9 @@ const PageMenu = () => {
onClick={() => {
setPageMeta(pageId, { favorite: !favorite });
toast(
favorite ? t('Removed from Favorites') : t('Added to Favorites')
favorite
? t['Removed from Favorites']()
: t['Added to Favorites']()
);
}}
icon={
@@ -94,7 +96,7 @@ const PageMenu = () => {
)
}
>
{favorite ? t('Remove from favorites') : t('Add to Favorites')}
{favorite ? t['Remove from favorites']() : t['Add to Favorites']()}
</MenuItem>
<MenuItem
icon={mode === 'page' ? <EdgelessIcon /> : <PageIcon />}
@@ -106,8 +108,8 @@ const PageMenu = () => {
}));
}}
>
{t('Convert to ')}
{mode === 'page' ? t('Edgeless') : t('Page')}
{t['Convert to ']()}
{mode === 'page' ? t['Edgeless']() : t['Page']()}
</MenuItem>
<Export />
{!pageMeta.isRootPinboard && (
@@ -153,7 +155,7 @@ const PageMenu = () => {
meta={pageMeta}
onConfirm={() => {
removeToTrash(pageMeta.id);
toast(t('Moved to Trash'));
toast(t['Moved to Trash']());
}}
onCancel={() => {
setOpenConfirm(false);

View File

@@ -1,12 +1,12 @@
import { Button, displayFlex, Menu, MenuItem, styled } from '@affine/component';
import { LOCALES } from '@affine/i18n';
import { useTranslation } from '@affine/i18n';
import { useI18N } from '@affine/i18n';
import { ArrowDownSmallIcon, PublishIcon } from '@blocksuite/icons';
import type { FC, ReactElement } from 'react';
import { useCallback } from 'react';
const LanguageMenuContent: FC = () => {
const { i18n } = useTranslation();
const i18n = useI18N();
const changeLanguage = useCallback(
(event: string) => {
i18n.changeLanguage(event);
@@ -32,7 +32,7 @@ const LanguageMenuContent: FC = () => {
);
};
export const LanguageMenu: React.FC = () => {
const { i18n } = useTranslation();
const i18n = useI18N();
const currentLanguage = LOCALES.find(item => item.tag === i18n.language);

View File

@@ -1,6 +1,6 @@
import { displayFlex, IconButton, styled, Tooltip } from '@affine/component';
import { config } from '@affine/env';
import { useTranslation } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import {
getLoginStorage,
setLoginStorage,
@@ -79,7 +79,7 @@ export const SyncUser = () => {
const [open, setOpen] = useState(false);
const { t } = useTranslation();
const t = useAFFiNEI18N();
const transformWorkspace = useTransformWorkspace();
if (!config.enableLegacyCloud) {
@@ -89,7 +89,7 @@ export const SyncUser = () => {
if (status === 'offline') {
return (
<Tooltip
content={t('Please make sure you are online')}
content={t['Please make sure you are online']()}
placement="bottom-end"
>
<IconWrapper>
@@ -103,7 +103,7 @@ export const SyncUser = () => {
return (
<>
<Tooltip
content={t('Saved then enable AFFiNE Cloud')}
content={t['Saved then enable AFFiNE Cloud']()}
placement="bottom-end"
>
<IconButton
@@ -156,7 +156,7 @@ export const SyncUser = () => {
}
return (
<Tooltip content={t('Synced with AFFiNE Cloud')} placement="bottom-end">
<Tooltip content={t['Synced with AFFiNE Cloud']()} placement="bottom-end">
<IconWrapper>
<CloudWorkspaceIcon />
</IconWrapper>

View File

@@ -1,5 +1,5 @@
import { Button, Confirm } from '@affine/component';
import { useTranslation } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { assertExists } from '@blocksuite/store';
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
import { useRouter } from 'next/router';
@@ -20,7 +20,7 @@ export const TrashButtonGroup = () => {
meta => meta.id === pageId
);
assertExists(pageMeta);
const { t } = useTranslation();
const t = useAFFiNEI18N();
const router = useRouter();
const { restoreFromTrash } = useBlockSuiteMetaHelper(blockSuiteWorkspace);
@@ -36,7 +36,7 @@ export const TrashButtonGroup = () => {
restoreFromTrash(pageId);
}}
>
{t('Restore it')}
{t['Restore it']()}
</Button>
<Button
bold={true}
@@ -46,12 +46,12 @@ export const TrashButtonGroup = () => {
setOpen(true);
}}
>
{t('Delete permanently')}
{t['Delete permanently']()}
</Button>
<Confirm
title={t('TrashButtonGroupTitle')}
content={t('TrashButtonGroupDescription')}
confirmText={t('Delete')}
title={t['TrashButtonGroupTitle']()}
content={t['TrashButtonGroupDescription']()}
confirmText={t['Delete']()}
confirmType="danger"
open={open}
onConfirm={() => {

View File

@@ -1,5 +1,5 @@
import { appSidebarOpenAtom } from '@affine/component/app-sidebar';
import { useTranslation } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import { WorkspaceFlavour } from '@affine/workspace/type';
import { CloseIcon } from '@blocksuite/icons';
import type { Page } from '@blocksuite/store';
@@ -133,7 +133,7 @@ export const Header = forwardRef<
setShowWarning(shouldShowWarning());
}, []);
const [open] = useAtom(appSidebarOpenAtom);
const { t } = useTranslation();
const t = useAFFiNEI18N();
return (
<StyledHeaderContainer
@@ -156,7 +156,7 @@ export const Header = forwardRef<
<Suspense>
<SidebarSwitch
visible={!open}
tooltipContent={t('Expand sidebar')}
tooltipContent={t['Expand sidebar']()}
data-testid="sliderBar-arrowButton-expand"
/>
</Suspense>

View File

@@ -1,5 +1,6 @@
import { getEnvironment } from '@affine/env';
import { Trans, useTranslation } from '@affine/i18n';
import { Trans } from '@affine/i18n';
import { useAFFiNEI18N } from '@affine/i18n/hooks';
import type React from 'react';
import { useEffect, useState } from 'react';
@@ -23,7 +24,7 @@ export const shouldShowWarning = () => {
};
export const OSWarningMessage: React.FC = () => {
const { t } = useTranslation();
const t = useAFFiNEI18N();
const [notChrome, setNotChrome] = useState(false);
const [notGoodVersion, setNotGoodVersion] = useState(false);
useEffect(() => {
@@ -44,7 +45,7 @@ export const OSWarningMessage: React.FC = () => {
</span>
);
} else if (notGoodVersion) {
return <span>{t('upgradeBrowser')}</span>;
return <span>{t['upgradeBrowser']()}</span>;
}
return null;
};