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-intl

v2.0.1

Published

A lightweight internationalization library for Angular applications

Downloads

38

Readme

angular-intl Build Status npm version

A lightweight internationalization library for Angular applications.

Versions

| Angular | angular-intl | | --------- | --------------- | | v6 | >=2.0.0 | | v5 | 1.0.2 - 1.1.12 |

Setup

First, import the TranslateModule in your root module, like so:

@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    TranslateModule.forRoot({ path: '/assets/languages/' })
  ]
})
export class AppModule {
  constructor(public translateService: TranslateService) {
    const defaultPrefix = 'default';
    const browserLanguage = this.translationService.getBrowserLanguage();
    this.translationService.setDefault(`${defaultPrefix}-${browserLanguage}`); // eg. 'default-en'
  }
}

This bootstraps the translation pipe, directive and service at the root level, making them available throughout the application.

In the /assets/languages directory, you should have .json files that contain the key/value pairs for your translations.

eg. '/assets/languages/default-en.json':

{
  "BODY": {
    "TITLE": "Title Translation",
    "DESCRIPTION": "Description translation",
    "GREETING": "Hello, {{ firstName }}!"
  }
}

Usage

Once bootstrapped, you can use the service, pipe and directive to make translations throughout your codebase.

Pipe example:

<!-- without parameters -->
<p>{{ 'BODY.TITLE' | translate }}</p>

<!-- with parameters -->
<p>{{ 'BODY.GREETING' | translate:{ firstName: 'Linda' } }}</p>

Directive example:

<!-- without parameters -->
<p translate>BODY.TITLE</p>

<!-- with parameters -->
<p [translate]="{ firstName: 'Linda' }">BODY.GREETING</p>

Service example:

// for one translation
this.translateService.get('BODY.TITLE')
  .subscribe(translatedTitle => this.title = translatedTitle);
  
// for multiple translations
this.translateService.get(['BODY.TITLE', 'BODY.DESCRIPTION'])
  .subscribe(translations => {
    this.title = translations['BODY.TITLE'];
    this.description = translations['BODY.DESCRIPTION'];
  });

Default overrides are also available, so after calling setDefault, you may call setOverride, which will be used first, and then fall back to the default if the key is not found in the language file specified in setOverride:

eg. '/assets/languages/override-en.json':

{
  "BODY": {
    "TITLE": "Title Translation Override",
  }
}
this.translateService.setDefault('default-en');
this.translateService.setOverride('override-en');

This will output 'BODY.TITLE' = 'Title Translation Override'(from override-en.json), and 'BODY.DESCRIPTION' = 'Description translation' (from default-en.json)

Alternatively, you can get a translation without enforcing an entire file override, by getting a translation by file. This uses the translation if it is already loaded, otherwise it requests the file and uses the value that corresponds to the key, all without loading that file as an override or making it default:

this.translateService.getByFileName('BODY.TITLE', 'alternate-en') // where 'alternate-en' is yet another language file
  .subscribe(translatedTitle => this.title = translatedTitle);

API

getBrowserLanguage(): string

  • This returns the current browser language code.

setDefault(fileName: string): void

  • Sets the default language. This can be used on its own, or as a fallback.

setOverride(fileName: string): void

  • Sets the language over the default. This should only be used once a default language has been specified, and does not need to be used unless you intend to override a default translation file.

translationsLoaded: BehaviorSubject<boolean>

  • This is used to determine exactly when translations have arrived (from the network call after setDefault or setOverride have been executed). You can subscribe to this if you wish to perform operations when translations are loaded throughout the lifecycle of the application.

get(keyPaths: string | Array<string>): Observable<TranslationResult>

  • Accepts a string or an array of strings. Returns an Observable that contains a string, or an object of strings keyed by the original translation key path (ex. 'BODY.TITLE'). This will only return values once translations have loaded, so it is safe to use anywhere.

getByFileName(keyPaths: string | Array<string>, fileName: string): Observable<TranslationResult>

  • Unlike setDefault or setOverride, which are globally applied, this method will load a translation file and return the translated key without enforcing that language globally. Like get, it can accept a string or an array of strings and returns in the same pattern.