-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(examples/with-lingui): update example (#26076)
Fixes one of the items from #25854 ## Documentation / Examples - [x] Make sure the linting passes - [x] Add the StackBlitz button - [x] [Live demo](https://stackblitz.com/github/akellbl4/next.js/tree/examples/with-lingui-update/examples/with-lingui)
- Loading branch information
Pavel Mineev
authored
Jun 17, 2021
1 parent
ef379f1
commit 9785ba8
Showing
11 changed files
with
104 additions
and
114 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
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,44 +1,21 @@ | ||
import Router from 'next/router' | ||
import { I18n } from '@lingui/react' | ||
import { t, Trans } from '@lingui/macro' | ||
import Link from 'next/link' | ||
import { useRouter } from 'next/router' | ||
import { t } from '@lingui/macro' | ||
|
||
const availableLanguageNames = { | ||
en: t`English`, | ||
sv: t`Swedish`, | ||
} | ||
const availableLanguages = Object.keys(availableLanguageNames) | ||
|
||
export default function LangSwitcher() { | ||
function onSubmit(evt) { | ||
evt.preventDefault() | ||
Router.push({ | ||
pathname: window.location.pathname, | ||
query: { lang: evt.currentTarget.lang.value }, | ||
}) | ||
} | ||
const { locale, locales, route } = useRouter() | ||
const otherLocale = locales?.find((cur) => cur !== locale) | ||
|
||
return ( | ||
<I18n> | ||
{({ i18n }) => ( | ||
<form onSubmit={onSubmit}> | ||
<select | ||
key={i18n.language} | ||
name="lang" | ||
defaultValue={availableLanguages.find( | ||
(lang) => lang !== i18n.language | ||
)} | ||
> | ||
{availableLanguages.map((lang) => ( | ||
<option key={lang} value={lang} disabled={i18n.language === lang}> | ||
{i18n._(availableLanguageNames[lang])} | ||
</option> | ||
))} | ||
</select> | ||
<button> | ||
<Trans>Switch language</Trans> | ||
</button> | ||
</form> | ||
)} | ||
</I18n> | ||
<Link href={route} locale={otherLocale}> | ||
<a style={{ display: 'block', marginBottom: '20px' }}> | ||
{availableLanguageNames[otherLocale]} | ||
</a> | ||
</Link> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
module.exports = { | ||
locales: ['en', 'sv'], | ||
sourceLocale: 'en', | ||
catalogs: [ | ||
{ | ||
path: '<rootDir>/locale/{locale}/messages', | ||
include: ['<rootDir>/'], | ||
exclude: ['**/node_modules/**'], | ||
}, | ||
], | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
const { locales, sourceLocale } = require('./lingui.config.js') | ||
|
||
module.exports = { | ||
webpack: (config, options) => { | ||
i18n: { | ||
locales, | ||
defaultLocale: sourceLocale, | ||
}, | ||
webpack: (config) => { | ||
config.module.rules.push({ | ||
test: /\.po/, | ||
use: [ | ||
{ | ||
loader: '@lingui/loader', | ||
}, | ||
], | ||
use: ['@lingui/loader'], | ||
}) | ||
|
||
return config | ||
}, | ||
} |
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,34 +1,26 @@ | ||
{ | ||
"name": "with-lingui", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "next", | ||
"build": "lingui compile && next build", | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start", | ||
"add-locale": "lingui add-locale", | ||
"extract": "lingui extract" | ||
"extract": "lingui extract", | ||
"compile": "lingui compile" | ||
}, | ||
"dependencies": { | ||
"@lingui/react": "latest", | ||
"@lingui/react": "^3.10.2", | ||
"next": "latest", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2" | ||
}, | ||
"devDependencies": { | ||
"@lingui/cli": "latest", | ||
"@lingui/loader": "latest", | ||
"@lingui/macro": "latest", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-plugin-macros": "^2.4.0" | ||
}, | ||
"lingui": { | ||
"sourceLocale": "en", | ||
"localeDir": "./locale", | ||
"srcPathDirs": [ | ||
"./pages/", | ||
"./components/" | ||
], | ||
"format": "po" | ||
}, | ||
"license": "MIT" | ||
"@babel/core": "7.14.5", | ||
"@lingui/cli": "^3.10.2", | ||
"@lingui/core": "3.10.2", | ||
"@lingui/loader": "3.10.2", | ||
"@lingui/macro": "^3.10.2", | ||
"babel-plugin-macros": "^3.1.0" | ||
} | ||
} |
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,34 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { I18nProvider } from '@lingui/react' | ||
import { i18n } from '@lingui/core' | ||
import { useRouter } from 'next/router' | ||
import { en, sv } from 'make-plural/plurals' | ||
import LangSwitcher from '../components/LangSwitcher' | ||
|
||
i18n.loadLocaleData('en', { plurals: en }) | ||
i18n.loadLocaleData('sv', { plurals: sv }) | ||
|
||
export default function Page({ Component, pageProps }) { | ||
const { locale } = useRouter() | ||
|
||
useEffect(() => { | ||
async function load(locale) { | ||
const { messages } = await import(`../locale/${locale}/messages.po`) | ||
|
||
i18n.load(locale, messages) | ||
i18n.activate(locale) | ||
} | ||
|
||
load(locale) | ||
}, [locale]) | ||
|
||
return ( | ||
<I18nProvider i18n={i18n}> | ||
<div> | ||
<LangSwitcher /> | ||
<Component {...pageProps} /> | ||
</div> | ||
</I18nProvider> | ||
) | ||
} |
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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { Trans } from '@lingui/macro' | ||
import withLang from '../components/withLang' | ||
import LangSwitcher from '../components/LangSwitcher' | ||
import Link from 'next/link' | ||
|
||
const Two = () => ( | ||
<div> | ||
<Trans>Page two. </Trans> | ||
<Trans>Page two.</Trans>{' '} | ||
<Link href="/"> | ||
<a> | ||
<Trans>Back home</Trans> | ||
</a> | ||
</Link> | ||
<br /> | ||
<LangSwitcher /> | ||
</div> | ||
) | ||
|
||
export default withLang(Two) | ||
export default Two |