Skip to content

Commit

Permalink
Add i18n preview for all content types
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Richard committed Jan 17, 2024
1 parent 850d573 commit 01b7ecd
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 203 deletions.
2 changes: 1 addition & 1 deletion cms/lib/desk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function buildStandaloneList(
S.documentListItem()
.schemaType(type)
.id(type + '-' + lang.id)
.title(title + ' (' + lang.id.toLocaleUpperCase() + ')'),
.title(title),
),
)
.canHandleIntent(
Expand Down
41 changes: 32 additions & 9 deletions cms/lib/previews/localization.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { PreviewValue } from 'sanity';
import React from 'react';
import { langToFlag } from '$lib/country';
import { LANGUAGES } from '$lib/constants';

export type SanityPreview = {
select: {
Expand All @@ -18,7 +20,6 @@ export type SanityPreview = {
* @return {SanityPreview} - Sanity preview object
*/
export function preview(title: string): SanityPreview {
const ot = title;
return {
select: {
lang: 'language',
Expand All @@ -28,14 +29,36 @@ export function preview(title: string): SanityPreview {
const { lang } = selection;
const { title } = selection;

return {
title: title || ot,
media: (
<span className="flag" style={{ fontSize: '2rem' }}>
{langToFlag(lang || 'en_US')}
</span>
),
};
return i18nPreview(
title,
lang,
LANGUAGES.find((l) => l.id === lang)?.title,
);
},
};
}

/**
* Actual preview to feed to Sanity
* @param {string} title
* @param {string} lang
* @param {string} [subtitle]
* @return {PreviewValue}
*/
export function i18nPreview(
title: string,
lang: string,
subtitle: string | undefined = undefined,
): PreviewValue {
const output = {
title,
subtitle,
media: (
<span className="flag" style={{ fontSize: '2rem' }}>
{langToFlag(lang || 'en_US')}
</span>
),
};

return output;
}
4 changes: 2 additions & 2 deletions cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
},
"devDependencies": {
"@sanity/eslint-config-studio": "^3.0.1",
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"dotenv": "^16.0.3",
"eslint": "^8.29.0",
"eslint-config-google": "^0.14.0",
Expand Down
Loading

0 comments on commit 01b7ecd

Please sign in to comment.