Skip to content

Commit

Permalink
feat(macro): add useLingui macro (#1859)
Browse files Browse the repository at this point in the history
Co-authored-by: ddyo <danrosenshain@gmail.com>
  • Loading branch information
2 people authored and andrii-bodnar committed Mar 18, 2024
1 parent 44cd960 commit 916732f
Show file tree
Hide file tree
Showing 20 changed files with 883 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,26 @@ exports[`@lingui/babel-plugin-extract-messages should extract all messages from
48,
],
},
{
comment: undefined,
context: undefined,
id: VO4BJY,
message: [useLingui]: TplLiteral,
origin: [
js-with-macros.js,
53,
],
},
{
comment: undefined,
context: undefined,
id: ZxxjOE,
message: [useLingui]: Text {0, plural, offset:1 =0 {No books} =1 {1 book} other {# books}},
origin: [
js-with-macros.js,
56,
],
},
]
`;

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

t`Message`

Expand Down Expand Up @@ -46,3 +46,17 @@ const defineMessageAlias = msg({
})

const defineMessageAlias2 = msg`TplLiteral`

function MyComponent() {
const { t } = useLingui()

t`[useLingui]: TplLiteral`

// macro nesting
const a = t`[useLingui]: Text ${plural(users.length, {
offset: 1,
0: "No books",
1: "1 book",
other: "# books",
})}`
}
2 changes: 2 additions & 0 deletions packages/conf/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ exports[`@lingui/conf should return default config 1`] = `
TransImportName: Trans,
i18nImportModule: @lingui/core,
i18nImportName: i18n,
useLinguiImportModule: @lingui/react,
useLinguiImportName: useLingui,
},
service: {
apiKey: ,
Expand Down
87 changes: 51 additions & 36 deletions packages/conf/src/migrations/normalizeRuntimeConfigModule.test.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,77 @@
import { normalizeRuntimeConfigModule } from "./normalizeRuntimeConfigModule"

describe("normalizeRuntimeConfigModule", () => {
it("only i18n specified", () => {
const actual = normalizeRuntimeConfigModule({
runtimeConfigModule: ["../my-i18n", "myI18n"],
})
test("all defaults", () => {
const actual = normalizeRuntimeConfigModule({ runtimeConfigModule: {} })

expect(actual.runtimeConfigModule).toStrictEqual({
TransImportModule: "@lingui/react",
TransImportName: "Trans",
i18nImportModule: "../my-i18n",
i18nImportName: "myI18n",
})
expect(actual.runtimeConfigModule).toMatchInlineSnapshot(`
{
TransImportModule: @lingui/react,
TransImportName: Trans,
i18nImportModule: @lingui/core,
i18nImportName: i18n,
useLinguiImportModule: @lingui/react,
useLinguiImportName: useLingui,
}
`)
})

it("Trans and i18n specified", () => {
test("i18n specified as legacy shorthand", () => {
const actual = normalizeRuntimeConfigModule({
runtimeConfigModule: {
i18n: ["./custom-i18n", "myI18n"],
Trans: ["./custom-trans", "myTrans"],
},
runtimeConfigModule: ["../my-i18n", "myI18n"],
})

expect(actual.runtimeConfigModule).toStrictEqual({
TransImportModule: "./custom-trans",
TransImportName: "myTrans",
i18nImportModule: "./custom-i18n",
i18nImportName: "myI18n",
})
expect(actual.runtimeConfigModule).toMatchInlineSnapshot(`
{
TransImportModule: @lingui/react,
TransImportName: Trans,
i18nImportModule: ../my-i18n,
i18nImportName: myI18n,
useLinguiImportModule: @lingui/react,
useLinguiImportName: useLingui,
}
`)
})

it("i18n specified as object", () => {
it("All runtime modules specified", () => {
const actual = normalizeRuntimeConfigModule({
runtimeConfigModule: {
i18n: ["./custom-i18n", "myI18n"],
Trans: ["./custom-trans", "myTrans"],
useLingui: ["./custom-use-lingui", "myLingui"],
},
})

expect(actual.runtimeConfigModule).toStrictEqual({
TransImportModule: "@lingui/react",
TransImportName: "Trans",
i18nImportModule: "./custom-i18n",
i18nImportName: "myI18n",
})
expect(actual.runtimeConfigModule).toMatchInlineSnapshot(`
{
TransImportModule: ./custom-trans,
TransImportName: myTrans,
i18nImportModule: ./custom-i18n,
i18nImportName: myI18n,
useLinguiImportModule: ./custom-use-lingui,
useLinguiImportName: myLingui,
}
`)
})

it("Trans specified as object", () => {
it("only module is specified", () => {
const actual = normalizeRuntimeConfigModule({
runtimeConfigModule: {
Trans: ["./custom-trans", "myTrans"],
i18n: ["./custom-i18n"],
Trans: ["./custom-trans"],
useLingui: ["./custom-use-lingui"],
},
})

expect(actual.runtimeConfigModule).toStrictEqual({
TransImportModule: "./custom-trans",
TransImportName: "myTrans",
i18nImportModule: "@lingui/core",
i18nImportName: "i18n",
})
expect(actual.runtimeConfigModule).toMatchInlineSnapshot(`
{
TransImportModule: ./custom-trans,
TransImportName: Trans,
i18nImportModule: ./custom-i18n,
i18nImportName: i18n,
useLinguiImportModule: ./custom-use-lingui,
useLinguiImportName: useLingui,
}
`)
})
})
16 changes: 14 additions & 2 deletions packages/conf/src/migrations/normalizeRuntimeConfigModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ const getSymbolSource = (
const name = defaults[1]
if (Array.isArray(config)) {
if (name === "i18n") {
return config
return config as ModuleSrc
}
return defaults
}

return config[name] || defaults
const override = (config as any)[name] as ModuleSrc

if (!override) {
return defaults
}

return [override[0], override[1] || name]
}

export function normalizeRuntimeConfigModule(
Expand All @@ -28,6 +34,10 @@ export function normalizeRuntimeConfigModule(
["@lingui/react", "Trans"],
config.runtimeConfigModule
)
const [useLinguiImportModule, useLinguiImportName] = getSymbolSource(
["@lingui/react", "useLingui"],
config.runtimeConfigModule
)

return {
...config,
Expand All @@ -36,6 +46,8 @@ export function normalizeRuntimeConfigModule(
i18nImportName,
TransImportModule,
TransImportName,
useLinguiImportModule,
useLinguiImportName,
} satisfies LinguiConfigNormalized["runtimeConfigModule"],
}
}
8 changes: 6 additions & 2 deletions packages/conf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type LocaleObject = {

export type FallbackLocales = LocaleObject

type ModuleSource = [string, string?]
type ModuleSource = readonly [module: string, specifier?: string]

type CatalogService = {
name: string
Expand Down Expand Up @@ -207,7 +207,9 @@ export type LinguiConfig = {
orderBy?: OrderBy
pseudoLocale?: string
rootDir?: string
runtimeConfigModule?: ModuleSource | { [symbolName: string]: ModuleSource }
runtimeConfigModule?:
| ModuleSource
| Partial<Record<"useLingui" | "Trans" | "i18n", ModuleSource>>
sourceLocale?: string
service?: CatalogService
experimental?: {
Expand All @@ -225,5 +227,7 @@ export type LinguiConfigNormalized = Omit<
i18nImportName: string
TransImportModule: string
TransImportName: string
useLinguiImportModule: string
useLinguiImportName: string
}
}
15 changes: 15 additions & 0 deletions packages/macro/__typetests__/index.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Plural,
Select,
SelectOrdinal,
useLingui,
} from "../index"
// eslint-disable-next-line import/no-extraneous-dependencies
import React from "react"
Expand Down Expand Up @@ -338,3 +339,17 @@ m = (
other={<Trans>...</Trans>}
/>
)

////////////////////////
//// React useLingui()
////////////////////////
function MyComponent() {
const { t, i18n } = useLingui()

expectType<string>(t`Hello world`)
expectType<string>(t({ message: "my message" }))
// @ts-expect-error: you could not pass a custom instance here
t(i18n)({ message: "my message" })

expectType<I18n>(i18n)
}
35 changes: 34 additions & 1 deletion packages/macro/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import type { ReactNode, VFC, FC } from "react"
import type { I18n, MessageDescriptor } from "@lingui/core"
import type { TransRenderCallbackOrComponent } from "@lingui/react"
import type { TransRenderCallbackOrComponent, I18nContext } from "@lingui/react"

export type ChoiceOptions = {
/** Offset of value when calculating plural forms */
Expand Down Expand Up @@ -316,3 +316,36 @@ export const SelectOrdinal: VFC<PluralChoiceProps>
* ```
*/
export const Select: VFC<SelectChoiceProps>

export function _t(descriptor: MacroMessageDescriptor): string
export function _t(
literals: TemplateStringsArray,
...placeholders: any[]
): string

/**
*
* Macro version of useLingui replaces _ function with `t` macro function which is bound to i18n passed from React.Context
*
* Returned `t` macro function has all the same signatures as global `t`
*
* @example
* ```
* const { t } = useLingui();
* const message = t`Text`;
* ```
*
* @example
* ```
* const { i18n, t } = useLingui();
* const locale = i18n.locale;
* const message = t({
* id: "msg.hello",
* comment: "Greetings at the homepage",
* message: `Hello ${name}`,
* });
* ```
*/
export function useLingui(): Omit<I18nContext, "_"> & {
t: typeof _t
}
Loading

0 comments on commit 916732f

Please sign in to comment.