Skip to content

Commit fc7de2b

Browse files
committed
feat: support all args from formatjs_cli
1 parent ce84874 commit fc7de2b

18 files changed

Lines changed: 404 additions & 67 deletions

BUILD.dev.bazel

Lines changed: 0 additions & 14 deletions
This file was deleted.

MODULE.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@ module(
3131
)
3232

3333
# Core dependencies required by rules_formatjs
34-
bazel_dep(name = "bazel_skylib", version = "1.8.2")
3534
bazel_dep(name = "jq.bzl", version = "0.4.0") # JSON manipulation in aggregation aspect
3635
bazel_dep(name = "package_metadata", version = "0.0.6") # Package metadata for BCR
3736
bazel_dep(name = "platforms", version = "1.0.0")
3837

3938
# Development dependencies
4039
bazel_dep(name = "bazel_lib", version = "3.0.0", dev_dependency = True)
4140
bazel_dep(name = "bazelrc-preset.bzl", version = "1.8.0", dev_dependency = True)
42-
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.8.2", dev_dependency = True)
4341
bazel_dep(name = "buildifier_prebuilt", version = "8.2.1", dev_dependency = True)
4442
bazel_dep(name = "gazelle", version = "0.47.0", dev_dependency = True, repo_name = "bazel_gazelle")
43+
bazel_dep(name = "rules_shell", version = "0.3.0", dev_dependency = True)
4544

4645
# FormatJS CLI toolchain
4746
formatjs_cli = use_extension("//formatjs_cli:extensions.bzl", "formatjs_cli")

MODULE.bazel.lock

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/aggregate/MODULE.bazel.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/custom_version/MODULE.bazel.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/simple/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ formatjs_extract(
99
)
1010

1111
# Compile messages for production using native toolchain
12+
# Can reference the output file directly since it uses attr.output
1213
formatjs_compile(
1314
name = "compile_messages",
14-
src = ":extract_messages",
15+
srcs = [":messages/en.json"],
1516
out = "messages/en-compiled.json",
1617
ast = True,
1718
)

examples/simple/MODULE.bazel.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1-
{}
1+
{
2+
"app.button": [
3+
{
4+
"location": null,
5+
"type": 0,
6+
"value": "Click me!"
7+
}
8+
],
9+
"app.description": [
10+
{
11+
"location": null,
12+
"type": 0,
13+
"value": "This is an example of internationalized React app using FormatJS and Bazel."
14+
}
15+
],
16+
"app.title": [
17+
{
18+
"location": null,
19+
"type": 0,
20+
"value": "Welcome to FormatJS"
21+
}
22+
]
23+
}

formatjs/compile.bzl

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,28 @@ def _formatjs_compile_impl(ctx):
2424
# Build arguments
2525
args = ctx.actions.args()
2626
args.add("compile")
27-
args.add(ctx.file.src)
27+
args.add_all(ctx.files.srcs)
2828
args.add("--out-file", out_file)
29-
args.add("--format", ctx.attr.format)
29+
30+
if ctx.attr.format:
31+
args.add("--format", ctx.attr.format)
3032

3133
if ctx.attr.ast:
3234
args.add("--ast")
3335

36+
if ctx.attr.skip_errors:
37+
args.add("--skip-errors")
38+
39+
if ctx.attr.pseudo_locale:
40+
args.add("--pseudo-locale", ctx.attr.pseudo_locale)
41+
42+
if ctx.attr.ignore_tag:
43+
args.add("--ignore-tag")
44+
3445
ctx.actions.run(
3546
executable = formatjs_cli_info.cli,
3647
arguments = [args],
37-
inputs = [ctx.file.src],
48+
inputs = ctx.files.srcs,
3849
outputs = [out_file],
3950
mnemonic = "FormatjsCompile",
4051
progress_message = "Compiling messages for %{label}",
@@ -73,7 +84,7 @@ formatjs_compile = rule(
7384
```starlark
7485
formatjs_compile(
7586
name = "messages_compiled",
76-
src = ":messages", # from formatjs_extract
87+
srcs = [":messages"], # from formatjs_extract
7788
out = "messages.json",
7889
)
7990
```
@@ -82,17 +93,30 @@ formatjs_compile = rule(
8293
```starlark
8394
formatjs_compile(
8495
name = "messages_prod",
85-
src = "translations/en.json",
96+
srcs = ["translations/en.json"],
8697
out = "compiled-en.json",
8798
ast = True,
8899
)
89100
```
90101
102+
### Compile multiple translation files:
103+
```starlark
104+
formatjs_compile(
105+
name = "messages_merged",
106+
srcs = [
107+
":base_messages",
108+
"translations/overrides.json",
109+
],
110+
out = "compiled-messages.json",
111+
ast = True,
112+
)
113+
```
114+
91115
### Compile translation files from Crowdin:
92116
```starlark
93117
formatjs_compile(
94118
name = "fr_messages",
95-
src = "translations/fr.json",
119+
srcs = ["translations/fr.json"],
96120
out = "compiled-fr.json",
97121
format = "crowdin",
98122
ast = True,
@@ -117,17 +141,18 @@ formatjs_compile = rule(
117141
- FormatJS CLI documentation: https://formatjs.github.io/docs/tooling/cli
118142
""",
119143
attrs = {
120-
"src": attr.label(
121-
allow_single_file = [".json"],
144+
"srcs": attr.label_list(
145+
allow_files = [".json"],
122146
mandatory = True,
123-
doc = """Source JSON file with extracted messages.
147+
doc = """Source JSON files with extracted messages.
124148
125-
This can be:
126-
- Output from `formatjs_extract` rule
127-
- Translation file from translators
149+
Can include:
150+
- Multiple outputs from `formatjs_extract` rules
151+
- Multiple translation files from translators
128152
- Aggregated messages from `formatjs_aggregate`
129153
130-
The file should contain messages in FormatJS JSON format.
154+
When multiple files are provided, they will be merged during compilation.
155+
The files should contain messages in FormatJS JSON format.
131156
""",
132157
),
133158
"out": attr.output(
@@ -153,17 +178,48 @@ formatjs_compile = rule(
153178
""",
154179
),
155180
"format": attr.string(
156-
default = "simple",
157-
values = ["simple", "crowdin", "smartling", "transifex"],
158181
doc = """Input format of the source file.
159182
160183
Supported formats:
161-
- `simple`: Standard FormatJS JSON format (default)
184+
- `default`: Default formatter: extracts defaultMessage from MessageDescriptor objects
185+
- `simple`: Simple formatter: pass-through for Record<string, string>
162186
- `crowdin`: Crowdin translation platform format
163187
- `smartling`: Smartling translation platform format
164188
- `transifex`: Transifex translation platform format
189+
- `lokalise`: Lokalise translation platform format
165190
166191
Use the appropriate format based on your translation management system.
192+
If not specified, uses the default formatter.
193+
""",
194+
),
195+
"skip_errors": attr.bool(
196+
default = False,
197+
doc = """Continue compiling after errors.
198+
199+
When enabled, keys with errors are excluded from output but compilation continues.
200+
When disabled (default), compilation fails on the first error.
201+
Useful for partially migrating or dealing with incomplete translations.
202+
""",
203+
),
204+
"pseudo_locale": attr.string(
205+
doc = """Generate pseudo-locale files for testing.
206+
207+
Requires `ast = True`. Available pseudo-locales:
208+
- `xx-LS`: Longer strings (tests UI expansion)
209+
- `xx-AC`: Accented characters (tests character encoding)
210+
- `xx-HA`: Right-to-left text (tests RTL layout)
211+
- `en-XA`: Accented English
212+
- `en-XB`: Bracketed English
213+
214+
Pseudo-locales help identify i18n issues without actual translations.
215+
""",
216+
),
217+
"ignore_tag": attr.bool(
218+
default = False,
219+
doc = """Treat HTML/XML tags as string literals instead of parsing them as tag tokens.
220+
221+
When enabled, tags like `<b>` are treated as plain text rather than formatting tags.
222+
Useful for messages that need to preserve literal tag syntax.
167223
""",
168224
),
169225
},

0 commit comments

Comments
 (0)