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

feat(i18n): localize desktool components #5106

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions packages/sanity/src/core/components/RelativeTime.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import React from 'react'
import {type RelativeTimeOptions, useRelativeTime} from '../hooks/useRelativeTime'

/**
* @internal
*/
export interface RelativeTimeProps extends RelativeTimeOptions {
time: string | Date
}

/**
* @internal
*/
export function RelativeTime({time, ...options}: RelativeTimeProps) {
const timestamp = time instanceof Date ? time : new Date(time)
const timeAgo = useRelativeTime(timestamp, options)
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from './transitional'
export * from './userAvatar'
export * from './WithReferringDocuments'
export * from './zOffsets'
export * from './RelativeTime'
7 changes: 5 additions & 2 deletions packages/sanity/src/core/i18n/Translate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import React, {ComponentType, ReactNode, useMemo} from 'react'
import type {TFunction} from 'i18next'
import {CloseTagToken, simpleParser, TextToken, Token} from './simpleParser'

type ComponentMap = Record<string, ComponentType<{children?: ReactNode}>>
type ComponentMap = Record<
string,
ComponentType<{children?: ReactNode}> | keyof JSX.IntrinsicElements
>

/**
* @beta
Expand All @@ -11,7 +14,7 @@ export interface TranslationProps {
t: TFunction
i18nKey: string
components: ComponentMap
values?: Record<string, string | string[]>
values?: Record<string, number | string | string[]>
}

function render(tokens: Token[], componentMap: ComponentMap): ReactNode {
Expand Down
16 changes: 12 additions & 4 deletions packages/sanity/src/desk/components/DocTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {SanityDocumentLike} from '@sanity/types'
import React from 'react'
import {useSchema, unstable_useValuePreview as useValuePreview} from 'sanity'
import {deskLocaleNamespace} from '../i18n'
import {useSchema, useTranslation, unstable_useValuePreview as useValuePreview} from 'sanity'

export interface DocTitleProps {
document: SanityDocumentLike
Expand All @@ -10,19 +11,26 @@ export function DocTitle(props: DocTitleProps) {
const {document: documentValue} = props
const schema = useSchema()
const schemaType = schema.get(documentValue._type)
const {t} = useTranslation(deskLocaleNamespace)

const {error, value} = useValuePreview({
schemaType: schemaType!,
value: documentValue,
})

if (!schemaType) {
return <code>Unknown schema type: {documentValue._type}</code>
return <code>{t('doc-title.unknown-schema-type.text', {schemaType: documentValue._type})}</code>
}

if (error) {
return <>Error: {error.message}</>
return <>{t('doc-title.error.text', {errorMessage: error.message})}</>
}

return <>{value?.title || <span style={{color: 'var(--card-muted-fg-color)'}}>Untitled</span>}</>
return (
<>
{value?.title || (
<span style={{color: 'var(--card-muted-fg-color)'}}>{t('doc-title.fallback.text')}</span>
)}
</>
)
}
17 changes: 13 additions & 4 deletions packages/sanity/src/desk/components/DraftStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import React from 'react'
import {EditIcon} from '@sanity/icons'
import {PreviewValue, SanityDocument} from '@sanity/types'
import {Box, Text, Tooltip} from '@sanity/ui'
import {TimeAgo} from './TimeAgo'
import {TextWithTone} from 'sanity'
import {deskLocaleNamespace} from '../i18n'
import {TextWithTone, Translate, useTranslation, RelativeTime} from 'sanity'

export function DraftStatus(props: {document?: PreviewValue | Partial<SanityDocument> | null}) {
const {document} = props
const updatedAt = document && '_updatedAt' in document && document._updatedAt
const {t} = useTranslation(deskLocaleNamespace)

return (
<Tooltip
Expand All @@ -16,9 +17,17 @@ export function DraftStatus(props: {document?: PreviewValue | Partial<SanityDocu
<Box padding={2}>
<Text size={1}>
{document ? (
<>Edited {updatedAt && <TimeAgo time={updatedAt} />}</>
<Translate
i18nKey="pane-item.draft-status.has-draft.tooltip"
t={t}
components={{
RelativeTime: () => (
<>{updatedAt && <RelativeTime time={updatedAt} useTemporalPhrase />}</>
),
}}
/>
) : (
<>No unpublished edits</>
t('pane-item.draft-status.no-draft.tooltip')
)}
</Text>
</Box>
Expand Down

This file was deleted.

42 changes: 26 additions & 16 deletions packages/sanity/src/desk/components/MissingSchemaType.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import {WarningOutlineIcon} from '@sanity/icons'
import {SanityDocument} from '@sanity/types'
import React from 'react'
import {GeneralPreviewLayoutKey, SanityDefaultPreview} from 'sanity'
import {deskLocaleNamespace} from '../i18n'
import {GeneralPreviewLayoutKey, SanityDefaultPreview, Translate, useTranslation} from 'sanity'

export interface MissingSchemaTypeProps {
layout?: GeneralPreviewLayoutKey
value: SanityDocument
}

const getUnknownTypeFallback = (id: string, typeName: string) => ({
title: (
<em>
No schema found for type <code>{typeName}</code>
</em>
),
subtitle: (
<em>
Document: <code>{id}</code>
</em>
),
media: () => <WarningOutlineIcon />,
})

export function MissingSchemaType(props: MissingSchemaTypeProps) {
const {t} = useTranslation(deskLocaleNamespace)
const {layout, value} = props

return (
<SanityDefaultPreview {...getUnknownTypeFallback(value._id, value._type)} layout={layout} />
<SanityDefaultPreview
title={
<em>
<Translate
t={t}
i18nKey="pane-item.missing-schema-type.title"
components={{Code: 'code'}}
values={{documentType: value._type}}
/>
</em>
}
subtitle={
<Translate
t={t}
i18nKey="pane-item.missing-schema-type.subtitle"
components={{Code: 'code'}}
values={{documentId: value._id}}
/>
}
// eslint-disable-next-line react/jsx-no-bind
media={() => <WarningOutlineIcon />}
layout={layout}
/>
)
}
13 changes: 0 additions & 13 deletions packages/sanity/src/desk/components/NotPublishedStatus.tsx

This file was deleted.

17 changes: 13 additions & 4 deletions packages/sanity/src/desk/components/PublishedStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import React from 'react'
import {PublishIcon} from '@sanity/icons'
import {PreviewValue, SanityDocument} from '@sanity/types'
import {Box, Text, Tooltip} from '@sanity/ui'
import {TimeAgo} from './TimeAgo'
import {TextWithTone} from 'sanity'
import {deskLocaleNamespace} from '../i18n'
import {RelativeTime, TextWithTone, Translate, useTranslation} from 'sanity'

export function PublishedStatus(props: {document?: PreviewValue | Partial<SanityDocument> | null}) {
const {document} = props
const updatedAt = document && '_updatedAt' in document && document._updatedAt
const statusLabel = document ? 'Published' : 'Not published'
const {t} = useTranslation(deskLocaleNamespace)

return (
<Tooltip
Expand All @@ -17,9 +18,17 @@ export function PublishedStatus(props: {document?: PreviewValue | Partial<Sanity
<Box padding={2}>
<Text size={1}>
{document ? (
<>Published {updatedAt && <TimeAgo time={updatedAt} />}</>
<Translate
t={t}
i18nKey="pane-item.published-status.has-published.tooltip"
components={{
RelativeTime: () => (
<>{updatedAt && <RelativeTime time={updatedAt} useTemporalPhrase />}</>
),
}}
/>
) : (
<>Not published</>
t('pane-item.published-status.no-published.tooltip')
)}
</Text>
</Box>
Expand Down
73 changes: 0 additions & 73 deletions packages/sanity/src/desk/components/ReferringDocumentsList.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions packages/sanity/src/desk/components/TimeAgo.tsx

This file was deleted.

Loading
Loading