Skip to content

Commit 7301142

Browse files
committed
hiding non-localized content
1 parent 9bae8c6 commit 7301142

File tree

17 files changed

+77
-106
lines changed

17 files changed

+77
-106
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Copy the `/public/config.json` file into your content repository to `/content/se
8484
|----------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
8585
| content | Object | TinaCMS content collections configuration |
8686
| content.collections | Array | Array of content keys to allow for data entry and routing: "posts", "paths" |
87-
| content.localize_pages | Boolean | If `true` pages content will be pulled from a locale directory (e.g. `/en/My-Awesome-Page.mdx`) |
87+
| content.localize_content | Boolean | If `true` pages and other content will be pulled from a locale directory (e.g. `/en/My-Awesome-Page.mdx`) |
8888
| core_data | Object | Core Data configuration |
8989
| core_data.url | String | URL of the Core Data application |
9090
| core_data.project_ids | Array | Array of Core Data project IDs as strings |

docs/configuration-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Array<"paths" | "posts">
1818

1919
Required: No
2020

21-
### localize_pages
21+
### localize_content
2222

2323
If set to `true`, pages will be nested within a localized directory (e.g `/pages/en/Home.mdx`). If `false`, pages will not be localized and placed in the root `/pages` directory.
2424

src/backend/tina/i18n.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import _ from 'underscore';
88
* @param items
99
*/
1010
export const filterAll = async (locale: string, items: Array<any>) => {
11-
if (!config.content.localize_pages) {
11+
if (!config.content.localize_content) {
1212
return items;
1313
}
1414

@@ -35,11 +35,8 @@ export const filterAll = async (locale: string, items: Array<any>) => {
3535

3636
if (localized[locale]) {
3737
filtered.push(localized[locale]);
38-
} else if (localized[config.i18n.default_locale]) {
39-
filtered.push(localized[config.i18n.default_locale])
4038
}
4139
});
42-
4340
return filtered;
4441
};
4542

@@ -53,7 +50,7 @@ export const filterAll = async (locale: string, items: Array<any>) => {
5350
export const fetchOne = async (locale: string, slug: string, query: any) => {
5451
let response;
5552

56-
if (!config.content.localize_pages) {
53+
if (!config.content.localize_content) {
5754
response = await query({ relativePath: `${slug}.mdx` });
5855
} else {
5956
response = await query({ relativePath: `${locale}/${slug}.mdx` });

src/backend/tina/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,26 @@ export const fetchPages = async (locale: string, params?: any) => {
2828
return filterAll(locale, pages);
2929
};
3030

31-
export const fetchPath = async (slug: string) => {
32-
const response = await client.queries.path({ relativePath: `${slug}.mdx`});
31+
export const fetchPath = async (locale: string, slug: string) => {
32+
const response = await fetchOne(locale, slug, client.queries.path);
3333
return response.data?.path;
3434
};
3535

36-
export const fetchPaths = async () => {
36+
export const fetchPaths = async (locale: string) => {
3737
const response = await client.queries.pathConnection();
38-
return response.data?.pathConnection?.edges?.map((item) => item?.node);
38+
const paths = response.data?.pathConnection?.edges?.map((item) => item?.node);
39+
40+
return filterAll(locale, paths)
3941
};
4042

41-
export const fetchPost = async (slug: string) => {
42-
const response = await client.queries.post({ relativePath: `${slug}.mdx`});
43+
export const fetchPost = async (locale: string, slug: string) => {
44+
const response = await fetchOne(locale, slug, client.queries.post);
4345
return response.data?.post;
4446
};
4547

46-
export const fetchPosts = async () => {
48+
export const fetchPosts = async (locale: string) => {
4749
const response = await client.queries.postConnection();
48-
return response.data?.postConnection?.edges?.map((item) => item?.node);
50+
const posts = response.data?.postConnection?.edges?.map((item) => item?.node);
51+
52+
return filterAll(locale, posts)
4953
};

src/pages/[lang]/paths/[slug].astro

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@ import Layout from "@layouts/Layout.astro";
77
import _ from 'underscore';
88
99
const { slug } = Astro.params;
10-
const path = await fetchPath(slug);
10+
const locale = Astro.currentLocale;
11+
const path = await fetchPath(locale, slug);
1112
12-
const { t } = await getTranslations(Astro.currentLocale);
13+
const { t } = await getTranslations(locale);
1314
const title = path?.title || t('paths');
1415
1516
export const getStaticPaths = async () => {
16-
const staticPaths = [];
17+
const paths = [];
1718
18-
const locales = config.i18n.locales;
19-
const paths = await fetchPaths();
19+
for (const lang of config.i18n.locales) {
20+
const paths = await fetchPaths(lang);
2021
21-
_.each(locales, (lang) => {
22-
_.each(paths, (path) => {
23-
staticPaths.push({ params: {
24-
lang,
25-
slug: path?._sys.filename
26-
}})
27-
});
28-
});
22+
for (const path of paths) {
23+
paths.push({ params: { lang, slug: path._sys.filename } });
24+
}
25+
}
2926
30-
return staticPaths;
27+
return paths;
3128
};
3229
3330
---

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getRelativeLocaleUrl } from 'astro:i18n';
1010
import _ from 'underscore';
1111
1212
const { t } = await getTranslations(Astro.currentLocale);
13-
const paths = await fetchPaths();
13+
const paths = await fetchPaths(Astro.currentLocale);
1414
1515
export const getStaticPaths = () => _.map(config.i18n.locales, (lang) => ({ params: { lang }}));
1616
@@ -31,15 +31,15 @@ export const getStaticPaths = () => _.map(config.i18n.locales, (lang) => ({ para
3131
{ t('paths') }
3232
</h1>
3333
<Cards>
34-
{ _.map(paths, (path) => (
34+
{ paths?.length ? _.map(paths, (path) => (
3535
<Card
3636
imageUrl={path?.image}
3737
alt={path?.imageAlt}
3838
slug={getRelativeLocaleUrl(Astro.currentLocale, `/paths/${path?._sys.filename}` || '#')}
3939
t={t}
4040
title={path?.title || t('untitled')}
4141
/>
42-
))}
42+
)) : <p class="italic text-sm">({ t('none') })</p> }
4343
</Cards>
4444
</Container>
4545
</Layout>

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,24 @@ import Layout from '@layouts/Layout.astro';
99
import _ from 'underscore';
1010
1111
const { slug } = Astro.params;
12-
const post = await fetchPost(slug);
1312
1413
const locale = Astro.currentLocale;
1514
16-
const localePost = getLocalizedContent(post, locale.replaceAll('-', '_')) //this is to deal with Tina's field naming restrictions
17-
15+
const post = await fetchPost(locale, slug);
1816
const { t } = await getTranslations(locale);
1917
2018
export const getStaticPaths = async () => {
21-
const staticPaths = [];
19+
const paths = [];
2220
23-
const locales = config.i18n.locales;
24-
const posts = await fetchPosts();
21+
for (const lang of config.i18n.locales) {
22+
const posts = await fetchPosts(lang);
2523
26-
_.each(locales, (lang) => {
27-
_.each(posts, (post) => {
28-
staticPaths.push({ params: {
29-
lang,
30-
slug: post?._sys.filename
31-
}});
32-
});
33-
});
24+
for (const post of posts) {
25+
paths.push({ params: { lang, slug: post._sys.filename } });
26+
}
27+
}
3428
35-
return staticPaths;
29+
return paths;
3630
};
3731
3832
---
@@ -41,14 +35,14 @@ export const getStaticPaths = async () => {
4135
footer
4236
t={t}
4337
tab='posts'
44-
title={localePost?.title}
38+
title={post?.title}
4539
>
4640
<Container
4741
className='pb-16 w-full'
4842
>
4943
<PostContent
50-
content={localePost?.body}
51-
title={localePost?.title}
44+
content={post?.body}
45+
title={post?.title}
5246
client:only='react'
5347
/>
5448
</Container>

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@ 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';
98
import Layout from '@layouts/Layout.astro';
109
import { getRelativeLocaleUrl } from 'astro:i18n';
1110
import _ from 'underscore';
1211
1312
const { t } = await getTranslations(Astro.currentLocale);
14-
const posts = await fetchPosts();
13+
const posts = await fetchPosts(Astro.currentLocale);
1514
1615
export const getStaticPaths = () => _.map(config.i18n.locales, (lang) => ({ params: { lang }}));
1716
18-
//Localize the post titles
19-
const localeField = Astro.currentLocale.replaceAll('-', '_') //this is to deal with Tina's field naming conventions
20-
const localizedPosts = _.map(posts, (post: any) => (getLocalizedContent(post, localeField)))
21-
2217
---
2318

2419
<Layout
@@ -36,15 +31,15 @@ const localizedPosts = _.map(posts, (post: any) => (getLocalizedContent(post, lo
3631
{ t('posts') }
3732
</h1>
3833
<Cards>
39-
{ _.map(localizedPosts, (post) => (
34+
{ posts?.length ? _.map(posts, (post) => (
4035
<Card
4136
imageUrl={post?.cardImage}
4237
alt={post?.imageAlt}
4338
slug={getRelativeLocaleUrl(Astro.currentLocale, `posts/${post._sys.filename}`)}
4439
t={t}
4540
title={post.title}
4641
/>
47-
))}
42+
)) : <p class="italic text-sm">({ t('none') })</p>}
4843
</Cards>
4944
</Container>
5045
</Layout>

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export interface SearchConfig {
5252
export interface Configuration {
5353
content?: {
5454
collections?: Array<String>,
55-
localize_pages?: boolean
55+
localize_content?: boolean
5656
};
5757

5858
core_data: {

src/utils/nav.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { fetchPages } from '@backend/tina';
2+
import config from '@config';
23
import _ from 'underscore';
34

45
/**
5-
* Returns the first page flagged as "home_page".
6+
* Returns the first page flagged as "home_page". If none exists for the current locale, will default to the configured `default_locale` if possible.
67
*
78
* @param locale
89
*/
910
export const getHomepage = async (locale: string) => {
1011
const pages = await fetchPages(locale, { filter: { home_page: { eq: true } } });
11-
const [page, ] = pages;
12+
let page: any;
13+
if (pages && pages.length) {
14+
page = pages[0];
15+
} else if (config.i18n.default_locale) {
16+
const defaultPages = await fetchPages(config.i18n.default_locale, { filter: { home_page: { eq: true } } });
17+
if (defaultPages && defaultPages.length) {
18+
page = defaultPages[0];
19+
}
20+
}
1221

1322
return page;
1423
};

0 commit comments

Comments
 (0)