Skip to content

Commit

Permalink
refactor(macro): @lingui/core/macro & @lingui/react/macro
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Mar 11, 2024
1 parent b1c26b7 commit 1b68a44
Show file tree
Hide file tree
Showing 108 changed files with 557 additions and 686 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
[![Join the community on Discord][Badge-Discord]][Discord]

[**Documentation**][Documentation] · [**Quickstart**](#quickstart) · [**Example**](#example) · [**Support**](#support) · [**Contribute**](#contribute) · [**License**](#license)

</div>

> Internationalization is the design and development of a product, application or document content that enables easy localization for target audiences that vary in culture, region, or language.
>
> --- [ W3C Web Internationalization FAQ](https://www.w3.org/International/questions/qa-i18n)

Lingui is an easy yet powerful internationalization framework for global projects.

- **Clean and readable** - Keep your code clean and readable, while the library uses
Expand Down Expand Up @@ -76,14 +76,14 @@ If you're a react-intl user, check out [a comparison of react-intl and Lingui](h
Short example how i18n looks with JSX:

```js
import { Trans } from "@lingui/macro"
import { Trans } from "@lingui/core/macro"

function App() {
return (
<Trans id="msg.docs" /* id is optional */>
Read the <a href="https://lingui.dev">documentation</a>
for more info.
</Trans>
<Trans id="msg.docs" /* id is optional */>
Read the <a href="https://lingui.dev">documentation</a>
for more info.
</Trans>
)
}
```
Expand Down Expand Up @@ -131,18 +131,15 @@ The project is licensed under the [MIT][License] license.
[SetupVite]: https://lingui.dev/tutorials/setup-vite
[RefCLI]: https://lingui.dev/ref/cli
[Examples]: https://github.com/lingui/js-lingui/tree/main/examples

[SWCPlugin]: https://lingui.dev/ref/swc-plugin
[VitePlugin]: https://lingui.dev/ref/vite-plugin
[ESLintPlugin]: https://lingui.dev/ref/eslint-plugin

[Badge-MainSuite-GithubCI]: https://github.com/lingui/js-lingui/workflows/main-suite/badge.svg
[Badge-ReleaseWorkflowTesting-GithubCI]: https://github.com/lingui/js-lingui/workflows/release-workflow-test/badge.svg
[Badge-Coverage]: https://img.shields.io/codecov/c/github/lingui/js-lingui/main.svg
[Badge-PRWelcome]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[Badge-Discord]: https://img.shields.io/discord/974702239358783608.svg?label=Discord&logo=Discord&colorB=7289da&style=flat-square
[Contributors]: https://github.com/lingui/js-lingui/graphs/contributors

[Coverage]: https://codecov.io/gh/lingui/js-lingui
[License]: https://github.com/lingui/js-lingui/blob/main/LICENSE
[Contributing]: https://github.com/lingui/js-lingui/blob/main/CONTRIBUTING.md
Expand Down
1 change: 0 additions & 1 deletion examples/create-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"devDependencies": {
"@lingui/cli": "^4.8.0-next.0",
"@lingui/loader": "^4.8.0-next.0",
"@lingui/macro": "^4.8.0-next.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/create-react-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./App.css"
import React, { useState } from "react"
import { Trans, Plural } from "@lingui/react.macro"
import { Trans, Plural } from "@lingui/react/macro"

import { locales, dynamicActivate } from "./i18n"
import { useLingui } from "@lingui/react"
Expand Down
3 changes: 1 addition & 2 deletions examples/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
"extract": "lingui extract --clean"
},
"dependencies": {
"@lingui/core": "^4.1.2",
"@lingui/macro": "^4.1.2"
"@lingui/core": "^4.1.2"
},
"devDependencies": {
"@babel/core": "^7.20.12",
Expand Down
2 changes: 1 addition & 1 deletion examples/js/src/ids.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { i18n } from "@lingui/core"
import { t, plural, defineMessage } from "@lingui/macro"
import { t, plural, defineMessage } from "@lingui/core/macro"

i18n.load({
en: require("./locale/en/messages").messages,
Expand Down
2 changes: 1 addition & 1 deletion examples/js/src/messages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { i18n } from "@lingui/core"
import { t, plural, defineMessage } from "@lingui/macro"
import { t, plural, defineMessage } from "@lingui/core/macro"

i18n.load({
en: require("./locale/en/messages").messages,
Expand Down
1 change: 0 additions & 1 deletion examples/nextjs-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"devDependencies": {
"@lingui/cli": "^4.8.0-next.0",
"@lingui/loader": "^4.8.0-next.0",
"@lingui/macro": "^4.8.0-next.0",
"@types/react": "^18.0.28",
"babel-plugin-macros": "^3.1.0",
"typescript": "^4.9.5",
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-babel/src/components/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Head from "next/head"
import classnames from "classnames"

import { Trans, useLingui } from "@lingui/react.macro"
import { Trans, useLingui } from "@lingui/react/macro"

import styles from "./Layout.module.css"
import { useRouter } from "next/router"
Expand Down
4 changes: 2 additions & 2 deletions examples/nextjs-babel/src/pages/examples.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { plural, msg } from "@lingui/macro"
import { Trans, Plural } from "@lingui/react.macro"
import { plural, msg } from "@lingui/core/macro"
import { Trans, Plural } from "@lingui/react/macro"

import { Layout } from "../components/Layout"
import { PluralExample } from "../components/PluralExample"
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-babel/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Link from "next/link"

import { Trans } from "@lingui/react.macro"
import { Trans } from "@lingui/react/macro"

import { Layout } from "../components/Layout"
import styles from "./index.module.css"
Expand Down
1 change: 0 additions & 1 deletion examples/nextjs-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"devDependencies": {
"@lingui/cli": "^4.8.0-next.0",
"@lingui/loader": "^4.8.0-next.0",
"@lingui/macro": "^4.8.0-next.0",
"@lingui/swc-plugin": "4.0.6",
"@types/react": "^18.0.14",
"eslint": "8.35.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-swc/src/components/AboutText.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Trans } from '@lingui/react.macro'
import { Trans } from '@lingui/react/macro'

export function AboutText() {
return (
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-swc/src/components/Developers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from 'react'
import { Trans, Plural } from '@lingui/react.macro'
import { Trans, Plural } from '@lingui/react/macro'

export default function Developers() {
const [selected, setSelected] = useState('1')
Expand Down
4 changes: 2 additions & 2 deletions examples/nextjs-swc/src/components/Switcher.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRouter } from 'next/router'
import { useState } from 'react'
import { t, msg } from '@lingui/macro'
import { MessageDescriptor } from '@lingui/core/src'
import { t, msg } from '@lingui/core/macro'
import { MessageDescriptor } from '@lingui/core'
import { useLingui } from '@lingui/react'

type LOCALES = 'en' | 'sr' | 'es' | 'pseudo'
Expand Down
4 changes: 2 additions & 2 deletions examples/nextjs-swc/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { msg } from '@lingui/macro'
import { Trans } from '@lingui/react.macro'
import { msg } from '@lingui/core/macro'
import { Trans } from '@lingui/react/macro'
import { GetStaticProps, NextPage } from 'next'
import Head from 'next/head'
import { AboutText } from '../components/AboutText'
Expand Down
1 change: 0 additions & 1 deletion examples/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"devDependencies": {
"@babel/core": "^7.21.0",
"@lingui/cli": "^4.1.2",
"@lingui/macro": "^4.1.2",
"@react-native-community/eslint-config": "^3.2.0",
"@types/react": "~18.0.14",
"@typescript-eslint/eslint-plugin": "^5.59.11",
Expand Down
4 changes: 2 additions & 2 deletions examples/react-native/src/MainScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import { StyleSheet, Text, View, Alert, SafeAreaView } from "react-native";
import { Plural, SelectOrdinal, Trans } from "@lingui/react.macro";
import { useLingui } from "@lingui/react.macro";
import { Plural, SelectOrdinal, Trans } from "@lingui/react/macro";
import { useLingui } from "@lingui/react/macro";
import { Button } from "./PaddedButton";
import { Heading } from "./Components";

Expand Down
1 change: 0 additions & 1 deletion examples/rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"devDependencies": {
"@babel/core": "^7.22.10",
"@lingui/cli": "^4.4.0",
"@lingui/macro": "^4.4.0",
"@rspack/cli": "latest",
"@types/react": "18.2.0",
"@types/react-dom": "18.2.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/rspack/src/Inbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react"

import { useLingui } from "@lingui/react"

import { Trans, Plural } from "@lingui/react.macro"
import { Trans, Plural } from "@lingui/react/macro"

import LocaleSwitcher from "./LocaleSwitcher"

Expand Down
2 changes: 1 addition & 1 deletion examples/vite-project-react-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"preview": "vite preview"
},
"dependencies": {
"@lingui/macro": "^4.1.2",
"@lingui/react": "^4.1.2",
"@lingui/core": "^4.1.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-project-react-babel/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import reactLogo from "./assets/react.svg"
import linguiLogo from "./assets/lingui-logo.svg"
import viteLogo from "/vite.svg"
import "./App.css"
import { Plural, Trans } from "@lingui/react.macro"
import { Plural, Trans } from "@lingui/react/macro"
import { I18nProvider } from "@lingui/react"
import { i18n } from "@lingui/core"
import { loadCatalog } from "./i18n"
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-project-react-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@swc/core": "1.3.56"
},
"dependencies": {
"@lingui/macro": "^4.2.0",
"@lingui/core": "^4.2.0",
"@lingui/react": "^4.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-project-react-swc/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import reactLogo from "./assets/react.svg"
import linguiLogo from "./assets/lingui-logo.svg"
import viteLogo from "/vite.svg"
import "./App.css"
import { Plural, Trans } from "@lingui/react.macro"
import { Plural, Trans } from "@lingui/react/macro"
import { I18nProvider } from "@lingui/react"
import { i18n } from "@lingui/core"
import { loadCatalog } from "./i18n"
Expand Down
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ module.exports = {
"<rootDir>/packages/conf",
"<rootDir>/packages/loader",
"<rootDir>/packages/macro",
"<rootDir>/packages/react.macro",
"<rootDir>/packages/vite-plugin",
"<rootDir>/packages/format-po",
"<rootDir>/packages/format-po-gettext",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ exports[`@lingui/babel-plugin-extract-messages should extract Plural messages fr
message: {count, plural, one {# book} other {# books}},
origin: [
jsx-without-trans.js,
3,
2,
],
},
{
Expand All @@ -159,7 +159,7 @@ exports[`@lingui/babel-plugin-extract-messages should extract Plural messages fr
message: {count, plural, one {# book} other {# books}},
origin: [
jsx-without-trans.js,
4,
3,
],
},
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Trans } from "@lingui/react.macro"
import { t, plural } from "@lingui/macro"
import { Trans } from "@lingui/react/macro"
import { t, plural } from "@lingui/core/macro"
;<Trans>Hi, my name is {name}</Trans>
;<Trans context="Context1">Some message</Trans>
;<Trans context="Context1">Some other message</Trans>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Plural } from "@lingui/react.macro"

import { Plural } from "@lingui/react/macro"
;<Plural value={count} one="# book" other="# books" />
;<Plural value={count} one="# book" other="# books" context="Some context" />
5 changes: 3 additions & 2 deletions packages/babel-plugin-lingui-macro/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ export const MESSAGE = "message"
export const COMMENT = "comment"
export const EXTRACT_MARK = "i18n"
export const CONTEXT = "context"
export const MACRO_PACKAGE = "@lingui/macro"
export const MACRO_REACT_PACKAGE = "@lingui/react.macro"
export const MACRO_LEGACY_PACKAGE = "@lingui/macro"
export const MACRO_CORE_PACKAGE = "@lingui/core/macro"
export const MACRO_REACT_PACKAGE = "@lingui/react/macro"

export enum JsMacroName {
t = "t",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-lingui-macro/src/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function macro({ state, babel, config }: MacroParams) {
Object.defineProperty(module.exports, name, {
get() {
throw new Error(
`The macro you imported from "@lingui/macro" or "@lingui/react.macro" is being executed outside the context of compilation with babel-plugin-macros. ` +
`The macro you imported from "@lingui/core/macro" or "@lingui/react/macro" is being executed outside the context of compilation with babel-plugin-macros. ` +
`This indicates that you don't have the babel plugin "babel-plugin-macros" configured correctly. ` +
`Please see the documentation for how to configure babel-plugin-macros properly: ` +
"https://github.com/kentcdodds/babel-plugin-macros/blob/main/other/docs/user.md"
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-lingui-macro/src/macroJs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { JsMacroName } from "./constants"
const parseExpression = (expression: string) => {
let path: NodePath<Expression>

const importExp = `import {t, plural, select, selectOrdinal} from "@lingui/macro"; \n`
const importExp = `import {t, plural, select, selectOrdinal} from "@lingui/core/macro"; \n`
transformSync(importExp + expression, {
filename: "unit-test.js",
configFile: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-lingui-macro/src/macroJsx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { JsxMacroName } from "./constants"
const parseExpression = (expression: string) => {
let path: NodePath<JSXElement>

const importExp = `import {Trans, Plural, Select, SelectOrdinal} from "@lingui/react.macro";\n`
const importExp = `import {Trans, Plural, Select, SelectOrdinal} from "@lingui/react/macro";\n`

transformSync(importExp + expression, {
filename: "unit-test.js",
Expand Down
14 changes: 9 additions & 5 deletions packages/babel-plugin-lingui-macro/src/macroJsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
MESSAGE,
JsxMacroName,
MACRO_REACT_PACKAGE,
MACRO_LEGACY_PACKAGE,
} from "./constants"
import { generateMessageId } from "@lingui/message-utils/generateMessageId"

Expand Down Expand Up @@ -505,12 +506,15 @@ export default class MacroJSX {
path: NodePath,
name: JsxMacroName
): path is NodePath<JSXElement> => {
if (!path.isJSXElement()) {
return false
}

const identifier = path.get("openingElement").get("name")

return (
path.isJSXElement() &&
path
.get("openingElement")
.get("name")
.referencesImport(MACRO_REACT_PACKAGE, name)
identifier.referencesImport(MACRO_REACT_PACKAGE, name) ||
identifier.referencesImport(MACRO_LEGACY_PACKAGE, name)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { t } from '@lingui/macro'
import { t } from "@lingui/core/macro"

t`Multiline\
with continuation`
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { t } from '@lingui/macro'
import { t } from "@lingui/core/macro"

function scoped (foo) {
function scoped(foo) {
if (foo) {
const bar = 50
t`This is bar ${bar}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Select, Plural } from "@lingui/react.macro"

import { Select, Plural } from "@lingui/react/macro"
;<Select
value={genderOfHost}
_female={
Expand Down
Loading

0 comments on commit 1b68a44

Please sign in to comment.