Skip to content

Commit 4b7cbdd

Browse files
committed
adding localization for posts content
1 parent b1129ca commit 4b7cbdd

File tree

8 files changed

+229
-273
lines changed

8 files changed

+229
-273
lines changed

src/layouts/Header.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const {
6868
<LanguagePicker
6969
locales={locales}
7070
currentLocale={currentLocale}
71-
currentUrl={currentUrl}
71+
currentUrl={currentUrl.pathname}
7272
/>
7373
</MobileHeader>
7474
</div>
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
---
2-
import PostContent from '@apps/posts/PostContent';
3-
import { getTranslations } from '@backend/i18n';
4-
import { fetchPost, fetchPosts } from '@backend/tina';
5-
import Container from '@components/Container.astro';
6-
import config from '@config';
7-
import Layout from '@layouts/Layout.astro';
8-
import { hasPostsContent } from '@utils/config';
9-
import _ from 'underscore';
2+
import PostContent from "@apps/posts/PostContent";
3+
import { getTranslations } from "@backend/i18n";
4+
import { fetchPost, fetchPosts } from "@backend/tina";
5+
import Container from "@components/Container.astro";
6+
import config from "@config";
7+
import { getLocalizedContent } from "@i18n/utils";
8+
import Layout from "@layouts/Layout.astro";
9+
import { hasPostsContent } from "@utils/config";
10+
import _ from "underscore";
1011
1112
const { slug } = Astro.params;
1213
const post = await fetchPost(slug);
1314
14-
const { t } = await getTranslations(Astro.currentLocale);
15+
const locale = Astro.currentLocale;
16+
17+
const localePost = getLocalizedContent(post, locale.replaceAll("-", "_")); //this is to deal with Tina's field naming restrictions
18+
19+
const { t } = await getTranslations(locale);
1520
1621
export const getStaticPaths = async () => {
1722
if (!hasPostsContent(config)) {
@@ -25,32 +30,27 @@ export const getStaticPaths = async () => {
2530
2631
_.each(locales, (lang) => {
2732
_.each(posts, (post) => {
28-
staticPaths.push({ params: {
29-
lang,
30-
slug: post?._sys.filename
31-
}});
33+
staticPaths.push({
34+
params: {
35+
lang,
36+
slug: post?._sys.filename,
37+
},
38+
});
3239
});
3340
});
3441
3542
return staticPaths;
3643
};
3744
---
3845

39-
<Layout
40-
footer
41-
t={t}
42-
tab='posts'
43-
title={slug}
44-
>
45-
<Container
46-
className='pb-16 w-full'
47-
>
46+
<Layout footer t={t} tab="posts" title={localePost?.title}>
47+
<Container className="pb-16 w-full">
4848
<PostContent
4949
content={post?.body}
5050
title={post?.title}
5151
author={post?.author}
5252
date={post?.date}
53-
client:only='react'
53+
client:only="react"
5454
/>
5555
</Container>
5656
</Layout>

src/pages/[lang]/posts/index.astro

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Card from '@components/Card.astro';
55
import Cards from '@components/Cards.astro';
66
import Container from '@components/Container.astro';
77
import config from '@config';
8+
import { getLocalizedContent } from '@i18n/utils';
89
import Layout from '@layouts/Layout.astro';
910
import { hasPostsContent } from '@utils/config';
1011
import { getRelativeLocaleUrl } from 'astro:i18n';
@@ -21,6 +22,10 @@ export const getStaticPaths = () => {
2122
return _.map(config.i18n.locales, (lang) => ({ params: { lang }}));
2223
}
2324
25+
//Localize the post titles
26+
const localeField = Astro.currentLocale.replaceAll('-', '_') //this is to deal with Tina's field naming conventions
27+
const localizedPosts = _.map(posts, (post: any) => (getLocalizedContent(post, localeField)))
28+
2429
---
2530

2631
<Layout
@@ -38,7 +43,7 @@ export const getStaticPaths = () => {
3843
{ t('posts') }
3944
</h1>
4045
<Cards>
41-
{ _.map(posts, (post) => (
46+
{ _.map(localizedPosts, (post) => (
4247
<Card
4348
imageUrl={post?.cardImage}
4449
alt={post?.imageAlt}

tina/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Branding from './content/branding';
55
import I18n from './content/i18n';
66
import PagesCollection from './content/pages';
77
import PathsCollection from './content/paths';
8-
import PostsCollection from './content/posts';
8+
import { getPostsCollection } from './content/posts';
99
import Settings from './content/settings';
1010
import { CustomAuthProvider } from './auth-provider';
1111
import { TinaUserCollection, UsernamePasswordAuthJSProvider } from 'tinacms-authjs/dist/tinacms';
@@ -14,6 +14,11 @@ const isLocal = process.env.TINA_PUBLIC_IS_LOCAL === 'true';
1414
const localContentPath = process.env.TINA_LOCAL_CONTENT_PATH;
1515
const useSSO = process.env.TINA_PUBLIC_AUTH_USE_KEYCLOAK === 'true';
1616

17+
//deal with localizing the Posts collection
18+
const defaultLocale = config.i18n.default_locale;
19+
const otherLocales = config.i18n.locales.filter((loc) => (loc !== defaultLocale))
20+
const PostsCollection = getPostsCollection(otherLocales)
21+
1722
export default defineConfig({
1823
authProvider: isLocal
1924
? new LocalAuthProvider()

tina/content/paths.ts

Lines changed: 4 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import TinaMediaPicker from '../components/TinaMediaPicker';
21
import TinaPlacePicker from '../components/TinaPlacePicker';
32
import { Collection } from '@tinacms/schema-tools';
3+
import { pathTemplates } from './templates';
4+
import type { RichTextTemplate } from '@tinacms/schema-tools';
45

56
const Paths: Collection = {
67
name: 'path',
@@ -40,79 +41,7 @@ const Paths: Collection = {
4041
label: 'Description',
4142
type: 'rich-text',
4243
isBody: true,
43-
templates: [
44-
{
45-
name: 'iframe',
46-
label: 'AV Embed',
47-
fields: [
48-
{
49-
name: 'src',
50-
label: 'Embed Link',
51-
type: 'string',
52-
required: true
53-
},
54-
{
55-
name: 'width',
56-
label: 'Width (pixels)',
57-
type: 'number',
58-
ui: {
59-
parse: (val?: number)=>val || 0,
60-
format: (val?: number)=> val === 0 ? null : val
61-
}
62-
},
63-
{
64-
name: 'height',
65-
label: 'Height (pixels)',
66-
type: 'number',
67-
ui: {
68-
parse: (val?: number)=>val || 0,
69-
format: (val?: number)=> val === 0 ? null : val
70-
}
71-
}
72-
]
73-
},
74-
{
75-
name: "media",
76-
label: "Media",
77-
fields: [
78-
{
79-
name: "media",
80-
label: "Media",
81-
type: "object",
82-
fields: [
83-
{
84-
name: "title",
85-
label: "Title",
86-
type: "string",
87-
},
88-
{
89-
name: "uuid",
90-
label: "UUID",
91-
type: "string"
92-
},
93-
{
94-
name: "manifest_url",
95-
label: "Manifest URL",
96-
type: "string"
97-
},
98-
{
99-
name: "content_url",
100-
label: "Content URL",
101-
type: "string"
102-
},
103-
{
104-
name: "content_preview_url",
105-
label: "Content Preview URL",
106-
type: "string"
107-
}
108-
],
109-
ui: {
110-
component: TinaMediaPicker
111-
}
112-
}
113-
]
114-
}
115-
]
44+
templates: pathTemplates as RichTextTemplate<false>[]
11645
},
11746
{
11847
name: 'path',
@@ -167,38 +96,7 @@ const Paths: Collection = {
16796
name: 'blurb',
16897
label: 'Blurb',
16998
type: 'rich-text',
170-
templates: [
171-
{
172-
name: 'iframe',
173-
label: 'AV Embed',
174-
fields: [
175-
{
176-
name: 'src',
177-
label: 'Embed Link',
178-
type: 'string',
179-
required: true
180-
},
181-
{
182-
name: 'width',
183-
label: 'Width (pixels)',
184-
type: 'number',
185-
ui: {
186-
parse: (val?: number)=>val || 0,
187-
format: (val?: number)=> val === 0 ? null : val
188-
}
189-
},
190-
{
191-
name: 'height',
192-
label: 'Height (pixels)',
193-
type: 'number',
194-
ui: {
195-
parse: (val?: number)=>val || 0,
196-
format: (val?: number)=> val === 0 ? null : val
197-
}
198-
}
199-
]
200-
}
201-
]
99+
templates: pathTemplates as RichTextTemplate<false>[]
202100
}
203101
]
204102
}

0 commit comments

Comments
 (0)