Skip to content

Commit

Permalink
V3-languages for localisation (#646)
Browse files Browse the repository at this point in the history
* cleanup of deskstructure

* language for localization

* renaming from languagedetails to supportedlanguages

* update schema with supportedLanguage

* Update studio/schemas/deskStructure.ts

Co-authored-by: christinaroise <[email protected]>

* variablename update

* Update studio/schemas/deskStructure.ts

Co-authored-by: Mathias Oterhals Myklebust <[email protected]>

* Update studio/schemas/deskStructure.ts

Co-authored-by: Mathias Oterhals Myklebust <[email protected]>

* variable name fix

---------

Co-authored-by: christinaroise <[email protected]>
Co-authored-by: Mathias Oterhals Myklebust <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2024
1 parent 5b415d7 commit 96f69a3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
2 changes: 1 addition & 1 deletion languages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const languages = [
export const languages = [
{ id: "en", title: "English" },
{ id: "se", title: "Swedish" },
{ id: "no", title: "Norwegian" },
Expand Down
2 changes: 2 additions & 0 deletions studio/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import companyLocation from "./schemas/documents/companyLocation";
import compensations from "./schemas/documents/compensations";
import redirect from "./schemas/documents/redirect";
import benefitsByLocation from "./schemas/objects/compensations/benefitsByLocation";
import supportedLanguages from "./schemas/documents/supportedLanguages";
import defaultSeo from "./schemas/documents/admin/defaultSeo";

export const schema: { types: SchemaTypeDefinition[] } = {
Expand All @@ -35,6 +36,7 @@ export const schema: { types: SchemaTypeDefinition[] } = {
redirect,
benefitsByLocation,
companyLocation,
supportedLanguages,
defaultSeo,
],
};
17 changes: 14 additions & 3 deletions studio/schemas/deskStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
HeartIcon,
SparkleIcon,
CaseIcon,
TranslateIcon,
DoubleChevronRightIcon,
PinIcon,
SearchIcon,
Expand All @@ -20,6 +21,7 @@ import { legalDocumentID } from "./documents/legalDocuments";
import { compensationsId } from "./documents/compensations";
import { redirectId } from "./documents/redirect";
import { companyLocationID } from "./documents/companyLocation";
import { supportedLanguagesID } from "./documents/supportedLanguages";
import { defaultSeoID } from "./documents/admin/defaultSeo";

// Admin Section
Expand Down Expand Up @@ -79,6 +81,15 @@ const siteSettingSection = (S: StructureBuilder) =>
.child(
S.document().schemaType(soMeLinksID).documentId(soMeLinksID),
),
S.listItem()
.title("Supported Languages")
.icon(TranslateIcon)
.child(
S.document()
.schemaType(supportedLanguagesID)
.documentId(supportedLanguagesID)
.title("Supported Languages"),
),
S.listItem()
.title("Default SEO")
.icon(SearchIcon)
Expand All @@ -92,8 +103,6 @@ const siteSettingSection = (S: StructureBuilder) =>
.title("Broken Links")
.icon(DoubleChevronRightIcon)
.child(S.documentTypeList(redirectId).title("Redirects")),
//TODO: Add SEO Settings
//TODO: Add Language selector
]),
);

Expand Down Expand Up @@ -135,7 +144,7 @@ const SpecialPagesSection = (S: StructureBuilder) =>
);

// Main export
export default (S: StructureBuilder) =>
const buildDeskStructure = (S: StructureBuilder) =>
S.list()
.title("Content")
.items([
Expand All @@ -144,3 +153,5 @@ export default (S: StructureBuilder) =>
pagesSection(S),
SpecialPagesSection(S),
]);

export default buildDeskStructure;
71 changes: 71 additions & 0 deletions studio/schemas/documents/supportedLanguages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { defineType } from "sanity";
import { languages } from "languages";

const languageOptions = languages.map((language) => {
return { title: language.title, value: language.id };
});

export const supportedLanguagesID = "supportedLanguages";

const supportedLanguages = defineType({
name: supportedLanguagesID,
type: "document",
fields: [
{
name: "languages",
type: "array",
description:
"Select languages available for translation and designate one as the default language for the homepage.",
of: [
{
type: "object",
fields: [
{
name: "language",
type: "string",
description: "Select the language available for translation",
title: "Language",
options: {
list: languageOptions,
},
},
{
name: "isDefault",
type: "boolean",
title: "Default Language",
description:
"Indicate whether the language should be set as the default. Please note that only one default language is allowed.",
},
],
preview: {
select: {
title: "language",
},
prepare(selection) {
const { title } = selection;
const languageOption = languageOptions.find(
(option) => option.value === title,
);
return {
title: languageOption ? languageOption.title : title,
};
},
},
},
],
validation: (Rule) =>
Rule.custom((languages: { language: string; isDefault: boolean }[]) => {
const defaultLanguages = languages.filter((lang) => lang.isDefault);
if (defaultLanguages.length > 1) {
return "Only one default language is allowed";
}
if (defaultLanguages.length === 0 && languages.length > 0) {
return "At least one language must be marked as default";
}
return true;
}),
},
],
});

export default supportedLanguages;

0 comments on commit 96f69a3

Please sign in to comment.