Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store lang in context and refer from descendants #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/app/[lang]/_components/i18n/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client'

import { type FC, type ReactNode, createContext, useContext } from 'react'

export const LangContext = createContext<string>('')

type LangContextProviderProps = Readonly<{
lang: string
children: ReactNode
}>

export const LangContextProvider: FC<LangContextProviderProps> = ({ lang, children }) => (
<LangContext.Provider value={lang}>{children}</LangContext.Provider>
)
16 changes: 16 additions & 0 deletions src/app/[lang]/sample/_components/Birthday/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use client'

import { type FC, use, useContext } from 'react'

import { LangContext } from '@/app/[lang]/_components/i18n'
import { useTranslation } from '@/app/_hooks/i18n'

import translationDef from './translationDef'

export const Birthday: FC = () => {
const lang = useContext(LangContext)
// devサーバーだとめちゃくちゃログが流れるのでなにか間違ってる気もする
const { t } = use(useTranslation({ lang, translationDef }))

return <p style={{ color: 'blue' }}>{t('myBirthday')}</p>
}
15 changes: 15 additions & 0 deletions src/app/[lang]/sample/_components/Birthday/translationDef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const ja = {
translation: {
myBirthday: '私の誕生日は11月21日です。',
},
} as const

const en = {
translation: {
myBirthday: 'My birthday is on Nov. 21st.',
},
} as const

const translationDef = { ja, en } as const

export default translationDef
13 changes: 9 additions & 4 deletions src/app/[lang]/sample/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { nonDefaultLanguages, useTranslation } from '../../_hooks/i18n'
import { LangContextProvider } from '../_components/i18n'
import { Birthday } from './_components/Birthday'
import translationDef from './translationDef'

export const generateStaticParams = async () => nonDefaultLanguages.map(lang => ({ lang }))
Expand All @@ -13,9 +15,12 @@ export default async function SamplePage({ params }: SamplePageProps) {
const { lang } = params
const { t } = await useTranslation({ lang, translationDef })
return (
<main>
<h1>{t('title')}</h1>
<p>{t('myLanguage')}</p>
</main>
<LangContextProvider lang={lang}>
<main>
<h1>{t('title')}</h1>
<p>{t('myLanguage')}</p>
<Birthday />
</main>
</LangContextProvider>
)
}