Skip to content

Commit 23b06b5

Browse files
authored
feat: add support for runtimeConfigModule w/ Trans (#895)
1 parent 5c95f5d commit 23b06b5

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

docs/ref/conf.rst

+14
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,20 @@ You may use a different named export:
416416
417417
.. config:: sourceLocale
418418

419+
In some advanced cases you may also need to change the module from which
420+
`Trans` is imported. To do that, pass an object to `runtimeConfigModule`:
421+
422+
.. code-block:: jsx
423+
424+
// If you import `i18n` object from custom module like this:
425+
import { Trans, i18n } from "./custom-config"
426+
427+
// ... then add following line to Lingui configuration:
428+
// "runtimeConfigModule": {
429+
// i18n: ["./custom-config", "i18n"],
430+
// Trans: ["./custom-config", "Trans"]
431+
// }
432+
419433
sourceLocale
420434
------------
421435

packages/conf/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ type DefaultLocaleObject = {
2727
}
2828
export type FallbackLocales = LocaleObject | DefaultLocaleObject | false
2929

30+
type ModuleSource = [string, string?];
31+
3032
export type LinguiConfig = {
3133
catalogs: CatalogConfig[]
3234
compileNamespace: string
@@ -39,7 +41,7 @@ export type LinguiConfig = {
3941
orderBy: OrderBy
4042
pseudoLocale: string
4143
rootDir: string
42-
runtimeConfigModule: [string, string?]
44+
runtimeConfigModule: ModuleSource | {[symbolName: string]: ModuleSource},
4345
sourceLocale: string
4446
}
4547

packages/macro/src/index.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,27 @@ import MacroJS from "./macroJs"
55
import MacroJSX from "./macroJsx"
66

77
const config = getConfig({ configPath: process.env.LINGUI_CONFIG })
8-
const [i18nImportModule, i18nImportName = "i18n"] = config.runtimeConfigModule
8+
9+
const getSymbolSource = (name: string) => {
10+
if (Array.isArray(config.runtimeConfigModule)) {
11+
if (name === "i18n") {
12+
return config.runtimeConfigModule
13+
} else {
14+
return ["@lingui/react", name]
15+
}
16+
} else {
17+
if (
18+
Object.prototype.hasOwnProperty.call(config.runtimeConfigModule, name)
19+
) {
20+
return config.runtimeConfigModule[name]
21+
} else {
22+
return ["@lingui/react", name]
23+
}
24+
}
25+
}
26+
27+
const [i18nImportModule, i18nImportName = "i18n"] = getSymbolSource("i18n")
28+
const [TransImportModule, TransImportName = "Trans"] = getSymbolSource("Trans")
929

1030
function macro({ references, state, babel }) {
1131
const jsxNodes = []
@@ -47,7 +67,7 @@ function macro({ references, state, babel }) {
4767
}
4868

4969
if (jsxNodes.length) {
50-
addImport(babel, state, "@lingui/react", "Trans")
70+
addImport(babel, state, TransImportModule, TransImportName)
5171
}
5272

5373
if (process.env.LINGUI_EXTRACT === "1") {
@@ -61,7 +81,8 @@ function addImport(babel, state, module, importName) {
6181
const { types: t } = babel
6282

6383
const linguiImport = state.file.path.node.body.find(
64-
(importNode) =>t.isImportDeclaration(importNode) &&
84+
(importNode) =>
85+
t.isImportDeclaration(importNode) &&
6586
importNode.source.value === module &&
6687
// https://github.com/lingui/js-lingui/issues/777
6788
importNode.importKind !== "type"

0 commit comments

Comments
 (0)