Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for runtimeConfigModule w/ Trans #895

Merged
merged 2 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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