-
Notifications
You must be signed in to change notification settings - Fork 4
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
v3 - update language schema and add doc translation #703
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
const getNextDefaultLanguage = (updatedValue: Language[], lang: Language) => { | ||
const indexInSupported = supportedLanguages.findIndex( | ||
(language) => language.id === lang.id, | ||
); | ||
return ( | ||
supportedLanguages | ||
.slice(indexInSupported + 1) | ||
.find((language) => | ||
updatedValue.some((item) => item.id === language.id), | ||
) || | ||
supportedLanguages.find((language) => | ||
updatedValue.some((item) => item.id === language.id), | ||
) | ||
); | ||
}; | ||
|
||
const handleLanguageSelection = (lang: Language, isSelected: boolean) => { | ||
if (isSelected && value.length === 1) return; | ||
|
||
let updatedValue = isSelected | ||
? value.filter((item) => item.id !== lang.id) | ||
: [...value, { ...lang, default: false }]; | ||
|
||
if (isSelected && lang.id === currentDefaultLanguage) { | ||
const nextDefaultLanguage = getNextDefaultLanguage(updatedValue, lang); | ||
updatedValue = updatedValue.map((item) => ({ | ||
...item, | ||
default: item.id === (nextDefaultLanguage?.id || null), | ||
})); | ||
} | ||
|
||
onChange(PatchEvent.from(set(updatedValue))); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a suggestion for a cleaner iterator for finding a new default, and an abort if a new default could not be found (edge-case that should not really happen).
const getNextDefaultLanguage = (updatedValue: Language[], lang: Language) => { | |
const indexInSupported = supportedLanguages.findIndex( | |
(language) => language.id === lang.id, | |
); | |
return ( | |
supportedLanguages | |
.slice(indexInSupported + 1) | |
.find((language) => | |
updatedValue.some((item) => item.id === language.id), | |
) || | |
supportedLanguages.find((language) => | |
updatedValue.some((item) => item.id === language.id), | |
) | |
); | |
}; | |
const handleLanguageSelection = (lang: Language, isSelected: boolean) => { | |
if (isSelected && value.length === 1) return; | |
let updatedValue = isSelected | |
? value.filter((item) => item.id !== lang.id) | |
: [...value, { ...lang, default: false }]; | |
if (isSelected && lang.id === currentDefaultLanguage) { | |
const nextDefaultLanguage = getNextDefaultLanguage(updatedValue, lang); | |
updatedValue = updatedValue.map((item) => ({ | |
...item, | |
default: item.id === (nextDefaultLanguage?.id || null), | |
})); | |
} | |
onChange(PatchEvent.from(set(updatedValue))); | |
}; | |
const getNextDefaultLanguageId = ( | |
updatedValue: Language[], | |
oldDefaultLanguage: Language, | |
) => { | |
const oldIndex = supportedLanguages.findIndex( | |
(language) => language.id === oldDefaultLanguage.id, | |
); | |
for (let i = 0; i < supportedLanguages.length; i++) { | |
const languageId = | |
supportedLanguages[(i + oldIndex) % supportedLanguages.length].id; | |
if (updatedValue.some((item) => item.id === languageId)) { | |
return languageId; | |
} | |
} | |
return null; | |
}; | |
const handleLanguageSelection = (lang: Language, isSelected: boolean) => { | |
if (isSelected && value.length === 1) return; | |
let updatedValue = isSelected | |
? value.filter((item) => item.id !== lang.id) | |
: [...value, { ...lang, default: false }]; | |
if (isSelected && lang.id === currentDefaultLanguage) { | |
const nextDefaultLanguageId = getNextDefaultLanguageId( | |
updatedValue, | |
lang, | |
); | |
if (nextDefaultLanguageId === null) { | |
// abort selection change | |
return; | |
} | |
updatedValue = updatedValue.map((item) => ({ | |
...item, | |
default: item.id === nextDefaultLanguageId, | |
})); | |
} | |
onChange(PatchEvent.from(set(updatedValue))); | |
}; |
Short Description
This feature introduces a custom language component to manage default and supported languages within the main studio. By default, the LanguageSettings schema initializes with English.
The documentInternationalization plugin currently lacks support for asynchronous language lists in initial value templates. As a result, all supported languages are displayed in the studio.
Users can set a default language and add supported languages, establishing a reliable reference for the frontend. However, this feature does not filter content based on the selected supported languages. Meaning, this allows users to create content in languages not supported by the site. For example, while Variant Norge may select Norwegian and English, Swedish will still appear as a translation option.
To ensure data integrity, documents without a language value are filtered out, preventing the creation of documents that lack a specified language. This design also facilitates future adjustments for integrating asynchronous language lists with minimal effort.
Visual Overview (Image/Video)
Language Settings
Document translation has been added to Legal Documents, they are currently not sorted.
Checklist
Please ensure that you’ve completed the following checkpoints before submitting your pull request:
I'm creating an issue to update the readme with the translations setup once we have field translation up and running too.