Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Localization support #18

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
refactor(studio): localization option for fields to be localized
  • Loading branch information
afenton90 committed Jun 8, 2020
commit 3fae9d866792a300dfffdd0275ae4b1062deec8d
48 changes: 24 additions & 24 deletions template/studio/localization/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
import {generateLocaleType, getLocaleTypeName, supportedLanguages} from './localize'

const localizedTypes = ['string', 'text', 'slug', 'portableText', 'simplePortableText']
const localizedTypes = new Set()
const fieldsModified = []

const localizeField = (field, traceModified) => {
if (!localizedTypes.includes(field.type)) return field
const localizeField = (field, parent) => {
if (!field.options || (field.options && !field.options.localization)) return field

const localizedField = {
...field,
type: getLocaleTypeName(field.type)
}

if (traceModified) {
traceModified.push(localizedField)
}
localizedTypes.add(field.type)
fieldsModified.push(`${parent.name}.${localizedField.name}`)

return localizedField
}

const inModified = name => field => field.name === name
const inModified = ({baseKey, localizedDef, selectorToChange}) => field => field.startsWith(`${localizedDef.name}.${baseKey}`) || field === selectorToChange

export const localizeDocumentFields = typeDef => {
// 1. Loop over all schema defs
// 2. For each field that has localization: true create a custom type
// 3. Save the changed field in a global list

export const localizeSupportedTypes = typeDef => {
const fieldsModified = []
const localizedDef = {...typeDef}

// Localize fields
if (localizedDef.fields) {
localizedDef.fields = localizedDef.fields.map(field => {
if (field.type === 'array') {
return {
...field,
of: field.of.map(localizeField)
}
}

return localizeField(field, fieldsModified)
})
localizedDef.fields = localizedDef.fields.map(field => localizeField(field, localizedDef))
}

// Localize any previews to first language
if (localizedDef.preview && localizedDef.preview.select && fieldsModified.length > 0) {
if (fieldsModified.length > 0 && localizedDef.preview && localizedDef.preview.select && fieldsModified.length > 0) {
localizedDef.preview = {
...localizedDef.preview,
select: Object.keys(localizedDef.preview.select).reduce((select, key) => {
@@ -51,16 +45,18 @@ export const localizeSupportedTypes = typeDef => {
baseKey = selectorToChange
}

if (!fieldsModified.find(inModified(baseKey))) {
if (!fieldsModified.find(inModified({baseKey, localizedDef, selectorToChange}))) {
select[key] = localizedDef.preview.select[key]
} else {
let prefix = selectorToChange
let suffix = ''
if (selectorToChange.includes('.')) {
if (selectorToChange.includes('.') && !fieldsModified.includes(selectorToChange)) {
prefix = selectorToChange.substring(0, selectorToChange.indexOf('.'))
suffix = selectorToChange.substring(selectorToChange.indexOf('.'))
select[key] = `${prefix}.${supportedLanguages[0].id}${suffix}`
} else {
select[key] = `${selectorToChange}.${supportedLanguages[0].id}`
}
select[key] = `${prefix}.${supportedLanguages[0].id}${suffix}`
}
return select
}, {})
@@ -70,4 +66,8 @@ export const localizeSupportedTypes = typeDef => {
return localizedDef
}

export default localizedTypes.map(generateLocaleType)
export const getLocalisedTypes = () => {
console.log(`Fields Modified:: `, fieldsModified)
console.log(`Localised Types:: `, localizedTypes.values())
return Array.from(localizedTypes.values()).map(generateLocaleType)
}
2 changes: 1 addition & 1 deletion template/studio/localization/localize.js
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ export const supportedLanguages = [

export const getLocaleTypeName = (name) => `locale${name.charAt(0).toUpperCase() + name.slice(1)}`

export const generateLocaleType = (type) => ({
export const generateLocaleType = (type) => console.log(`Generating locale type:: `, type) || ({
title: type,
name: getLocaleTypeName(type),
type: 'object',
3 changes: 3 additions & 0 deletions template/studio/schemas/documents/page.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,9 @@ export default {
name: 'title',
type: 'string',
title: 'Title',
options: {
localization: true
}
},
{
name: 'content',
7 changes: 5 additions & 2 deletions template/studio/schemas/documents/route.js
Original file line number Diff line number Diff line change
@@ -9,7 +9,10 @@ export default {
{
name: 'slug',
type: 'slug',
title: 'Slug'
title: 'Slug',
options: {
localization: true
}
},
{
name: 'page',
@@ -37,7 +40,7 @@ export default {
preview: {
select: {
slug: 'slug.current',
pageTitle: 'page.title.en'
pageTitle: 'page.title'
},
prepare ({slug, pageTitle}) {
return {
3 changes: 3 additions & 0 deletions template/studio/schemas/objects/hero.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,9 @@ export default {
name: 'heading',
type: 'string',
title: 'Heading',
options: {
localization: true
}
},
{
name: 'tagline',
6 changes: 3 additions & 3 deletions template/studio/schemas/schema.js
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ import mailchimp from './objects/mailchimp'
import textSection from './objects/textSection'

// Localised types
import localized, { localizeSupportedTypes } from '../localization'
import { localizeDocumentFields, getLocalisedTypes } from '../localization'

const types = [
cta,
@@ -43,8 +43,8 @@ const types = [
siteConfig,
textSection,
]
.map(localizeSupportedTypes)
.concat(localized)
.map(localizeDocumentFields)
.concat(getLocalisedTypes())

// Then we give our schema to the builder and provide the result to Sanity
export default createSchema({