Skip to content

Commit

Permalink
feat(i18n): add i18n resources for browser document title (#5477)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and juice49 committed Jan 17, 2024
1 parent 800a35b commit b9f1b93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import React, {useEffect} from 'react'
import type {ObjectSchemaType} from '@sanity/types'
import type {Panes} from '../../structureResolvers'
import {structureLocaleNamespace} from '../../i18n'
import type {DocumentPaneNode} from '../../types'
import {useStructureTool} from '../../useStructureTool'
import {LOADING_PANE} from '../../constants'
import {useEditState, useSchema, unstable_useValuePreview as useValuePreview} from 'sanity'
import {
useEditState,
useSchema,
useTranslation,
unstable_useValuePreview as useValuePreview,
} from 'sanity'

interface StructureTitleProps {
resolvedPanes: Panes['resolvedPanes']
Expand All @@ -14,6 +20,7 @@ const DocumentTitle = (props: {documentId: string; documentType: string}) => {
const {documentId, documentType} = props
const editState = useEditState(documentId, documentType)
const schema = useSchema()
const {t} = useTranslation(structureLocaleNamespace)
const isNewDocument = !editState?.published && !editState?.draft
const documentValue = editState?.draft || editState?.published
const schemaType = schema.get(documentType) as ObjectSchemaType | undefined
Expand All @@ -25,8 +32,10 @@ const DocumentTitle = (props: {documentId: string; documentType: string}) => {
})

const documentTitle = isNewDocument
? `New ${schemaType?.title || schemaType?.name}`
: value?.title || 'Untitled'
? t('browser-document-title.new-document', {
schemaType: schemaType?.title || schemaType?.name,
})
: value?.title || t('browser-document-title.untitled-document')

const settled = editState.ready && !previewValueIsLoading
const newTitle = useConstructDocumentTitle(documentTitle)
Expand Down Expand Up @@ -81,8 +90,8 @@ export const StructureTitle = (props: StructureTitleProps) => {
*
* @param activeTitle - Title of the first segment
*
* @returns A pipe delimited title in the format `${activeTitle} | %BASE_DESK_TITLE%`
* or simply `%BASE_DESK_TITLE` if `activeTitle` is undefined.
* @returns A pipe delimited title in the format `${activeTitle} | %BASE_STRUCTURE_TITLE%`
* or simply `%BASE_STRUCTURE_TITLE` if `activeTitle` is undefined.
*/
function useConstructDocumentTitle(activeTitle?: string) {
const structureToolBaseTitle = useStructureTool().structureContext.title
Expand Down
5 changes: 5 additions & 0 deletions packages/sanity/src/structure/i18n/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ const structureLocaleStrings = defineLocalesResources('structure', {
'banners.reference-changed-banner.reason-removed.text':
'This reference has been removed since you opened it.',

/** Browser/tab title when creating a new document of a given type */
'browser-document-title.new-document': 'New {{schemaType}}',
/** Browser/tab title when editing a document where the title cannot be resolved from preview configuration */
'browser-document-title.untitled-document': 'Untitled',

/** The action menu button aria-label */
'buttons.action-menu-button.aria-label': 'Open document actions',
/** The action menu button tooltip */
Expand Down

0 comments on commit b9f1b93

Please sign in to comment.