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

rollup-plugin-ng-i18n

v0.0.2

Published

A plugin for Rollup that allows you to use the $localize global constant built by the Angular team in any vite/rollup build setting to localize builds.

Downloads

2

Readme

About this Package

This is a plugin for Vite/Rollup allowing you to use the Angular Localize i18n package (@angular/localize) in Vite/Rollup.

The @angular/localize package is unique from many i18n solutions in that it focuses on providing different compilations for each target language. This allows us to improve app performance and reduce the hassle associated with i18n.

Usage

This package must be used alongside the @angular/localize package and you must include the @angular/localize/init package in your build.

// Or in main.ts, index.ts, etc.
import './wc-lit-todos';

You can then use the $localize global variable provided by the @angular/localize to mark tags for translation.

import { LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators.js";

@customElement("wc-greeting")
export class WcGreeting extends LitElement {
  @property() user = '(;)
  render() {
    return html`<h1>${$localize`Hello, ${this.user}`}</h1>`;
  }
}

Unfortunately, the i18n tag is not supported as in Angular.

Building

There are two parts to using the package.

  1. Generating base translation files
  2. Using translations to generate builds

Generating base translation files

Just like in the Angular version, you must first generate a base messages file to write translations for. This can be done through using the @angular/localize bin function localize-extract or programmatically using the plugin in a vite configuration file as seen below:

import { defineConfig } from 'vite';
import { ngi18n } from 'rollup-plugin-ng-i18n';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    ngi18n({
      extract: { localeOutput: 'messages.json', format: 'json' },
      sourceLocale: 'en',
    }),
  ],
});

The base messages file will be outputted to your output folder along with regular build files. I suggest committing this to your project file in a directory like locale/messages.json.

Using Translations to Build

Next, we can translate the base translation file to our target files.

locale/messages.ja.json

{
  "locale": "ja",
  "translations": {
    "4584092443788135411": "こんにちは {$PH}"
  }
}

locale/messages.es.json

{
  "locale": "es",
  "translations": {
    "4584092443788135411": "Hola {$PH}"
  }
}

We must then reference this translations in our vite config as seen below:

import { defineConfig } from 'vite';
import { ngi18n } from 'rollup-plugin-ng-i18n';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    ngi18n({
      translate: {
        translationFilePaths: ['./locale/ja.json', './locale/es.json'],
        translationFileLocales: ['ja', 'es'],
      },
      sourceLocale: 'en',
    }),
  ],
});

Running vite with the above config will yield us with three subfolders in our build folder: es/index.js, ja/index.js and the original en/index.js.

License

Distributed under the MIT License.

I take no responsibility for any breaking changes.

Accredation

This package uses the source code in the Angular GitHub Repository maintained by Google. The @angular/localize source code is not commited to this package; it is used as a submodule to ensure maintainability.

I personally want to thank the Angular team for Angular, and the brilliant approach to i18n they devised.

Also of course a big thanks to the Vite and Rollup crew for providing a greate build platform that can be extended like this.