Skip to content

Commit

Permalink
feat: add support for runtimeConfigModule w/ Trans (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis authored Nov 30, 2020
1 parent 5c95f5d commit 23b06b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
14 changes: 14 additions & 0 deletions docs/ref/conf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,20 @@ You may use a different named export:
.. config:: sourceLocale

In some advanced cases you may also need to change the module from which
`Trans` is imported. To do that, pass an object to `runtimeConfigModule`:

.. code-block:: jsx
// If you import `i18n` object from custom module like this:
import { Trans, i18n } from "./custom-config"
// ... then add following line to Lingui configuration:
// "runtimeConfigModule": {
// i18n: ["./custom-config", "i18n"],
// Trans: ["./custom-config", "Trans"]
// }
sourceLocale
------------

Expand Down
4 changes: 3 additions & 1 deletion packages/conf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type DefaultLocaleObject = {
}
export type FallbackLocales = LocaleObject | DefaultLocaleObject | false

type ModuleSource = [string, string?];

export type LinguiConfig = {
catalogs: CatalogConfig[]
compileNamespace: string
Expand All @@ -39,7 +41,7 @@ export type LinguiConfig = {
orderBy: OrderBy
pseudoLocale: string
rootDir: string
runtimeConfigModule: [string, string?]
runtimeConfigModule: ModuleSource | {[symbolName: string]: ModuleSource},
sourceLocale: string
}

Expand Down
27 changes: 24 additions & 3 deletions packages/macro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,27 @@ import MacroJS from "./macroJs"
import MacroJSX from "./macroJsx"

const config = getConfig({ configPath: process.env.LINGUI_CONFIG })
const [i18nImportModule, i18nImportName = "i18n"] = config.runtimeConfigModule

const getSymbolSource = (name: string) => {
if (Array.isArray(config.runtimeConfigModule)) {
if (name === "i18n") {
return config.runtimeConfigModule
} else {
return ["@lingui/react", name]
}
} else {
if (
Object.prototype.hasOwnProperty.call(config.runtimeConfigModule, name)
) {
return config.runtimeConfigModule[name]
} else {
return ["@lingui/react", name]
}
}
}

const [i18nImportModule, i18nImportName = "i18n"] = getSymbolSource("i18n")
const [TransImportModule, TransImportName = "Trans"] = getSymbolSource("Trans")

function macro({ references, state, babel }) {
const jsxNodes = []
Expand Down Expand Up @@ -47,7 +67,7 @@ function macro({ references, state, babel }) {
}

if (jsxNodes.length) {
addImport(babel, state, "@lingui/react", "Trans")
addImport(babel, state, TransImportModule, TransImportName)
}

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

const linguiImport = state.file.path.node.body.find(
(importNode) =>t.isImportDeclaration(importNode) &&
(importNode) =>
t.isImportDeclaration(importNode) &&
importNode.source.value === module &&
// https://github.com/lingui/js-lingui/issues/777
importNode.importKind !== "type"
Expand Down

1 comment on commit 23b06b5

@vercel
Copy link

@vercel vercel bot commented on 23b06b5 Nov 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.