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

@wolve/translate

v1.0.5

Published

This library was created to work well with signals in angular 17.3.0 and above.

Downloads

4

Readme

Translate

This library was created to work well with signals in angular 17.3.0 and above.

Installation

Install the package

npm install --save-exact @wolve/translate

Add it to your angular project:

export const appConfig: ApplicationConfig = {
  providers: [
    
    // ...

    // This will add all the required services to your module.
    // It is recommended that you provide them in your root module.
    provideTranslations({
      defaultLanguage: 'en',
      supportedLanguages: ['en', 'de', 'fr', 'es', 'it'],
      filePrefix: '.json',
      urlToI18n: 'assets/i18n'
    }),

    // ...
  ]
};

with this config you the module will expect translation files in the folder 'src/assets/i18n' (or in release build 'assets/i18n') for each language a separate file structured as json is expected.

Usage

get a translated value

The recommended usage is to translate using the translate service. Therefore there are 3 methods available:

  1. get

returns a signal to a translation. If the language changes signal will be notified and updated


import { TranslateService } from '@wolve/translate';
import { inject, Signal } from '@angular/core';

// ...

private readonly translateService = inject(TranslateService);

// this requires a format like '{"path": {"to": {"translation": {"key": "your translation!"}}}}' in the translation json
public readonly translation: Signal<string> = this.translateService.get('path.to.translation.key');

// ...
  1. instant

Returns the translated key once in real time. If the selected language changes the value will NOT change


import { TranslateService } from '@wolve/translate';
import { inject } from '@angular/core';

// ...

private readonly translateService = inject(TranslateService);

public readonly translation: string = this.translateService.instant('path.to.translation.key');

// ...
  1. stream

creates an observable to the translated value. If the language changes the observable will be notified


import { TranslateService } from '@wolve/translate';
import { inject } from '@angular/core';
import { interval } from 'rxjs';

// ...

private readonly translateService = inject(TranslateService);

public readonly translation: Observable<string | null> = this.translateService.stream('path.to.translation.key');

// ...

select the language

only languages that are provided as supported or default languages in the initialization of the module can be selected.


import { TranslateService } from '@wolve/translate';

// ...

private readonly translateService = inject(TranslateService);

// ...

// all the language will be loaded if it has never been selected before and signals/observables will be notified
this.translateService.selectLanguage('fr');

translate pipe


@Component({
  standalone: true,
  selector: 'app-translate-pipe-demo',
  templateUrl: './translate-pipe-demo.component.html',
  styleUrls: ['./translate-pipe-demo.component.scss'],
  imports: [
    TranslatePipe,
  ],
})
export class TranslatePipeDemoComponent {}

template:

<p> {{ 'path.to.translate.key' | translate }} </p>

with parameters.

<p> {{ 'path.to.translate.key' | translate: {param1:'Some custom value'} }} </p>

translate directive

@Component({
  standalone: true,
  selector: 'app-translate-pipe-demo',
  templateUrl: './translate-pipe-demo.component.html',
  styleUrls: ['./translate-pipe-demo.component.scss'],
  imports: [
    TranslatePipe,
  ],
})
export class TranslatePipeDemoComponent {}

the template:

<p translate>path.to.translate.key</p>

OR

<p translate="path.to.translate.key"></p>

with parameters

<p translate [translateParams]="{param1: 'ParamValue'}">path.to.translate.key</p>

OR

<p translate="path.to.translate.key" [translateParams]="{param1: 'ParamValue'}"></p>

translation parameters

parameters are passed as Record<string, string> to the methods

tranlsation json format

consider you have a path like 'path.to.your.translations'

this will expect a json file like

{
    "path": {
        "to": {
            "your": {
                "translation": "Thats the actual translation"
            }
        }
    }
}

for non static translations you can provide custom parameters like:

{
    "translation": {
        "withParams": "There are {{numberOfEntries}} entries missing"
    }
}

calling translateService.instant('translation.withParams', {numberOfEntries: '2'}); will translate to 'There are 2 entries missing'