Compiles a resx file into typescript.
The compiled file will expose the available translations, so that the typescript compiler can help enforce valid usage.
Note that for server-side this should be added as a regular dependency, since it exposes a format
function that the compiled translation files will import. For a client-side bundle, it does not matter, as long as the dependency is available at build-time.
Files can be generated using npx resx-compiler <source folder>
. The code will detect all .resx
files and generate a sibling .resx.ts
file with the content.
This content can then be accessed via import { R } from "foo.resx"
. The R
object have all the translations on it, via R.someTranslation
.
If a translation name contains .
or -
, the translation will be added with the original name, but it will also follow the convention of the C# resx compiler and replace the character with _
. This means that foo.bar-baz
can be accessed both as R["foo.bar-baz"]
and R.foo_bar_baz
If the translation contains replacement points, a format-function that follows the same rules as C#'s string.Format()
is exposed along with the resources: import { R, format } from "foo.resx"
. It can also be fetched from the library directly: import { format } from "@hejdoktor/resx-compiler"
.
For a translation <data name="foo"><value>Some {0} value {1}</value></data>
, it can be used like so: format(R.foo, "bar", "baz")
.