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

@zeferinix/ngx-fluent

v4.0.0

Published

An Angular library for Fluent

Downloads

18

Readme

ngx-fluent

An Angular library for Fluent.

Angular compatibility

Latest version available for each version of Angular

| @zeferinix/ngx-fluent | Angular | @fluent/bundle | | --------------------- | ------- | -------------- | | >= 1.0 | 13.x | < 1.x | | >= 1.1 | 14.x | < 1.x | | >= 1.2 | 15.x | < 1.x | | >= 2.0 | 16.x | < 1.x | | >= 3.0 | 17.x | < 1.x | | >= 4.0 | 18.x | < 1.x |

Installation

npm install --save @fluent/bundle @zeferinix/ngx-fluent 

Usage

Initialize module

Import the library into your app module:

import { NgxFluentModule } from '@zeferinix/ngx-fluent';

@NgModule({
  imports: [
    // ... your other module imports
    NgxFluentModule,
  ],
})

Register locale source mapping and initial locale

The library needs to know where to locate the .ftl files so you have to provide this first (preferably on the root of your app such as app.component.ts) by providing a flat object where the key is the locale code and the value is the source then passing this object to the setTranslationSourceMap() method of the service.

Note: Translation sources are lazy loaded then cached in memory. Translation source for the respective locale is only loaded after calling the setLocale() method.

Tip: Make your locale keys compliant to the BCP 47 standard as much as possible so that you don't encounter potential issues when using Fluent's built-in functions since they make use of the Intl API which also relies on the same standard. For example, see Intl.NumberFormat()'s locales parameter.

import { Component, OnInit } from '@angular/core';
import { NgxFluentService } from '@zeferinix/ngx-fluent';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  constructor(private fluentService: NgxFluentService) {}

  ngOnInit() {
    this.fluentService.setTranslationSourceMap({
      en: 'assets/i18n/en.ftl', // could be on your assets folder
      sv: 'https://my.domain.com/translations/sv.ftl', // or external provided you don't get CORS issues
    });

    this.fluentService.setLocale('en');  // set initial locale
  }
}

If you want to pass a config to the FluentBundle constructor, you can do so by passing an object instead.

export class AppComponent implements OnInit {
  constructor(private fluentService: NgxFluentService) {}

  ngOnInit() {
    this.fluentService.setTranslationSourceMap({
      en: { 
        path: 'assets/i18n/en.ftl', // URL or path to the translation source
        bundleConfig: { // Pass FluentBundle config here
          useIsolating: false,
        }, 
      },
    });

    this.fluentService.setLocale('en');  // set initial locale
  }
}

If you want more granular control over the FluentBundle instances, you can pass it directly.

export class AppComponent implements OnInit {
  constructor(private fluentService: NgxFluentService) {}

  ngOnInit() {
    // Initialize your bundle somewhere
    const bundle = new FluentBundle('en', {
      useIsolating: false,
    });
    const resource = new FluentResource('welcome-user = Welcome, {$user}!');
    bundle.addResource(resource);

    this.fluentService.setTranslationSourceMap({
      en: bundle, // add it to the map, make sure the key is the same as the locale
    });

    this.fluentService.setLocale('en');  // set initial locale
  }
}

You can add multiple sources later as well if needed.

export class AppComponent implements OnInit {
  constructor(private fluentService: NgxFluentService) {}

  ngOnInit() {
    this.fluentService.setTranslationSourceMap({
      en: 'assets/i18n/en.ftl',
    });

    this.fluentService.setLocale('en');  // set initial locale

    // ... After some time ...
    
    this.fluentService.setTranslationSourceMap({
      sv: 'assets/i18n/sv.ftl',
    });
  }
}

Switching locale

Switching to another language is as simple as calling the setLocale() method and passing the new locale.

export class MyComponent {
  switchLocale(locale: string) {
    this.fluentService.setLocale(locale);
  }
}

Pipe

Use the fluent pipe.

{{ 'welcome-user' | fluent: { user: 'John Done' } }}

Note: The pipe will return the key if it fails to resolve.

Programmatically via service

You can call the translate() method on the service to translate a message by passing the message key and optional arguments for interpolation.

Note: The service will return null if it fails to resolve.

export class MyComponent {
  async translate(key: string, args?: Record<string, any>) {
    const translation = await this.fluentService.translate(key, args);
    console.log(translation);
    return translation;
  }
}

Contributing

See Contributing Guide.