Skip to content

Commit

Permalink
Merge pull request #64 from sanity-io/feat/derive-language-from-document
Browse files Browse the repository at this point in the history
feat: Derive language from document
  • Loading branch information
LiamMartens authored Sep 4, 2022
2 parents e86db6b + 72fcfd9 commit f7ad0d4
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v0.3.6
* Updates the internal logic to derive the language from the given field name as opposed to the ID.

## v0.3.5
* Fix how translation draft references are created so that they can be deleted

## v0.3.4
* Updated docs regarding V3 studio

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sanity/document-internationalization",
"version": "0.3.5",
"version": "0.3.6",
"license": "MIT",
"engines": {
"node": ">=14"
Expand Down
8 changes: 5 additions & 3 deletions src/actions/DuplicateWithi18nAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getBaseIdFromId,
getTranslationsFor,
buildDocId,
getLanguageFromId,
getLanguageFromDocument,
getConfig,
} from '../utils'
import {IUseDocumentOperationResult} from '../types'
import {UiMessages} from '../constants'
Expand All @@ -24,6 +25,7 @@ const DISABLED_REASON_TITLE = {

export const DuplicateWithi18nAction: DocumentActionComponent = ({id, type, draft, published}) => {
const toast = useToast()
const config = React.useMemo(() => getConfig(type), [type])
const client = getSanityClient()
const baseDocumentId = getBaseIdFromId(id)
const {duplicate: duplicateOp} = useDocumentOperation(id, type) as IUseDocumentOperationResult
Expand All @@ -42,7 +44,7 @@ export const DuplicateWithi18nAction: DocumentActionComponent = ({id, type, draf
})
translations.forEach((t) => {
const isDraft = t._id.startsWith('drafts.')
const newId = buildDocId(dupeId, getLanguageFromId(t._id))
const newId = buildDocId(dupeId, getLanguageFromDocument(t, config))
transaction.create({
...t,
_id: isDraft ? `drafts.${newId}` : newId,
Expand All @@ -54,7 +56,7 @@ export const DuplicateWithi18nAction: DocumentActionComponent = ({id, type, draf
toast.push(err.message)
}
setDuplicating(false)
}, [client, toast, baseDocumentId, type, draft, published])
}, [client, toast, config, baseDocumentId, type, draft, published])

return {
icon: ContentCopyIcon,
Expand Down
9 changes: 4 additions & 5 deletions src/badges/LanguageBadge.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {IResolverProps} from '../types'
import {getLanguageFromId, getSchema, getConfig} from '../utils'
import {getConfig, getLanguageFromDocument} from '../utils'

export const LanguageBadge = (props: IResolverProps) => {
const config = getConfig(props.type)
const doc = props.draft || props.published
const idLang = getLanguageFromId(props.id)
const fieldName = config.fieldNames.lang
if ((doc && doc[fieldName]) || idLang) {
const lang = doc ? getLanguageFromDocument(doc, config) : null
if (lang) {
return {
label: (doc && doc[fieldName]) || idLang,
label: lang,
color: 'success',
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getBaseIdFromId,
getBaseLanguage,
getConfig,
getLanguageFromId,
getLanguageFromDocument,
} from '../../../utils'
import {useLanguages} from '../../hooks'
import {UiMessages} from '../../../constants'
Expand Down Expand Up @@ -39,8 +39,8 @@ export const LanguageSelect: React.FC<Props> = ({schemaType, document}) => {
[languages, config.base]
)
const currentLanguageCode = React.useMemo(
() => getLanguageFromId(document._id) || (baseLanguage ? baseLanguage.id : null),
[document._id, baseLanguage]
() => getLanguageFromDocument(document, config) || (baseLanguage ? baseLanguage.id : null),
[document, baseLanguage, config]
)
const currentLanguageObject = React.useMemo(
() => languages.find((lang) => lang.id === currentLanguageCode),
Expand Down
6 changes: 3 additions & 3 deletions src/structure/utils/fixIdStructureMismatchDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
createSanityReference,
getBaseIdFromId,
getConfig,
getLanguageFromId,
getLanguageFromDocument,
getSanityClient,
} from '../../utils'

export const fixIdStructureMismatchDocuments = async (
schema: string,
documents: Ti18nDocument[]
) => {
): Promise<void> => {
const config = getConfig()
const sanityClient = getSanityClient()
const refsFieldName = config.fieldNames.references
Expand All @@ -37,7 +37,7 @@ export const fixIdStructureMismatchDocuments = async (
const transaction = sanityClient.transaction()
documentsChunk.forEach((d) => {
const baseId = getBaseIdFromId(d._id)
const lang = getLanguageFromId(d._id)
const lang = getLanguageFromDocument(d, config)
if (lang) {
const newId = buildDocId(baseId, lang)
transaction.createIfNotExists({
Expand Down
2 changes: 2 additions & 0 deletions src/structure/utils/fixLanguageFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const fixLanguageFields = async (schema: string, documents: Ti18nDocument
const base =
(typeof schemaObject.i18n === 'object' ? schemaObject.i18n.base : undefined) || config.base
if (!d[langFieldName]) {
// @README keep the language from ID behavior
// because in this case we expect the language field not to be available
const language = getLanguageFromId(d._id) || base
await sanityClient
.patch(d._id, {
Expand Down
6 changes: 3 additions & 3 deletions src/structure/utils/fixTranslationRefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
createSanityReference,
getBaseIdFromId,
getConfig,
getLanguageFromId,
getLanguageFromDocument,
getSanityClient,
} from '../../utils'

export const fixTranslationRefs = async (
schema: string,
baseDocuments: Ti18nDocument[],
translatedDocuments: Ti18nDocument[]
) => {
): Promise<void> => {
const sanityClient = getSanityClient()
const config = getConfig(schema)
const refsFieldName = config.fieldNames.references
Expand All @@ -28,7 +28,7 @@ export const fixTranslationRefs = async (
if (config.referenceBehavior !== ReferenceBehavior.DISABLED) {
translatedRefs = _.compact(
relevantTranslations.map((doc) => {
const lang = getLanguageFromId(doc._id)
const lang = getLanguageFromDocument(doc, config)
if (!lang) return null
return {
_key: lang,
Expand Down
2 changes: 1 addition & 1 deletion src/types/Ti18nConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IdStructure, ReferenceBehavior} from '../constants'
import {TLanguagesOption} from './TLanguagesOption'
import {TFieldNamesConfig} from './TFieldNamesConfig'
import {IdStructure, ReferenceBehavior} from '../constants'

export type Ti18nConfig = {
base?: string
Expand Down
9 changes: 9 additions & 0 deletions src/utils/getLanguageFromDocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type {SanityDocument} from '@sanity/client'
import type {getConfig} from './getConfig'

export function getLanguageFromDocument(
doc: SanityDocument,
config: ReturnType<typeof getConfig>
): string | null {
return doc?.[config.fieldNames.lang] || config.base || null
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './createSanityReference'
export * from './updateIntlFieldsForDocument'
export * from './serializePath'
export * from './batch'
export * from './getLanguageFromDocument'
7 changes: 4 additions & 3 deletions src/utils/updateIntlFieldsForDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {getSanityClient} from './getSanityClient'
import {getConfig} from './getConfig'
import {getSchema} from './getSchema'
import {getLanguagesFromOption} from './getLanguagesFromOption'
import {getLanguageFromId} from './getLanguageFromId'
import {getBaseLanguage} from './getBaseLanguage'
import {getTranslationsFor} from './getTranslationsForId'
import {getBaseIdFromId} from './getBaseIdFromId'
import {createSanityReference} from './createSanityReference'
import {getLanguageFromDocument} from './getLanguageFromDocument'

// @TODO make this into a hook so the hook
// can look up the existance of a base document on its own
Expand All @@ -28,7 +28,8 @@ export async function updateIntlFieldsForDocument(
const refsFieldName = config.fieldNames.references
const baseRefFieldName = config.fieldNames.baseReference
const langs = await getLanguagesFromOption(config.languages, document)
const languageId = getLanguageFromId(id) || getBaseLanguage(langs, config.base)?.id
const languageId =
getLanguageFromDocument(document, config) || getBaseLanguage(langs, config.base)?.id

// Update I18n field for current document
const currentDocumentTransaction = client.transaction()
Expand Down Expand Up @@ -57,7 +58,7 @@ export async function updateIntlFieldsForDocument(
if (config.referenceBehavior !== ReferenceBehavior.DISABLED) {
translatedRefs = _.compact(
translatedDocuments.map((doc) => {
const lang = getLanguageFromId(doc._id)
const lang = getLanguageFromDocument(doc, config)
if (!lang) return null
return {
_key: lang,
Expand Down

0 comments on commit f7ad0d4

Please sign in to comment.