Skip to content

Commit

Permalink
feat(i18n): convert document actions strings to use i18n primitives (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored and rexxars committed Nov 17, 2023
1 parent 9e6eab0 commit 5a4fc06
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 70 deletions.
98 changes: 86 additions & 12 deletions dev/test-studio/plugins/locale-no-nb/bundles/desk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type {DeskLocaleResourceKeys} from 'sanity/desk'

const deskResources: Record<DeskLocaleResourceKeys, string> = {
/** Label for the "Publish" document action when there are pending changes.*/
/** --- PUBLISH ACTION --- */
/** Tooltip when action is disabled because the studio is not ready.*/
'action.publish.disabled.not-ready': 'Operasjonen er ikke klar',

/** Label for action when there are pending changes.*/
'action.publish.draft.label': 'Publiser',

/** Label for the "Publish" document action while publish is being executed.*/
Expand All @@ -11,34 +15,104 @@ const deskResources: Record<DeskLocaleResourceKeys, string> = {
'action.publish.published.label': 'Publisert',

/** Label for the "Publish" document action when the document has live edit enabled.*/
'action.publish.liveEdit.label': 'Publiser',
'action.publish.live-edit.label': 'Publiser',

/** Tooltip for the "Publish" document action when the document has live edit enabled.*/
'action.publish.liveEdit.tooltip':
'action.publish.live-edit.tooltip':
'"Live Edit" er skrudd på for denne dokumenttypen og publisering skjer automatisk når du gjør endringer',

/** Fallback tooltip for the "Publish" document action when publish is invoked for a document with live edit enabled.*/
'action.publish.liveEdit.publishDisabled':
'Kan ikke publsere fordi liveEdit er skrudd på for denne dokumenttypen.',
'action.publish.live-edit.publish-disabled':
'Kan ikke publisere fordi "Live Edit" er skrudd på for denne dokumenttypen.',

/** Tooltip when the "Publish" document action is disabled due to validation issues */
'action.publish.validationIssues.tooltip':
'action.publish.validation-issues.tooltip':
'Valideringsfeil som må rettes før dokumentet kan publiseres',

/** Tooltip when publish button is disabled because the document is already published.*/
'action.publish.alreadyPublished.tooltip': 'Publisert for {{timeSincePublished}} siden',
'action.publish.already-published.tooltip': 'Publisert for {{timeSincePublished}} siden',

/** Tooltip when publish button is disabled because the document is already published, and published time is unavailable.*/
'action.publish.alreadyPublished.noTimeAgo.tooltip': 'Allerede publisert',
'action.publish.already-published.no-time-ago.tooltip': 'Allerede publisert',

/** Tooltip when publish button is disabled because there are no changes.*/
'action.publish.tooltip.noChanges': 'Ingen upubliserte endringer',

/** Tooltip when publish button is disabled because the studio is not ready.*/
'action.publish.disabled.notReady': 'Operasjonen er ikke klar',
'action.publish.tooltip.no-changes': 'Ingen upubliserte endringer',

/** Tooltip when publish button is waiting for validation and async tasks to complete.*/
'action.publish.waiting': 'Venter på at andre oppgaver skal fullføre',

/** --- DELETE ACTION --- **/
/** Tooltip when action button is disabled because the operation is not ready */
'action.delete.disabled.not-ready': 'Operasjonen er ikke klar',

/** Tooltip when action button is disabled because the document does not exist */
'action.delete.disabled.nothing-to-delete':
'Dette dokumentet eksisterer ikke eller har allerede blitt slettet',

/** Label for the "Delete" document action button */
'action.delete.label': 'Slett',

/** Label for the "Delete" document action while the document is being deleted */
'action.delete.running.label': 'Sletter…',

/** --- DISCARD CHANGES ACTION --- **/
/** Tooltip when action button is disabled because the operation is not ready */
'action.discard-changes.disabled.not-ready': 'Operasjonen er ikke klar',

/** Label for the "Discard changes" document action */
'action.discard-changes.label': 'Forkast endringer',

/** Tooltip when action is disabled because the document has no unpublished changes */
'action.discard-changes.disabled.no-change': 'Dette dokumentet har ingen endringer',

/** Tooltip when action is disabled because the document is not published */
'action.discard-changes.disabled.not-published': 'Dette dokumentet er ikke publisert',

/** Message prompting the user to confirm discarding changes */
'action.discard-changes.confirm-dialog.confirm-discard-changes':
'Er du sikker på at du vil forkaste alle endringer siden forrige gang dette dokumentet ble publisert?',

/** --- DUPLICATE ACTION --- */
/** Tooltip when action is disabled because the operation is not ready */
'action.duplicate.disabled.not-ready': 'Operasjonen er ikke klar',

/** Tooltip when action is disabled because the document doesn't exist */
'action.duplicate.disabled.nothing-to-duplicate':
'Dette dokumentet er tomt og kan ikke dupliseres',

/** Label for the "Duplicate" document action */
'action.duplicate.label': 'Duplisèr',

/** Label for the "Duplicate" document action while the document is being duplicated */
'action.duplicate.running.label': 'Duplisèrer…',

/** --- UNPUBLISH ACTION --- */
/** Tooltip when action is disabled because the operation is not ready */
'action.unpublish.disabled.not-ready': 'Operasjonen er ikke klar',

/** Label for the "Unpublish" document action */
'action.unpublish.label': 'Avpubliser',

/** Tooltip when action is disabled because the document is not already published */
'action.unpublish.disabled.not-published': 'Dette dokumentet er ikke publisert',

/** Fallback tooltip for the Unpublish document action when publish is invoked for a document with live edit enabled.*/
'action.unpublish.live-edit.disabled':
'Dette dokumentet har "Live Edit" skrudd på og kan ikke avpubliseres',

/** --- RESTORE ACTION --- */
/** Label for the "Restore" document action */
'action.restore.label': 'Gjenopprett',

/** Fallback tooltip for when user is looking at the initial version */
'action.restore.disabled.cannot-restore-initial': 'Kan ikke gjenopprette til første version',

/** Default tooltip for the action */
'action.restore.tooltip': 'Gjenopprett til denne versjonen',

/** Message prompting the user to confirm that they want to restore to an earlier version*/
'action.restore.confirm-dialog.confirm-discard-changes':
'Er du sikker på at du vil gjenopprette til valgte versjon?',
}

export default deskResources
20 changes: 11 additions & 9 deletions packages/sanity/src/desk/documentActions/DeleteAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import {TrashIcon} from '@sanity/icons'
import React, {useCallback, useState} from 'react'
import {ConfirmDeleteDialog} from '../components'
import {useDocumentPane} from '../panes/document/useDocumentPane'
import {deskLocaleNamespace} from '../i18n'
import {
DocumentActionComponent,
InsufficientPermissionsMessage,
useCurrentUser,
useDocumentOperation,
useDocumentPairPermissions,
useTranslation,
} from 'sanity'
import {useDocumentPane} from '../panes/document/useDocumentPane'

const DISABLED_REASON_TITLE = {
NOTHING_TO_DELETE: 'This document doesn’t yet exist or is already deleted',
const DISABLED_REASON_TITLE_KEY = {
NOTHING_TO_DELETE: 'action.delete.disabled.nothingToDelete',
NOT_READY: 'action.delete.disabled.notReady',
}

/** @internal */
Expand All @@ -23,6 +26,8 @@ export const DeleteAction: DocumentActionComponent = ({id, type, draft, onComple
const [isDeleting, setIsDeleting] = useState(false)
const [isConfirmDialogOpen, setConfirmDialogOpen] = useState(false)

const {t} = useTranslation(deskLocaleNamespace)

const handleCancel = useCallback(() => {
setConfirmDialogOpen(false)
onComplete()
Expand Down Expand Up @@ -53,7 +58,7 @@ export const DeleteAction: DocumentActionComponent = ({id, type, draft, onComple
tone: 'critical',
icon: TrashIcon,
disabled: true,
label: 'Delete',
label: t('action.delete.label'),
title: (
<InsufficientPermissionsMessage
operationLabel="delete this document"
Expand All @@ -67,11 +72,8 @@ export const DeleteAction: DocumentActionComponent = ({id, type, draft, onComple
tone: 'critical',
icon: TrashIcon,
disabled: isDeleting || Boolean(deleteOp.disabled) || isPermissionsLoading,
title:
(deleteOp.disabled &&
DISABLED_REASON_TITLE[deleteOp.disabled as keyof typeof DISABLED_REASON_TITLE]) ||
'',
label: isDeleting ? 'Deleting…' : 'Delete',
title: (deleteOp.disabled && t(DISABLED_REASON_TITLE_KEY[deleteOp.disabled])) || '',
label: isDeleting ? t('action.delete.running.label') : t('action.delete.label'),
shortcut: 'Ctrl+Alt+D',
onHandle: handle,
dialog: isConfirmDialogOpen && {
Expand Down
26 changes: 14 additions & 12 deletions packages/sanity/src/desk/documentActions/DiscardChangesAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import {ResetIcon} from '@sanity/icons'
import React, {useCallback, useMemo, useState} from 'react'
import {deskLocaleNamespace} from '../i18n'
import {
DocumentActionComponent,
DocumentActionDialogProps,
InsufficientPermissionsMessage,
useCurrentUser,
useDocumentOperation,
useDocumentPairPermissions,
useTranslation,
} from 'sanity'

const DISABLED_REASON_TITLE = {
NO_CHANGES: 'This document has no unpublished changes',
NOT_PUBLISHED: 'This document is not published',
}
const DISABLED_REASON_KEY = {
NO_CHANGES: 'action.discardChanges.disabled.noChanges',
NOT_PUBLISHED: 'action.discardChanges.disabled.notPublished',
NOT_READY: 'action.discardChanges.disabled.notReady',
} as const

/** @internal */
export const DiscardChangesAction: DocumentActionComponent = ({
Expand All @@ -33,6 +36,8 @@ export const DiscardChangesAction: DocumentActionComponent = ({
})
const currentUser = useCurrentUser()

const {t} = useTranslation(deskLocaleNamespace)

const handleConfirm = useCallback(() => {
discardChanges.execute()
onComplete()
Expand All @@ -49,9 +54,9 @@ export const DiscardChangesAction: DocumentActionComponent = ({
tone: 'critical',
onCancel: onComplete,
onConfirm: handleConfirm,
message: <>Are you sure you want to discard all changes since last published?</>,
message: t('action.discardChanges.confirmDialog.confirm-discard-changes'),
},
[handleConfirm, isConfirmDialogOpen, onComplete],
[handleConfirm, isConfirmDialogOpen, onComplete, t],
)

if (!published || liveEdit) {
Expand All @@ -63,7 +68,7 @@ export const DiscardChangesAction: DocumentActionComponent = ({
tone: 'critical',
icon: ResetIcon,
disabled: true,
label: 'Discard changes',
label: t('action.discardChanges.label'),
title: (
<InsufficientPermissionsMessage
operationLabel="discard changes in this document"
Expand All @@ -77,11 +82,8 @@ export const DiscardChangesAction: DocumentActionComponent = ({
tone: 'critical',
icon: ResetIcon,
disabled: Boolean(discardChanges.disabled) || isPermissionsLoading,
title:
(discardChanges.disabled &&
DISABLED_REASON_TITLE[discardChanges.disabled as keyof typeof DISABLED_REASON_TITLE]) ||
'',
label: 'Discard changes',
title: (discardChanges.disabled && DISABLED_REASON_KEY[discardChanges.disabled]) || '',
label: t('action.discardChanges.label'),
onHandle: handle,
dialog,
}
Expand Down
18 changes: 10 additions & 8 deletions packages/sanity/src/desk/documentActions/DuplicateAction.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import {CopyIcon} from '@sanity/icons'
import {uuid} from '@sanity/uuid'
import React, {useCallback, useState} from 'react'
import {deskLocaleNamespace} from '../i18n'
import {useRouter} from 'sanity/router'
import {
DocumentActionComponent,
InsufficientPermissionsMessage,
useDocumentPairPermissions,
useDocumentOperation,
useCurrentUser,
useTranslation,
} from 'sanity'

const DISABLED_REASON_TITLE = {
NOTHING_TO_DUPLICATE: 'This document doesn’t yet exist so there‘s nothing to duplicate',
const DISABLED_REASON_KEY = {
NOTHING_TO_DUPLICATE: 'action.duplicate.disabled.nothingToDuplicate',
NOT_READY: 'action.duplicate.disabled.notReady',
}

/** @internal */
Expand All @@ -25,6 +28,8 @@ export const DuplicateAction: DocumentActionComponent = ({id, type, onComplete})
permission: 'duplicate',
})

const {t} = useTranslation(deskLocaleNamespace)

const currentUser = useCurrentUser()

const handle = useCallback(() => {
Expand All @@ -40,7 +45,7 @@ export const DuplicateAction: DocumentActionComponent = ({id, type, onComplete})
return {
icon: CopyIcon,
disabled: true,
label: 'Duplicate',
label: t('action.duplicate.label'),
title: (
<InsufficientPermissionsMessage
operationLabel="duplicate this document"
Expand All @@ -53,11 +58,8 @@ export const DuplicateAction: DocumentActionComponent = ({id, type, onComplete})
return {
icon: CopyIcon,
disabled: isDuplicating || Boolean(duplicate.disabled) || isPermissionsLoading,
label: isDuplicating ? 'Duplicating…' : 'Duplicate',
title:
(duplicate.disabled &&
DISABLED_REASON_TITLE[duplicate.disabled as keyof typeof DISABLED_REASON_TITLE]) ||
'',
label: isDuplicating ? t('action.duplicate.running.label') : t('action.duplicate.label'),
title: duplicate.disabled ? t(DISABLED_REASON_KEY[duplicate.disabled]) : '',
onHandle: handle,
}
}
Expand Down
17 changes: 11 additions & 6 deletions packages/sanity/src/desk/documentActions/HistoryRestoreAction.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {RestoreIcon} from '@sanity/icons'
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'
import {deskLocaleNamespace} from '../i18n'
import {
DocumentActionComponent,
DocumentActionDialogProps,
useDocumentOperation,
useDocumentOperationEvent,
useTranslation,
} from 'sanity'
import {useRouter} from 'sanity/router'

Expand All @@ -15,6 +17,7 @@ export const HistoryRestoreAction: DocumentActionComponent = ({id, type, revisio
const {navigateIntent} = useRouter()
const prevEvent = useRef(event)
const [isConfirmDialogOpen, setConfirmDialogOpen] = useState(false)
const {t} = useTranslation(deskLocaleNamespace)

const handleConfirm = useCallback(() => {
restore.execute(revision!)
Expand Down Expand Up @@ -45,12 +48,12 @@ export const HistoryRestoreAction: DocumentActionComponent = ({id, type, revisio
tone: 'critical',
onCancel: onComplete,
onConfirm: handleConfirm,
message: <>Are you sure you want to restore this document?</>,
message: t('action.restore.confirm.message'),
}
}

return null
}, [handleConfirm, isConfirmDialogOpen, onComplete])
}, [handleConfirm, isConfirmDialogOpen, onComplete, t])

const isRevisionInitialVersion = revision === '@initial'
const isRevisionLatestVersion = revision === undefined // undefined means latest version
Expand All @@ -60,12 +63,14 @@ export const HistoryRestoreAction: DocumentActionComponent = ({id, type, revisio
}

return {
label: 'Restore',
label: t('action.restore.label'),
color: 'primary',
onHandle: handle,
title: isRevisionInitialVersion
? "You can't restore to the initial version"
: 'Restore to this version',
title: t(
isRevisionInitialVersion
? 'action.restore.disabled.cannot-restore-initial'
: 'action.restore.tooltip',
),
icon: RestoreIcon,
dialog,
disabled: isRevisionInitialVersion,
Expand Down
12 changes: 6 additions & 6 deletions packages/sanity/src/desk/documentActions/PublishAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import {
} from 'sanity'

const DISABLED_REASON_TITLE_KEY: Record<string, DeskLocaleResourceKeys> = {
LIVE_EDIT_ENABLED: 'action.publish.liveEdit.publishDisabled',
ALREADY_PUBLISHED: 'action.publish.alreadyPublished.noTimeAgo.tooltip',
NO_CHANGES: 'action.publish.tooltip.noChanges',
NOT_READY: 'action.publish.disabled.notReady',
LIVE_EDIT_ENABLED: 'action.publish.live-edit.publish-disabled',
ALREADY_PUBLISHED: 'action.publish.already-published.no-time-ago.tooltip',
NO_CHANGES: 'action.publish.tooltip.no-changes',
NOT_READY: 'action.publish.disabled.not-ready',
} as const

function getDisabledReason(
Expand Down Expand Up @@ -143,8 +143,8 @@ export const PublishAction: DocumentActionComponent = (props) => {
if (liveEdit) {
return {
tone: 'positive',
label: t('action.publish.liveEdit.label'),
title: t('action.publish.liveEdit.tooltip'),
label: t('action.publish.live-edit.label'),
title: t('action.publish.live-edit.tooltip'),
disabled: true,
}
}
Expand Down
Loading

0 comments on commit 5a4fc06

Please sign in to comment.