npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hejdoktor/resx-compiler

v0.3.1

Published

Compiles a resx file into typescript

Downloads

68

Readme

resx-compiler

Compiles a resx file into typescript.

The compiled file will expose the available translations, so that the typescript compiler can help enforce valid usage.

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.

Multiple languages

If there are multiple locales for the .resx files (eg. foo.resx and foo.en.resx or bar.da.resx and bar.en.resx), the system requires a default locale to be set. This is done by adding the --default-locale <locale> option:

npx resx-compiler --default-locale en <source folder>

This will result in multiple files being created for each resource, so if da and en are the available locales, three files will be created:

  • foo.resx.ts
  • foo.da.resx.ts
  • foo.en.resx.ts

All can be imported, but only the base file is recommended. It loads the correct resources from the current locale, which can be set by importing setLocale and then calling it with the correct locale:

import { setLocale } from "@hejdoktor/resx-compiler"

setLocale("da")

It defaults to the --default-locale option until it is called the first time.

The supported set of translations are defined by the default locale, and any missing keys in other locales will automatically get the value from the default translation.

Optimizing builds

Translations can take up a lot of space, and users typically only need one language at a time. The resulting file can be optimized by running the bundler with the enforcedLocale environment variable set to the appropriate locale. This will ensure that the other languages cannot be chosen, and tree shaking from the optimizer will remove non-matching translations.

This should be used to produce one bundle per locale, which the server can then load directly to serve as little code as possible to a client.

Unusual translation names

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

Replacement strings

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").