Skip to content

Commit ea800fe

Browse files
top image created
1 parent 848a3ec commit ea800fe

File tree

12 files changed

+123
-97
lines changed

12 files changed

+123
-97
lines changed

public/images/translate.png

165 KB
Loading

src/content/accessing-i18next-translation-from-json-keys-instead-of-string-path.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ i18next.t('homePage.header.buttons.signIn.title');
6363

6464
As you can see, by having the **translation key as a string**, we are losing all benefits described above.
6565

66+
## Solution
67+
6668
What we want to achieve is something that looks like this:
6769

6870
```ts
@@ -126,6 +128,8 @@ const translationKeys = getTranslationKeys(en);
126128
const value = i18next.t(translationKeys.homePage.header.buttons.signIn.title);
127129
```
128130

131+
## End note
132+
129133
Finally, we've reached the goal.
130134
Now the TypeScript and IDE tools are active when we reference the translation keys. And we can trust our code more.
131135

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getPostDescriptions } from '../post/post.api';
2+
import { GetStaticProps } from 'next';
3+
import { HomeProps } from './home.page';
4+
5+
export const getStaticProps: GetStaticProps<HomeProps> = async () => {
6+
const postDescriptions = getPostDescriptions();
7+
return {
8+
props: {
9+
postDescriptions,
10+
},
11+
};
12+
};

src/pages-internal/home/home.page.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import renderer from 'react-test-renderer';
2-
import HomePage from './home.page';
2+
import { HomePage } from './home.page';
33

44
describe('HomePage', () => {
55
it('should match the snapshot', () => {

src/pages-internal/home/home.page.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import Head from 'next/head';
22
import { Layout } from '../../components/layout';
3-
import { getPostDescriptions } from '../post/post.logic';
43
import Link from 'next/link';
54
import { Date } from '../../components/date';
6-
import { GetStaticProps } from 'next';
75
import React from 'react';
86
import { PostDescription } from '../post/post.model';
97
import { translate, translationKeys } from '../../logic/translations/translation.service';
108

11-
interface HomeProps {
9+
export interface HomeProps {
1210
postDescriptions: PostDescription[];
1311
}
1412

@@ -43,14 +41,3 @@ export const HomePage: React.FC<HomeProps> = ({ postDescriptions }) => {
4341
</Layout>
4442
);
4543
};
46-
47-
export const getStaticProps: GetStaticProps<HomeProps> = async () => {
48-
const postDescriptions = getPostDescriptions();
49-
return {
50-
props: {
51-
postDescriptions,
52-
},
53-
};
54-
};
55-
56-
export default HomePage;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
3+
import { Components } from 'react-markdown/src/ast-to-react';
4+
5+
export const markDownComponents: Components = {
6+
code({ inline, className, children, ...props }) {
7+
const match = /language-(\w+)/.exec(className || '');
8+
return !inline && match ? (
9+
<SyntaxHighlighter language={match[1]} PreTag="div" {...props}>
10+
{String(children).replace(/\n$/, '')}
11+
</SyntaxHighlighter>
12+
) : (
13+
<code className={className} {...props} />
14+
);
15+
},
16+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { GetStaticPaths, GetStaticProps } from 'next';
2+
import { getPostData, getPostIds } from './post.api';
3+
4+
export const getStaticPaths: GetStaticPaths = async () => {
5+
const paths = getPostIds();
6+
return {
7+
paths,
8+
fallback: false,
9+
};
10+
};
11+
12+
export const getStaticProps: GetStaticProps = async ({ params }) => {
13+
if (!params || !params.id || Array.isArray(params.id)) return { props: {} };
14+
15+
const postData = await getPostData(params.id);
16+
17+
return {
18+
props: {
19+
postData,
20+
},
21+
};
22+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Layout } from '../../components/layout';
2+
import Head from 'next/head';
3+
import { Date } from '../../components/date';
4+
import React from 'react';
5+
import { Post } from './post.model';
6+
import styled from 'styled-components';
7+
import ReactMarkdown from 'react-markdown';
8+
import Image from 'next/image';
9+
import { markDownComponents } from './post.components';
10+
11+
interface PostPageProps {
12+
postData: Post;
13+
}
14+
15+
export const PostPage: React.FC<PostPageProps> = ({ postData }) => {
16+
return (
17+
<Layout>
18+
<Head>
19+
<title>{postData.title}</title>
20+
</Head>
21+
<article>
22+
<PostHeader>
23+
<Title>{postData.title}</Title>
24+
{postData.date && (
25+
<div>
26+
<Date date={postData.date} />
27+
</div>
28+
)}
29+
<PostImage priority src="/images/translate.png" height={682 / 2} width={680} alt={''} />
30+
</PostHeader>
31+
<PostContent>
32+
<ReactMarkdown components={markDownComponents}>{postData.content || ''}</ReactMarkdown>
33+
</PostContent>
34+
</article>
35+
</Layout>
36+
);
37+
};
38+
39+
const Title = styled.h1`
40+
font-size: 36px;
41+
font-weight: 400;
42+
`;
43+
44+
const PostContent = styled.div`
45+
font-weight: 100;
46+
line-height: 1.8;
47+
letter-spacing: -0.5px;
48+
text-align: justify;
49+
`;
50+
51+
const PostHeader = styled.div`
52+
display: flex;
53+
flex-direction: column;
54+
flex: 1;
55+
text-align: center;
56+
`;
57+
58+
const PostImage = styled(Image)`
59+
border-width: 1px;
60+
border-color: black;
61+
border-style: solid;
62+
object-fit: cover;
63+
`;

src/pages-internal/post/post.tsx

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)