Unverified Commit 9785ba8e authored by Pavel Mineev's avatar Pavel Mineev Committed by GitHub

(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)
parent ef379f15
...@@ -6,6 +6,12 @@ It adds a webpack loader for the messages to avoid having to manually compile wh ...@@ -6,6 +6,12 @@ It adds a webpack loader for the messages to avoid having to manually compile wh
The example also uses a Higher order Component which can be added to all pages which will be translated and that checks for a `?lang` query string switch the language. Next.js will dynamically load in the messages for the locale when navigating using a Next.js `<Link />` component. The example also uses a Higher order Component which can be added to all pages which will be translated and that checks for a `?lang` query string switch the language. Next.js will dynamically load in the messages for the locale when navigating using a Next.js `<Link />` component.
## Preview
Preview the example live on [StackBlitz](http://stackblitz.com/):
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-lingui)
## Deploy your own ## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
......
import Router from 'next/router' import Link from 'next/link'
import { I18n } from '@lingui/react' import { useRouter } from 'next/router'
import { t, Trans } from '@lingui/macro' import { t } from '@lingui/macro'
const availableLanguageNames = { const availableLanguageNames = {
en: t`English`, en: t`English`,
sv: t`Swedish`, sv: t`Swedish`,
} }
const availableLanguages = Object.keys(availableLanguageNames)
export default function LangSwitcher() { export default function LangSwitcher() {
function onSubmit(evt) { const { locale, locales, route } = useRouter()
evt.preventDefault() const otherLocale = locales?.find((cur) => cur !== locale)
Router.push({
pathname: window.location.pathname,
query: { lang: evt.currentTarget.lang.value },
})
}
return ( return (
<I18n> <Link href={route} locale={otherLocale}>
{({ i18n }) => ( <a style={{ display: 'block', marginBottom: '20px' }}>
<form onSubmit={onSubmit}> {availableLanguageNames[otherLocale]}
<select </a>
key={i18n.language} </Link>
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>
) )
} }
import React from 'react'
import { I18nProvider } from '@lingui/react'
export default function withLang(Component, defaultLang = 'en') {
return class WithLang extends React.Component {
static async getInitialProps(ctx) {
const language = ctx.query.lang || defaultLang
const [props, catalog] = await Promise.all([
Component.getInitialProps ? Component.getInitialProps(ctx) : {},
import(`../locale/${language}/messages.po`).then((m) => m.default),
])
return {
...props,
language,
catalogs: {
[language]: catalog,
},
}
}
render() {
const { language, catalogs, ...restProps } = this.props
return (
<I18nProvider language={language} catalogs={catalogs}>
<Component {...restProps} />
</I18nProvider>
)
}
}
}
module.exports = {
locales: ['en', 'sv'],
sourceLocale: 'en',
catalogs: [
{
path: '<rootDir>/locale/{locale}/messages',
include: ['<rootDir>/'],
exclude: ['**/node_modules/**'],
},
],
}
...@@ -13,6 +13,10 @@ msgstr "" ...@@ -13,6 +13,10 @@ msgstr ""
"Language-Team: \n" "Language-Team: \n"
"Plural-Forms: \n" "Plural-Forms: \n"
#: pages/two.js:9
msgid "Back home"
msgstr "Back home"
#: components/LangSwitcher.js:6 #: components/LangSwitcher.js:6
msgid "English" msgid "English"
msgstr "English" msgstr "English"
...@@ -21,18 +25,14 @@ msgstr "English" ...@@ -21,18 +25,14 @@ msgstr "English"
msgid "Go to page 2" msgid "Go to page 2"
msgstr "Go to page 2" msgstr "Go to page 2"
#: pages/index.js:8 #: pages/index.js:6
msgid "Hello World." msgid "Hello World."
msgstr "Hello World. " msgstr "Hello World. "
#: pages/two.js:8 #: pages/two.js:6
msgid "Page two." msgid "Page two."
msgstr "Page two." msgstr "Page two."
#: components/LangSwitcher.js:7 #: components/LangSwitcher.js:7
msgid "Swedish" msgid "Swedish"
msgstr "Swedish" msgstr "Swedish"
#: components/LangSwitcher.js:26
msgid "Switch language"
msgstr "Switch language"
...@@ -13,6 +13,10 @@ msgstr "" ...@@ -13,6 +13,10 @@ msgstr ""
"Language-Team: \n" "Language-Team: \n"
"Plural-Forms: \n" "Plural-Forms: \n"
#: pages/two.js:9
msgid "Back home"
msgstr "Tillbaka hem"
#: components/LangSwitcher.js:6 #: components/LangSwitcher.js:6
msgid "English" msgid "English"
msgstr "Engelska" msgstr "Engelska"
...@@ -21,19 +25,14 @@ msgstr "Engelska" ...@@ -21,19 +25,14 @@ msgstr "Engelska"
msgid "Go to page 2" msgid "Go to page 2"
msgstr "Gå till sida 2" msgstr "Gå till sida 2"
#: pages/index.js:8 #: pages/index.js:6
msgid "Hello World." msgid "Hello World."
msgstr "Hej Världen. " msgstr "Hej Världen. "
#: pages/two.js:8 #: pages/two.js:6
msgid "Page two." msgid "Page two."
msgstr "Sida två." msgstr "Sida två."
#: components/LangSwitcher.js:7 #: components/LangSwitcher.js:7
msgid "Swedish" msgid "Swedish"
msgstr "Svenska" msgstr "Svenska"
#: components/LangSwitcher.js:26
msgid "Switch language"
msgstr "Byt Språk"
const { locales, sourceLocale } = require('./lingui.config.js')
module.exports = { module.exports = {
webpack: (config, options) => { i18n: {
locales,
defaultLocale: sourceLocale,
},
webpack: (config) => {
config.module.rules.push({ config.module.rules.push({
test: /\.po/, test: /\.po/,
use: [ use: ['@lingui/loader'],
{
loader: '@lingui/loader',
},
],
}) })
return config return config
}, },
} }
{ {
"name": "with-lingui", "name": "with-lingui",
"version": "1.0.0", "version": "1.0.0",
"license": "MIT",
"scripts": { "scripts": {
"dev": "next", "dev": "next dev",
"build": "lingui compile && next build", "build": "next build",
"start": "next start", "start": "next start",
"add-locale": "lingui add-locale", "extract": "lingui extract",
"extract": "lingui extract" "compile": "lingui compile"
}, },
"dependencies": { "dependencies": {
"@lingui/react": "latest", "@lingui/react": "^3.10.2",
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2" "react-dom": "^17.0.2"
}, },
"devDependencies": { "devDependencies": {
"@lingui/cli": "latest", "@babel/core": "7.14.5",
"@lingui/loader": "latest", "@lingui/cli": "^3.10.2",
"@lingui/macro": "latest", "@lingui/core": "3.10.2",
"babel-core": "^7.0.0-bridge.0", "@lingui/loader": "3.10.2",
"babel-plugin-macros": "^2.4.0" "@lingui/macro": "^3.10.2",
}, "babel-plugin-macros": "^3.1.0"
"lingui": { }
"sourceLocale": "en",
"localeDir": "./locale",
"srcPathDirs": [
"./pages/",
"./components/"
],
"format": "po"
},
"license": "MIT"
} }
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>
)
}
import Link from 'next/link' import Link from 'next/link'
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import withLang from '../components/withLang'
import LangSwitcher from '../components/LangSwitcher'
const Index = () => ( const Index = () => (
<div> <div>
...@@ -12,8 +10,7 @@ const Index = () => ( ...@@ -12,8 +10,7 @@ const Index = () => (
</a> </a>
</Link> </Link>
<br /> <br />
<LangSwitcher />
</div> </div>
) )
export default withLang(Index) export default Index
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import withLang from '../components/withLang' import Link from 'next/link'
import LangSwitcher from '../components/LangSwitcher'
const Two = () => ( const Two = () => (
<div> <div>
<Trans>Page two. </Trans> <Trans>Page two.</Trans>{' '}
<Link href="/">
<a>
<Trans>Back home</Trans>
</a>
</Link>
<br /> <br />
<LangSwitcher />
</div> </div>
) )
export default withLang(Two) export default Two
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment