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

v3 - update language schema and add doc translation #703

Merged
merged 22 commits into from
Sep 25, 2024

Conversation

christinaroise
Copy link

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
Screenshot 2024-09-19 at 21 39 08

Document translation has been added to Legal Documents, they are currently not sorted.
Screenshot 2024-09-23 at 15 21 46


Checklist

Please ensure that you’ve completed the following checkpoints before submitting your pull request:

  • Documentation: Relevant documentation has been added or updated (if applicable).
    I'm creating an issue to update the readme with the translations setup once we have field translation up and running too.
  • Testing: Have you tested your changes thoroughly?

Copy link

vercel bot commented Sep 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
variant-no ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2024 9:35am
variant-se-2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2024 9:35am

src/app/layout.tsx Outdated Show resolved Hide resolved
studio/deskStructure.ts Outdated Show resolved Hide resolved
@christinaroise christinaroise changed the title update language schema and add doc translation v3 - update language schema and add doc translation Sep 23, 2024
Copy link
Contributor

@anemne anemne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Comment on lines +29 to +61
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)));
};
Copy link
Contributor

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).

Suggested change
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)));
};

@christinaroise christinaroise merged commit ed3d0a1 into v3 Sep 25, 2024
6 checks passed
@christinaroise christinaroise deleted the update-language-schema branch September 25, 2024 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants