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

angular-lingua

v0.1.14

Published

Make @angular i18n translation simple and easy again

Downloads

7

Readme

AngularLingua

AngularLingua is an easy to use Translation Library with Typescript autocompletion and Compile-time safety that your Translation exists.

Important

If you want the Compile-time safety to work you have to build it with aot enabled.

install / integrate

First you need to install the npm module:

npm i angular-lingua

To use angular-lingua in your project you have to import the module in your AppModule and provide a default language. This can be done via a provider or through a forRoot parameter. The language can be changed at runtime.

import { NgModule } from '@angular/core';
import { TranslationModule, LANGUAGE_TOKEN } from 'angular-lingua';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    TranslationModule.forRoot(),
    ...
  ],
  providers: [
    {
      provide: LANGUAGE_TOKEN,
      useValue: 'ita'
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Translation file

All your translations are in a single Typescript file. this file could be located at: src/locales/locales.ts. important ist that it expots all the translations under 'LOCALES'.

Your translation file is composed of translations keys, whom hold key value of the lanuagekey and the actual translation. In your translations you can have variables with two curly braces. Those you can replace at runtime.

export const LOCALES =  {
  'TITLE': {
    'deu': 'übersetzung framework',
    'ita': 'quadro di traduzione',
    'fra': 'cadre de traduction'
  },
  'GREETING_MALE': {
    'deu': 'hallo herr {{NAME}}',
    'ita': 'ciao signor {{NAME}}',
    'fra': 'bonjour monsieur {{NAME}}'
  },
  'FLAG': {
    'deu': 'dies \"{{FLAG}}\" ist eine Flagge',
    'ita': 'questa \"{{FLAG}}\" è una bandiera',
    'fra': 'c\"est \"{{FLAG}}\" un drapeau'
  }
};

Usage inside template

To use the translations in the template of a component you have to copy it into a member variable. Thus is then accesable in the template.

import {Component} from '@angular/core';
import {LOCALES} from '../locales/locales';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  readonly LOCALES = LOCALES;
}

You have three options to translate in your template. With all of these options you can pass variables into the translation or fix the language.

Directive

<p [i18n]="LOCALES.TITLE"></p>

<!-- Example with Parameters -->
<p [i18n]="LOCALES.GREETING_MALE" [i18nParams]="{NAME: 'Hans Peter'}"></p>

<!-- Example with fixed language -->
<p [i18n]="LOCALES.TITLE" i18nLanguage="ita"></p>

Pipe

<p>
  {{ LOCALES.TITLE | i18n }}
</p>

<!-- Example with Parameters -->
<p>
  {{ LOCALES.GREETING_MALE | i18n: {NAME: "Hans Peter"} }}
</p>

<!-- Example with fixed language -->
<p>
  {{ LOCALES.TITLE | i18n: {} : 'ita' }}
</p>

Component

Special about the component is that you can replace your Variable placeholders with whole Components.

<translate [key]="LOCALES.TITLE">
</translate>

<!-- Example with Parameters -->
<translate [key]="LOCALES.FLAG">
  <mat-icon *appOptKey="'FLAG'">flag</mat-icon>
</translate>

<!-- Example with fixed language -->
<translate [key]="LOCALES.FLAG">
  <ng-template appOptKey="FLAG">
    <mat-icon>flag</mat-icon>
  </ng-template>
</translate>

Usage with service

you can inject the service like any other.

constructor(private translationService: TranslationService) {}
  basicExample() {
    const result = this.translationService.get(LOCALES.TITLE);
    console.log(result);
  }

  exampleWithParameters() {
    const result = this.translationService.get(LOCALES.GREETING_MALE, {NAME: 'Hans Peter'});
    console.log(result);
  }

  examplesWithFixedLanguage() {
    const result = this.translationService.get(LOCALES.TITLE, {}, 'ita');
    console.log(result);
  }

Switching language at runtime

When you change the language at runtime all the translations will be updated.

this.translationService.changeLanguage("fra");