Skip to content

Commit

Permalink
feat: add i18n resource bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
nkgentile committed Nov 12, 2024
1 parent 0222cdf commit d974871
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/actions/DuplicateWithTranslationsAction.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CopyIcon} from '@sanity/icons'
import {CopyIcon, TranslateIcon} from '@sanity/icons'
import {useToast} from '@sanity/ui'
import {uuid} from '@sanity/uuid'
import {useCallback, useMemo, useState} from 'react'
Expand All @@ -21,10 +21,10 @@ import {useRouter} from 'sanity/router'
import {structureLocaleNamespace} from 'sanity/structure'

import {METADATA_SCHEMA_NAME, TRANSLATIONS_ARRAY_NAME} from '../constants'
import {documenti18nLocaleNamespace} from '../i18n'

const DISABLED_REASON_KEY = {
// TODO: add localization
I18N_METADATA_NOT_FOUND: 'This document does not have i18n metadata',
METADATA_NOT_FOUND: 'action.duplicate.disabled.missing-metadata',
NOTHING_TO_DUPLICATE: 'action.duplicate.disabled.nothing-to-duplicate',
NOT_READY: 'action.duplicate.disabled.not-ready',
}
Expand All @@ -46,7 +46,8 @@ export const DuplicateWithTranslationsAction: DocumentActionComponent = ({
const metadataDocument = useI18nMetadata(id)
const client = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS)
const toast = useToast()
const {t} = useTranslation(structureLocaleNamespace)
const {t: structureT} = useTranslation(structureLocaleNamespace)
const {t: documenti18nT} = useTranslation(documenti18nLocaleNamespace)
const currentUser = useCurrentUser()

const handle = useCallback(async () => {
Expand Down Expand Up @@ -174,7 +175,7 @@ export const DuplicateWithTranslationsAction: DocumentActionComponent = ({
return {
icon: CopyIcon,
disabled: true,
label: t('action.duplicate.label'),
label: documenti18nT('action.duplicate.label'),
title: (
<InsufficientPermissionsMessage
context="duplicate-document"
Expand All @@ -186,34 +187,35 @@ export const DuplicateWithTranslationsAction: DocumentActionComponent = ({

if (!metadataDocument) {
return {
icon: CopyIcon,
icon: TranslateIcon,
disabled: true,
label: t('action.duplicate.label'),
title: DISABLED_REASON_KEY.I18N_METADATA_NOT_FOUND,
label: documenti18nT('action.duplicate.label'),
title: documenti18nT(DISABLED_REASON_KEY.METADATA_NOT_FOUND),
}
}

return {
icon: CopyIcon,
icon: TranslateIcon,
disabled:
isDuplicating || Boolean(duplicate.disabled) || isPermissionsLoading,
label: isDuplicating
? t('action.duplicate.running.label')
: t('action.duplicate.label'),
? structureT('action.duplicate.running.label')
: documenti18nT('action.duplicate.label'),
title: duplicate.disabled
? t(DISABLED_REASON_KEY[duplicate.disabled])
? structureT(DISABLED_REASON_KEY[duplicate.disabled])
: '',
onHandle: handle,
}
}, [
currentUser,
documenti18nT,
duplicate.disabled,
handle,
isDuplicating,
isPermissionsLoading,
metadataDocument,
permissions?.granted,
t,
structureT,
])
}

Expand Down
21 changes: 21 additions & 0 deletions src/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {defineLocaleResourceBundle} from 'sanity'

/**
* The locale namespace for the document internationalization plugin.
*
* @public
*/
export const documenti18nLocaleNamespace =
'document-internationalization' as const

/**
* The default locale bundle for the document internationalization plugin, which is US English.
*
* @internal
*/
export const documentInternationalizationUsEnglishLocaleBundle =
defineLocaleResourceBundle({
locale: 'en-US',
namespace: documenti18nLocaleNamespace,
resources: () => import('./resources'),
})
5 changes: 5 additions & 0 deletions src/i18n/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
'action.duplicate.label': 'Duplicate with translations',
'action.duplicate.disabled.missing-metadata':
'The document cannot be duplicated because the metadata document is missing',
}
5 changes: 5 additions & 0 deletions src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {DocumentInternationalizationProvider} from './components/DocumentInterna
import {DocumentInternationalizationMenu} from './components/DocumentInternationalizationMenu'
import OptimisticallyStrengthen from './components/OptimisticallyStrengthen'
import {API_VERSION, DEFAULT_CONFIG, METADATA_SCHEMA_NAME} from './constants'
import {documentInternationalizationUsEnglishLocaleBundle} from './i18n'
import metadata from './schema/translation/metadata'
import type {PluginConfig, TranslationReference} from './types'

Expand Down Expand Up @@ -39,6 +40,10 @@ export const documentInternationalization = definePlugin<PluginConfig>(
},
},

i18n: {
bundles: [documentInternationalizationUsEnglishLocaleBundle],
},

// Adds:
// - A bulk-publishing UI component to the form
// - Will only work for projects on a compatible plan
Expand Down

0 comments on commit d974871

Please sign in to comment.