-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow SSR and SSG with getLocaleProps (#25)
- Loading branch information
Showing
8 changed files
with
98 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import { createI18n } from 'next-international'; | ||
import type Locale from './en'; | ||
|
||
export const { useI18n, I18nProvider, useChangeLocale, defineLocale, getLocaleStaticProps } = createI18n<typeof Locale>( | ||
{ | ||
en: () => import('./en'), | ||
fr: () => import('./fr'), | ||
}, | ||
); | ||
export const { useI18n, I18nProvider, useChangeLocale, defineLocale, getLocaleProps } = createI18n<typeof Locale>({ | ||
en: () => import('./en'), | ||
fr: () => import('./fr'), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react'; | ||
import { GetServerSideProps } from 'next'; | ||
import { getLocaleProps, useChangeLocale, useI18n } from '../locales'; | ||
|
||
const Home = () => { | ||
const { t, scopedT } = useI18n(); | ||
const changeLocale = useChangeLocale(); | ||
const t2 = scopedT('scope.more'); | ||
|
||
return ( | ||
<div> | ||
<h1>SSR</h1> | ||
<p>Hello: {t('hello')}</p> | ||
<p> | ||
Hello:{' '} | ||
{t('welcome', { | ||
name: 'John', | ||
})} | ||
</p> | ||
<p> | ||
Hello:{' '} | ||
{t('about.you', { | ||
age: '23', | ||
name: 'Doe', | ||
})} | ||
</p> | ||
<p>{t2('test')}</p> | ||
<p> | ||
{t2('param', { | ||
param: 'test', | ||
})} | ||
</p> | ||
<p>{t2('and.more.test')}</p> | ||
<p>{t('missing.translation.in.fr')}</p> | ||
<button type="button" onClick={() => changeLocale('en')}> | ||
EN | ||
</button> | ||
<button type="button" onClick={() => changeLocale('fr')}> | ||
FR | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
// Comment this to disable SSR of initial locale | ||
export const getServerSideProps: GetServerSideProps = getLocaleProps(); | ||
|
||
export default Home; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
packages/next-international/src/i18n/create-get-locale-static-props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters