Skip to content

Commit 8979aaf

Browse files
authored
fix: mandatory ext on @lingui/loader (#831)
1 parent 3924d79 commit 8979aaf

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

docs/ref/loader.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ Usage
2525
Simply prepend ``@lingui/loader:`` in front of path to message catalog you want to
2626
import. Here's an example of dynamic import:
2727

28+
Extension is mandatory. If you use minimal or lingui file format, use ``.json``. In case of using po format, use ``.po``.
29+
2830
.. code-block:: jsx
2931
3032
export async function dynamicActivate(locale: string) {
31-
const { messages } = await import(`@lingui/loader!./locales/${locale}/messages.js`)
33+
const { messages } = await import(`@lingui/loader!./locales/${locale}/messages.json`)
3234
i18n.load(locale, messages)
3335
i18n.activate(locale)
3436
}

examples/create-react-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
},
4242
"devDependencies": {
4343
"@lingui/cli": "^3.0.0",
44+
"@lingui/loader": "^3.0.0",
4445
"@lingui/macro": "^3.0.0"
4546
}
4647
}

examples/create-react-app/src/i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ i18n.loadLocaleData({
1717
* @param locale any locale string
1818
*/
1919
export async function dynamicActivate(locale: string) {
20-
const { messages } = await import(`./locales/${locale}/messages`)
20+
const { messages } = await import(`@lingui/loader!./locales/${locale}/messages.po`)
2121
i18n.load(locale, messages)
2222
i18n.activate(locale)
2323
}

packages/loader/src/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,23 @@ export default function (source) {
5454
cwd: path.dirname(this.resourcePath),
5555
})
5656

57+
const EMPTY_EXT = /\.[0-9a-z]+$/.test(this.resourcePath)
58+
const JS_EXT = /\.js+$/.test(this.resourcePath)
59+
60+
const catalogRelativePath = path.relative(config.rootDir, this.resourcePath)
61+
62+
if (!EMPTY_EXT || JS_EXT) {
63+
const formats = {
64+
minimal: ".json",
65+
po: ".po",
66+
lingui: ".json"
67+
}
68+
// we replace the .js, because webpack appends automatically the .js on imports without extension
69+
throw new Error(`File extension is mandatory, for ex: import("@lingui/loader!./${catalogRelativePath.replace(".js", formats[config.format])}")`)
70+
}
71+
5772
const { locale, catalog } = getCatalogForFile(
58-
path.relative(config.rootDir, this.resourcePath),
73+
catalogRelativePath,
5974
getCatalogs(config)
6075
)
6176
const catalogs = catalog.readAll()

0 commit comments

Comments
 (0)