Skip to content

Commit 5cd3e1c

Browse files
committed
docs: add metro-transformer docs
1 parent b377058 commit 5cd3e1c

3 files changed

Lines changed: 109 additions & 1 deletion

File tree

website/docs/ref/loader.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Webpack Loader
22

3-
The Webpack loader compiles catalogs on the fly. It replaces the `lingui compile` command: with the loader, you can `import` po files directly, instead of running `lingui compile` and `import`ing its output.
3+
The Webpack loader compiles catalogs on the fly. It's an alternative to the `lingui compile` command, so that you can `import` `.po` files directly, instead of running `lingui compile` and `import`ing its output.
44

55
## Installation
66

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Metro transformer
2+
3+
[Metro bundler](https://metrobundler.dev/) is a JavaScript bundler used in React Native apps. This package provides a transformer for Metro to compile `.po` message catalogs on the fly. It's an alternative to the `lingui compile`, so that you can `import` `.po` files directly, instead of running `lingui compile` and `import`ing its output.
4+
5+
## Installation
6+
7+
:::note
8+
Only Expo SDK 49 and React Native v0.72.1 or newer are supported.
9+
10+
The library is currently in beta. If you encounter any issues, please [report them](https://github.com/lingui/js-lingui/issues).
11+
:::
12+
13+
Install `@lingui/metro-transformer` as a development dependency:
14+
15+
```bash npm2yarn
16+
npm install --save-dev @lingui/metro-transformer
17+
```
18+
19+
Set up the transformer in your `metro.config.js` by specifying [`babelTransformerPath`](https://metrobundler.dev/docs/configuration/#babeltransformerpath):
20+
21+
import Tabs from "@theme/Tabs";
22+
import TabItem from "@theme/TabItem";
23+
24+
<Tabs groupId="babel-swc">
25+
<TabItem value="expo" label="Expo" default>
26+
27+
```js title="metro.config.js"
28+
// Learn more https://docs.expo.io/guides/customizing-metro
29+
const { getDefaultConfig } = require("expo/metro-config");
30+
31+
const config = getDefaultConfig(__dirname);
32+
const { transformer, resolver } = config;
33+
34+
config.transformer = {
35+
...transformer,
36+
babelTransformerPath: require.resolve("@lingui/metro-transformer/expo"),
37+
};
38+
config.resolver = {
39+
...resolver,
40+
assetExts: resolver.assetExts.filter((ext) => ext !== "po"),
41+
sourceExts: [...resolver.sourceExts, "po"],
42+
};
43+
44+
module.exports = config;
45+
```
46+
47+
</TabItem>
48+
<TabItem value="plain-RN" label="plain React Native">
49+
50+
```js title="metro.config.js"
51+
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
52+
53+
const defaultConfig = getDefaultConfig(__dirname);
54+
const { assetExts, sourceExts } = defaultConfig.resolver;
55+
56+
/**
57+
* Metro configuration
58+
* https://reactnative.dev/docs/metro
59+
*
60+
* @type {import('metro-config').MetroConfig}
61+
*/
62+
const config = {
63+
transformer: {
64+
babelTransformerPath: require.resolve(
65+
"@lingui/metro-transformer/react-native"
66+
)
67+
},
68+
resolver: {
69+
assetExts: assetExts.filter((ext) => ext !== "po"),
70+
sourceExts: [...sourceExts, "po"]
71+
}
72+
};
73+
74+
module.exports = mergeConfig(defaultConfig, config);
75+
```
76+
77+
</TabItem>
78+
</Tabs>
79+
80+
## Usage
81+
82+
:::tip
83+
Take a look at the [example app](https://github.com/lingui/js-lingui/tree/main/examples/react-native) that uses the transformer. Only `.po` files are supported by the transformer.
84+
:::
85+
86+
1. Import `.po` files directly in your code:
87+
88+
```diff
89+
-import { messages } from "./src/locales/en/messages.ts";
90+
+import { messages } from "./src/locales/en/messages.po";
91+
```
92+
93+
2. If you are using TypeScript, you need to add `.po` file declaration so that TypeScript understands the file extension:
94+
95+
```ts title="src/po-types.d.ts"
96+
declare module "*.po" {
97+
import type { Messages } from "@lingui/core";
98+
export const messages: Messages;
99+
}
100+
```
101+
102+
3. Restart Metro bundler with `expo start -c` or `yarn start --reset-cache` to clear the transformer cache.
103+
4. Profit! 🎉

website/sidebars.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ const sidebar = [
161161
label: "@lingui/loader",
162162
id: "ref/loader",
163163
},
164+
{
165+
type: "doc",
166+
label: "@lingui/metro-transformer",
167+
id: "ref/metro-transformer",
168+
},
164169
{
165170
type: "doc",
166171
label: "@lingui/extractor-vue",

0 commit comments

Comments
 (0)