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

@ngneers/signal-translate

v0.1.4

Published

Translation service that is using signals at its core.

Downloads

85

Readme

npm Build Status bundle size

signal-translate

Provides an easy way to provide translations in the form of signals in an Angular application.

Motivation 💥

Signals are a powerful way to provide state in an Angular application. They are a great way to provide translations as well. Unfortunately, traditional translation libraries are not designed to work with signals and instead rely on Observables. Also these libraries typically do not provide type safety for translations.

Features 🔥

✅ Easy to setup and use

✅ Type safe translations

✅ ESM & CJS exports

This library provides a way to easily provide translations in the form of signals.

Built With 🔧

Usage Example 🚀

import { Injectable, inject, signal } from '@angular/core';
import { BaseTranslateService, InterpolatePipe, interpolate } from '@ngneers/signal-translate';

import type translations from './en.json';

@Injectable({ providedIn: 'root' })
export class TranslateService extends BaseTranslateService<
  // Provide the type of the translations object for one language
  typeof translations
> {
  constructor() {
    super(
      // Provide the available languages
      ['en', 'de'],
      // Provide the initial language (if null the browser language (navigator.language) is used)
      'en'
    );
  }

  protected loadTranslations(lang: string): Promise<typeof translations> {
    // Load the translations for the given language
    switch (lang) {
      case 'en':
        return import('./en.json').then(x => x.default);
      case 'de':
        return import('./de.json').then(x => x.default);
      default:
        // Only languages that are in the available languages array are passed
        // so this should never happen
        throw new Error(`Language ${lang} is not supported`);
    }
  }
}

// Usage
@Component({
  selector: 'app-root',
  standalone: true,
  imports: [InterpolatePipe],
  template: `
    <!-- Just call the signal for the wanted translation. -->
    <h1>{{ translations.hello() }}</h1>

    <!--
      You can also pass parameters to the signal to interpolate translations
      `{{ name }}` in the translation will be replaced with the value of the name property
    -->
    <h2>{{ translations.greetings({ name: 'John' }) }}</h2>

    <!-- You can also use the interpolate pipe to interpolate strings -->
    <h2>{{ "Hello {{name}}!" | interpolate: { name: 'John' } }}</h2>

    <!--
      You can access translations unsafely with an unspecified string using the _unsafe property.
      This is useful for translations that are not known at compile time.
    -->
    <h2>{{ translations._unsafe["status_" + status()]() }}</h2>

    <button (click)="setLanguage('de')">Change language to german</button>
  `,
})
export class AppComponent {
  private readonly _translateService = inject(TranslateService);

  // Retrieve the translations signal
  protected readonly translations = _translateService.translations;

  protected readonly status = signal<string>('vip');

  public setLanguage(lang: string): void {
    // Set the language
    this._translateService.setLanguage(lang);
  }

  public greet(): void {
    // You can interpolate translations by passing the values as the argument
    // `{{ name }}` in the translation will be replaced with the value of the name property
    alert(this.translations.greetings({ name: 'John' }));

    // You can also interpolate strings using the interpolate function
    alert(interpolate("Hello {{name}}!", { name: 'John' }));
  }
}

Strongly typed translations 📚

The IDE will provide autocompletion for the translations. Autocompletion

Contributing 🧑🏻‍💻

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License 🔑

Distributed under the MIT License. See LICENSE.txt for more information.