Skip to content

Commit

Permalink
Multilingual (i18n) Blog Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
hlog-kr committed Sep 30, 2023
1 parent 28d6de4 commit b244525
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 4 deletions.
28 changes: 28 additions & 0 deletions components/LanguageSwitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useRouter } from 'next/router'
import Link from 'next/link'

const LanguageSwitch = () => {
const { locales, locale, asPath } = useRouter()

if (!locales) {
return null
}

return (
<div className="rounded bg-gray-200 py-2 dark:bg-gray-800">
{locales.map((item) => (
<Link key={item} locale={item} href={asPath}>
<a
className={`p-2 font-medium text-gray-900 dark:text-gray-100 ${
item === locale ? 'rounded bg-gray-400 dark:bg-gray-600' : ''
}`}
>
{item}
</a>
</Link>
))}
</div>
)
}

export default LanguageSwitch
2 changes: 2 additions & 0 deletions components/LayoutWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Footer from './Footer'
import MobileNav from './MobileNav'
import ThemeSwitch from './ThemeSwitch'
import GAdsBanner from '@/components/GAdsBanner'
import LanguageSwitch from '@/components/LanguageSwitch'

const LayoutWrapper = ({ children }) => {
return (
Expand Down Expand Up @@ -41,6 +42,7 @@ const LayoutWrapper = ({ children }) => {
</Link>
))}
</div>
<LanguageSwitch />
<ThemeSwitch />
<MobileNav />
</div>
Expand Down
7 changes: 7 additions & 0 deletions next-i18next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('next-i18next').UserConfig} */
module.exports = {
i18n: {
defaultLocale: 'ko',
locales: ['ko', 'en'],
},
}
3 changes: 3 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { i18n } = require('./next-i18next.config')

const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
Expand Down Expand Up @@ -53,6 +55,7 @@ const securityHeaders = [
]

module.exports = withBundleAnalyzer({
i18n,
reactStrictMode: true,
pageExtensions: ['js', 'jsx', 'md', 'mdx'],
eslint: {
Expand Down
5 changes: 4 additions & 1 deletion pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import siteMetadata from '@/data/siteMetadata'
import Analytics from '@/components/analytics'
import LayoutWrapper from '@/components/LayoutWrapper'
import { ClientReload } from '@/components/ClientReload'
import { appWithTranslation } from 'next-i18next'

const isDevelopment = process.env.NODE_ENV === 'development'
const isSocket = process.env.SOCKET

export default function App({ Component, pageProps }) {
function App({ Component, pageProps }) {
return (
<ThemeProvider attribute="class" defaultTheme={siteMetadata.theme}>
<Head>
Expand All @@ -30,3 +31,5 @@ export default function App({ Component, pageProps }) {
</ThemeProvider>
)
}

export default appWithTranslation(App)
16 changes: 13 additions & 3 deletions pages/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@ import { getAllFilesFrontMatter } from '@/lib/mdx'
import siteMetadata from '@/data/siteMetadata'
import ListLayout from '@/layouts/ListLayout'
import { PageSEO } from '@/components/SEO'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { useTranslation } from 'next-i18next'

export const POSTS_PER_PAGE = 5

export async function getStaticProps() {
export async function getStaticProps({ locale }) {
const posts = await getAllFilesFrontMatter('blog')
const initialDisplayPosts = posts.slice(0, POSTS_PER_PAGE)
const pagination = {
currentPage: 1,
totalPages: Math.ceil(posts.length / POSTS_PER_PAGE),
}

return { props: { initialDisplayPosts, posts, pagination } }
return {
props: {
...(await serverSideTranslations(locale, ['common'])),
initialDisplayPosts,
posts,
pagination,
},
}
}

export default function Blog({ posts, initialDisplayPosts, pagination }) {
const { t } = useTranslation('common')
return (
<>
<PageSEO title={`Blog - ${siteMetadata.author}`} description={siteMetadata.description} />
<ListLayout
posts={posts}
initialDisplayPosts={initialDisplayPosts}
pagination={pagination}
title="All Posts"
title={t('all')}
/>
</>
)
Expand Down
3 changes: 3 additions & 0 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"all": "All Posts"
}
3 changes: 3 additions & 0 deletions public/locales/ko/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"all": "전체 글"
}

0 comments on commit b244525

Please sign in to comment.