Skip to content

Commit 894c877

Browse files
examples: migrate examples to v4 (#1674)
1 parent 3303228 commit 894c877

File tree

10 files changed

+184
-172
lines changed

10 files changed

+184
-172
lines changed

examples/create-react-app/src/App.tsx

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
1-
import './App.css';
2-
import React, { useState } from 'react';
3-
import { Trans, Plural } from "@lingui/macro";
4-
import { i18n } from "@lingui/core";
1+
import "./App.css"
2+
import React, { useState } from "react"
3+
import { Trans, Plural } from "@lingui/macro"
54

6-
import { locales, dynamicActivate } from './i18n';
5+
import { locales, dynamicActivate } from "./i18n"
6+
import { useLingui } from "@lingui/react"
77

88
function App() {
9-
const [count, setCount] = useState(0);
9+
const [count, setCount] = useState(0)
10+
const { i18n } = useLingui()
1011
return (
1112
<div className="App">
1213
<header className="App-header">
13-
<img className="App-logo" src="https://avatars3.githubusercontent.com/u/11225539?s=200&v=4" />
14-
<h3 data-testid="h3-title"><Trans>Language switcher example: </Trans></h3>
14+
<img
15+
className="App-logo"
16+
src="https://avatars3.githubusercontent.com/u/11225539?s=200&v=4"
17+
/>
18+
<h3 data-testid="h3-title">
19+
<Trans>Language switcher example: </Trans>
20+
</h3>
1521
<div className="lang-container">
1622
{Object.values(locales).map((locale, index) => (
17-
<button type="button" onClick={() => dynamicActivate(Object.keys(locales)[index])} key={locale}>
23+
<button
24+
type="button"
25+
onClick={() => dynamicActivate(Object.keys(locales)[index])}
26+
key={locale}
27+
>
1828
{locale}
1929
</button>
2030
))}
2131
</div>
22-
<h3><Trans>Plurals example: </Trans></h3>
32+
<h3>
33+
<Trans>Plurals example: </Trans>
34+
</h3>
2335
<div className="lang-container">
24-
<button type="button" onClick={() => setCount(state => state + 1)}>
36+
<button type="button" onClick={() => setCount((state) => state + 1)}>
2537
<Trans>Increment</Trans>
2638
</button>
27-
<button type="button" onClick={() => setCount(state => state - 1)}>
39+
<button type="button" onClick={() => setCount((state) => state - 1)}>
2840
<Trans>Decrement</Trans>
2941
</button>
3042
</div>
@@ -34,21 +46,24 @@ function App() {
3446
one={"There's one book"}
3547
other={"There are # books"}
3648
/>
37-
<h3><Trans>Date formatter example:</Trans></h3>
49+
<h3>
50+
<Trans>Date formatter example:</Trans>
51+
</h3>
3852
<div>
39-
<Trans>
40-
Today is {i18n.date(new Date(), {})}
41-
</Trans>
53+
<Trans>Today is {i18n.date(new Date(), {})}</Trans>
4254
</div>
43-
<h3><Trans>Number formatter example:</Trans></h3>
55+
<h3>
56+
<Trans>Number formatter example:</Trans>
57+
</h3>
4458
<div>
4559
<Trans>
46-
I have a balance of {i18n.number(1_000_000, { style: "currency", currency: "EUR" })}
60+
I have a balance of{" "}
61+
{i18n.number(1_000_000, { style: "currency", currency: "EUR" })}
4762
</Trans>
4863
</div>
4964
</header>
5065
</div>
51-
);
66+
)
5267
}
5368

54-
export default App;
69+
export default App

examples/nextjs-swc/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
44

5+
## SWC Compatibility
6+
SWC Plugin support is still experimental. Semver backwards compatibility between different `next-swc` versions is not guaranteed.
7+
8+
Therefore, you need to select an appropriate version of the Lingui plugin to match compatible `NextJs` version.
9+
10+
For more information on compatibility, please refer to the [Compatibility section](https://github.com/lingui/swc-plugin#compatibility).
11+
512
## Getting Started
613

714
First, run the development server:

examples/nextjs-swc/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
"debug": "NODE_OPTIONS='--inspect' next dev",
88
"build": "yarn lang:extract && next build",
99
"start": "next start",
10-
"lang:extract": "lingui extract --clean",
10+
"lingui:extract": "lingui extract --clean",
1111
"test": "yarn build"
1212
},
1313
"dependencies": {
1414
"@lingui/core": "^4.1.2",
1515
"@lingui/react": "^4.1.2",
16-
"next": "13.2.3",
16+
"next": "13.3.1",
1717
"react": "18.2.0",
1818
"react-dom": "18.2.0"
1919
},

examples/nextjs-swc/src/components/Developers.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useState } from 'react'
2-
import { Trans, plural } from '@lingui/macro'
2+
import { Trans, Plural } from '@lingui/macro'
33

44
export default function Developers() {
55
const [selected, setSelected] = useState('1')
@@ -17,10 +17,7 @@ export default function Developers() {
1717
<option value={'2'}>2</option>
1818
</select>
1919
<p>
20-
{plural(selected, {
21-
one: 'Developer',
22-
other: `Developers`
23-
})}
20+
<Plural value={selected} one={'Developer'} other={`Developers`} />
2421
</p>
2522
</div>
2623
</div>

examples/nextjs-swc/src/locales/en.po

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,35 @@ msgstr ""
1313
"Language-Team: \n"
1414
"Plural-Forms: \n"
1515

16-
#: src/components/Switcher.tsx:14
16+
#: src/components/Developers.tsx:20
17+
msgid "{selected, plural, one {Developer} other {Developers}}"
18+
msgstr "{selected, plural, one {Developer} other {Developers}}"
19+
20+
#: src/components/Switcher.tsx:10
1721
msgid "English"
1822
msgstr "English"
1923

24+
#. js-lingui-explicit-id
25+
#: src/components/AboutText.tsx:6
26+
msgid "next-explanation"
27+
msgstr "Next.js is an open-source React front-end development web framework that enables functionality such as server-side rendering and generating static websites for React based web applications. It is a production-ready framework that allows developers to quickly create static and dynamic JAMstack websites and is used widely by many large companies."
28+
2029
#: src/components/Developers.tsx:9
2130
msgid "Plural Test: How many developers?"
2231
msgstr "Plural Test: How many developers?"
2332

24-
#: src/components/Switcher.tsx:15
33+
#: src/components/Switcher.tsx:11
2534
msgid "Serbian"
2635
msgstr "Serbian"
2736

28-
#: src/components/Switcher.tsx:16
37+
#: src/components/Switcher.tsx:12
2938
msgid "Spanish"
3039
msgstr "Spanish"
3140

32-
#: src/pages/index.tsx:25
41+
#: src/pages/index.tsx:28
3342
msgid "Translation Demo"
3443
msgstr "Translation Demo"
3544

36-
#: src/pages/index.tsx:32
45+
#: src/pages/index.tsx:35
3746
msgid "Welcome to <0>Next.js!</0>"
3847
msgstr "Welcome to <0>Next.js!</0>"
39-
40-
#: src/components/AboutText.tsx:6
41-
msgid "next-explanation"
42-
msgstr "Next.js is an open-source React front-end development web framework that enables functionality such as server-side rendering and generating static websites for React based web applications. It is a production-ready framework that allows developers to quickly create static and dynamic JAMstack websites and is used widely by many large companies."
43-
44-
#: src/components/Developers.tsx:20
45-
msgid "{selected, plural, one {Developer} other {Developers}}"
46-
msgstr "{selected, plural, one {Developer} other {Developers}}"

examples/nextjs-swc/src/locales/es.po

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,35 @@ msgstr ""
1313
"Language-Team: \n"
1414
"Plural-Forms: \n"
1515

16-
#: src/components/Switcher.tsx:14
16+
#: src/components/Developers.tsx:20
17+
msgid "{selected, plural, one {Developer} other {Developers}}"
18+
msgstr "{selected, plural, one {Programador} other {Programadores}}"
19+
20+
#: src/components/Switcher.tsx:10
1721
msgid "English"
1822
msgstr "Inglés"
1923

24+
#. js-lingui-explicit-id
25+
#: src/components/AboutText.tsx:6
26+
msgid "next-explanation"
27+
msgstr "Next.js es un marco de trabajo web de desarrollo front-end de React de código abierto que permite funciones como la representación del lado del servidor y la generación de sitios web estáticos para aplicaciones web basadas en React. Es un marco listo para producción que permite a los desarrolladores crear rápidamente sitios web JAMstack estáticos y dinámicos y es ampliamente utilizado por muchas grandes empresas."
28+
2029
#: src/components/Developers.tsx:9
2130
msgid "Plural Test: How many developers?"
2231
msgstr "Prueba Plural: Cuantos programadores?"
2332

24-
#: src/components/Switcher.tsx:15
33+
#: src/components/Switcher.tsx:11
2534
msgid "Serbian"
2635
msgstr "Serbio"
2736

28-
#: src/components/Switcher.tsx:16
37+
#: src/components/Switcher.tsx:12
2938
msgid "Spanish"
3039
msgstr "Español"
3140

32-
#: src/pages/index.tsx:25
41+
#: src/pages/index.tsx:28
3342
msgid "Translation Demo"
3443
msgstr "Demostración de Traducción"
3544

36-
#: src/pages/index.tsx:32
45+
#: src/pages/index.tsx:35
3746
msgid "Welcome to <0>Next.js!</0>"
3847
msgstr "Bienvenido a <0>Next.js!</0>"
39-
40-
#: src/components/AboutText.tsx:6
41-
msgid "next-explanation"
42-
msgstr "Next.js es un marco de trabajo web de desarrollo front-end de React de código abierto que permite funciones como la representación del lado del servidor y la generación de sitios web estáticos para aplicaciones web basadas en React. Es un marco listo para producción que permite a los desarrolladores crear rápidamente sitios web JAMstack estáticos y dinámicos y es ampliamente utilizado por muchas grandes empresas."
43-
44-
#: src/components/Developers.tsx:20
45-
msgid "{selected, plural, one {Developer} other {Developers}}"
46-
msgstr "{selected, plural, one {Programador} other {Programadores}}"

examples/nextjs-swc/src/locales/pseudo.po

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,35 @@ msgstr ""
1313
"Language-Team: \n"
1414
"Plural-Forms: \n"
1515

16-
#: src/components/Switcher.tsx:14
16+
#: src/components/Developers.tsx:20
17+
msgid "{selected, plural, one {Developer} other {Developers}}"
18+
msgstr ""
19+
20+
#: src/components/Switcher.tsx:10
1721
msgid "English"
1822
msgstr ""
1923

24+
#. js-lingui-explicit-id
25+
#: src/components/AboutText.tsx:6
26+
msgid "next-explanation"
27+
msgstr ""
28+
2029
#: src/components/Developers.tsx:9
2130
msgid "Plural Test: How many developers?"
2231
msgstr ""
2332

24-
#: src/components/Switcher.tsx:15
33+
#: src/components/Switcher.tsx:11
2534
msgid "Serbian"
2635
msgstr ""
2736

28-
#: src/components/Switcher.tsx:16
37+
#: src/components/Switcher.tsx:12
2938
msgid "Spanish"
3039
msgstr ""
3140

32-
#: src/pages/index.tsx:25
41+
#: src/pages/index.tsx:28
3342
msgid "Translation Demo"
3443
msgstr ""
3544

36-
#: src/pages/index.tsx:32
45+
#: src/pages/index.tsx:35
3746
msgid "Welcome to <0>Next.js!</0>"
3847
msgstr ""
39-
40-
#: src/components/AboutText.tsx:6
41-
msgid "next-explanation"
42-
msgstr ""
43-
44-
#: src/components/Developers.tsx:20
45-
msgid "{selected, plural, one {Developer} other {Developers}}"
46-
msgstr ""

examples/nextjs-swc/src/locales/sr.po

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,35 @@ msgstr ""
1313
"Language-Team: \n"
1414
"Plural-Forms: \n"
1515

16-
#: src/components/Switcher.tsx:14
16+
#: src/components/Developers.tsx:20
17+
msgid "{selected, plural, one {Developer} other {Developers}}"
18+
msgstr "{selected, plural, one {Програмер} other {Програмера}}"
19+
20+
#: src/components/Switcher.tsx:10
1721
msgid "English"
1822
msgstr "Енглески"
1923

24+
#. js-lingui-explicit-id
25+
#: src/components/AboutText.tsx:6
26+
msgid "next-explanation"
27+
msgstr "Некст.јс је отворени изворни Реацт-ов развојни вебоквир који омогућава функционалност као што је приказивање на страни сервера и генерисање статичких веблокација за веб апликације засноване на Реацт-у."
28+
2029
#: src/components/Developers.tsx:9
2130
msgid "Plural Test: How many developers?"
2231
msgstr "Тест за Множину: Колико програмера?"
2332

24-
#: src/components/Switcher.tsx:15
33+
#: src/components/Switcher.tsx:11
2534
msgid "Serbian"
2635
msgstr "Српски"
2736

28-
#: src/components/Switcher.tsx:16
37+
#: src/components/Switcher.tsx:12
2938
msgid "Spanish"
3039
msgstr "Шпански"
3140

32-
#: src/pages/index.tsx:25
41+
#: src/pages/index.tsx:28
3342
msgid "Translation Demo"
3443
msgstr "Демо Превод"
3544

36-
#: src/pages/index.tsx:32
45+
#: src/pages/index.tsx:35
3746
msgid "Welcome to <0>Next.js!</0>"
3847
msgstr "Добродошли у <0>Нект.јс!</0>"
39-
40-
#: src/components/AboutText.tsx:6
41-
msgid "next-explanation"
42-
msgstr "Некст.јс је отворени изворни Реацт-ов развојни вебоквир који омогућава функционалност као што је приказивање на страни сервера и генерисање статичких веблокација за веб апликације засноване на Реацт-у."
43-
44-
#: src/components/Developers.tsx:20
45-
msgid "{selected, plural, one {Developer} other {Developers}}"
46-
msgstr "{selected, plural, one {Програмер} other {Програмера}}"

examples/nextjs-swc/src/pages/index.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Developers from '../components/Developers'
66
import { Switcher } from '../components/Switcher'
77
import styles from '../styles/Index.module.css'
88
import { loadCatalog } from '../utils'
9+
import { useLingui } from '@lingui/react'
910

1011
export const getStaticProps: GetStaticProps = async (ctx) => {
1112
const translation = await loadCatalog(ctx.locale!)
@@ -17,9 +18,20 @@ export const getStaticProps: GetStaticProps = async (ctx) => {
1718
}
1819

1920
const Index: NextPage = () => {
21+
/**
22+
* This hook is needed to subscribe your
23+
* component for changes if you use t`` macro
24+
*/
25+
useLingui()
26+
2027
return (
2128
<div className={styles.container}>
2229
<Head>
30+
{/*
31+
The Next Head component is not being rendered in the React
32+
component tree and React Context is not being passed down to the components placed in the <Head>.
33+
That means we cannot use the <Trans> component here and instead have to use `t` macro.
34+
*/}
2335
<title>{t`Translation Demo`}</title>
2436
<link rel="icon" href="/favicon.ico" />
2537
</Head>

0 commit comments

Comments
 (0)