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

ng2-intl-tb

v1.0.9

Published

Internationalize Angular2 apps. This library provides Angular2 components, pipe and an API to format dates, numbers, and strings, including pluralization and handling translations.

Downloads

5

Readme

Ng2 Intl

Inspired by ng2-translate (setup is similary) and react-intl (same api)

Internationalize Angular2 apps. This library provides angular2 components, pipe and an API to format dates, numbers, and strings, including pluralization and handling translations.

npm Version Dependency Status

Overview

Ng2 Intl use FormatJS. It provides bindings to angular 2 via its components, pipe and API.

Features

  • Display numbers with separators.
  • Display dates and times correctly.
  • Display dates relative to "now".
  • Pluralize labels in strings.
  • Support for 150+ languages.
  • Runs in the browser and Node.js.
  • Built on standards.

Installation

First you need to install the npm module:

npm install ng2-intl --save

Usage

1. Import the IntlModule:

Finally, you can use ng2-intl in your Angular 2 project. It is recommended to import IntlModule.forRoot() in the NgModule of your application.

The forRoot method is a convention for modules that provide a singleton service (such as the Angular 2 Router), you can also use it to configure the IntlModule loader. By default it will use the IntlStaticLoader, but you can provide another loader instead as a parameter of this method (see below Write & use your own loader).

For now ng2-intl requires HttpModule from @angular/http (this will change soon).

import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from '@angular/core';
import {HttpModule} from '@angular/http';
import {IntlModule} from 'ng2-intl';

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        IntlModule.forRoot()
    ],
    bootstrap: [AppComponent]
})
export class AppModule {
}

If you have multiple NgModules and you use one as a shared NgModule (that you import in all of your other NgModules), don't forget that you can use it to export the IntlModule that you imported in order to avoid having to import it multiple times.

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        IntlModule.forRoot()
    ],
    exports: [BrowserModule, HttpModule, IntlModule],
})
export class SharedModule {
}

By default, only the IntlStaticLoader is available. It will search for files in i18n/*.json, if you want you can customize this behavior by changing the default prefix/suffix:

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        IntlModule.forRoot({ 
          provide: IntlLoader,
          useFactory: (http: Http) => new IntlStaticLoader(http, '/assets/i18n', '.json'),
          deps: [Http]
        })
    ],
    exports: [BrowserModule, HttpModule, IntlModule],
})
export class SharedModule {
}

2. Init the IntlService for your application:

import {Component} from '@angular/core';
import {IntlService} from 'ng2-intl';

@Component({
    selector: 'app',
    template: `
        <div>{{ 'HELLO' | formattedMessage:{values: param} }}</div>
    `
})
export class AppComponent {
    param: string = "world";

    constructor(intlService: IntlService) {
        // this language will be used as a fallback when a translation isn't found in the current language
        intlService.setDefaultLang('en');
		
		// initialize the download of the default lang
		intlService.start();

         // the lang to use, if the lang isn't available, it will use the current loader to get them
        intlService.use('fr');
    }
}

Write & use your own loader

If you want to write your own loader, you need to create a class that implements IntlLoader. The only required method is getTranslation that must return an Observable. If your loader is synchronous, just use Observable.of to create an observable from your static value.

Example
class CustomLoader implements IntlLoader {
    getTranslation(lang: string): Observable<any> {
        return Observable.of({"KEY": "Value"});
    }
}

Once you've defined your loader, you can provide it in your NgModule by adding it to its providers property. Don't forget that you have to import IntlModule as well:

@NgModule({
    imports: [
        BrowserModule,
        IntlModule.forRoot({ provide: IntlLoader, useClass: CustomLoader })
    ],
    exports: [IntlModule],
})
export class SharedModule {
}

How to handle missing translations

You can setup a provider for MissingTranslationHandler in the bootstrap of your application (recommended), or in the providers property of a component. It will be called when the requested translation is not available. The only required method is handle where you can do whatever you want. If this method returns a value or an observable (that should return a string), then this will be used. Just don't forget that it will be called synchronously from the instant method.

Example:

Create a Missing Translation Handler

import {MissingTranslationHandler, MissingTranslationHandlerParams} from 'ng2-intl';

export class MyMissingTranslationHandler implements MissingTranslationHandler {
  handle(params: MissingTranslationHandlerParams) {
      return 'some value';
  }
}

Setup the Missing Translation Handler in your NgModule (recommended) by adding it to its providers property:

{ provide: MissingTranslationHandler, useClass: MyMissingTranslationHandler }