An implementation of FormatJS [Application Workflow](https://formatjs.github.io/docs/getting-started/application-workflow/), declaring package messages [with a convention](https://formatjs.github.io/docs/guides/distribute-libraries#declaring-with-a-conven
npm install @postinumero/formatjs-toolsAn implementation of FormatJS Application Workflow, declaring package messages with a convention.
Extract from app to lang/.extracted.json:
``sh`
formatjs-tools extract
#### --path (multiple)
Extract from src to lang/.extracted.json:
`sh`
formatjs-tools extract --path src
#### --out-file
Extract from app to lang/en-DEFAULT.json:
`sh`
formatjs-tools extract --out-file lang/en-DEFAULT.json
Aggregate the extracted files from dependencies into a single file for each environment and locale.
Aggregate messages to .lang/aggregated/[environment:]:
`sh`
formatjs-tools aggregate
Collect the translation files into a single file for each environment and locale.
Collect messages to .lang/collected/[environment:]:
`sh`
formatjs-tools collect
Merge aggregated messages and collected translations for each environment and locale.
Merge:
- .lang/aggregated/[environment:].lang/collected/[environment:]
-
(saved to: .lang/merged/[environment:])
`sh`
formatjs-tools merge
Compile merged files for each environment and locale.
By default, output is written to .lang/compiled/[environment:]:
`sh`
formatjs-tools compile
To customize the output directory and enable hashed filenames:
`sh`
formatjs-tools compile --out-dir public/.compiled-lang --hash
- --out-dir sets a custom output path--hash
- appends a content hash to filenames (e.g. en-US-abc123.json)manifest.json
- A is also generated to map [environment:] to hash values
Write internal locale and environment configuration to .lang/config.json.
`sh`
formatjs-tools config
Run all of the commands above in correct order:
config, extract, aggregate, collect, merge, compile
`sh`
formatjs-tools process-messages
You can give options to individual commands by prefixing the option name with the command name (e.g. --extract.path src).
Use programmatically in Node.js:
`ts
import {
aggregate,
collect,
compile,
config,
extract,
merge,
processMessages,
} from "@postinumero/formatjs-tools/commands";
await aggregate();
`
Configuration is based on:
- Filenames in the lang directoryconfig.json
- Optional in the project rootlocales
- Environment variables (, environments)
See the example app for reference.
Files must follow the format:
``
lang/[environment:]
- environment is optional and may include multiple segments (e.g. com.example.test). com.example.test
By convention, use reverse domain naming. Environments inherit from their parent and subdomains — e.g. inherits from com.example, com, example.test and test.locale
- can include a region (e.g. en-US). Regional locales inherit from the base (e.g. en-US inherits from en).
When messages are extracted, they are saved as .extracted.json. .gitignore
These files are autogenerated and should not be edited manually — feel free to add them to .
To override default messages for all locales, you can create a .default.json file (optionally per environment).
Use config.json to customize environments and locales.lang
By default, all locales and environments are inferred from filenames in the directory.
You can override or extend this behavior:
Replace detected values entirely:
`json`
{
"locales": ["en", "fi"],
"environments": ["development", "production"]
}
Or extend/modify what's found in the lang directory:
`json`
{
"locales": {
"include": ["fr"], // Adds "fr" to any locales found in lang/
"exclude": ["fi"] // Removes "fi" if found
},
"environments": {
"include": ["test"] // Adds "test" environment
}
}
Example lib uses @formatjs/ts-transformer and ts-patch.
1. Extend your TSConfig\\
tsconfig.json:
`json`
{
"extends": "@postinumero/formatjs-tools/configs/tsconfig.lib.json"
}
2. Add prebuild script
package.json:
`json``
{
"scripts": {
"prebuild": "formatjs-tools extract --path src",
"build": "tspc"
}
}