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): add i18n resources for browser document title #5477

Merged
merged 1 commit into from
Jan 16, 2024
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
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 @@ -114,6 +114,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
Loading