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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ngx-material-translate

v1.0.2

Published

The internationalization (i18n) library (including @angular/material, @ngrx/store, @ngrx/effects, @ngrx/entity) for Angular.

Downloads

27

Readme

ngx-material-translate

The internationalization (i18n) library (including @angular/material, @ngrx/store, @ngrx/effects, @ngrx/entity) for Angular.

Official documentation: https://astritdemiri.com/ng-library/ngx-material-translate

Simple example using ngx-material-translate: https://stackblitz.com/github/astritdemiri11/ngx-material-translate-example

Get the complete changelog here: https://github.com/astritdemiri11/ngx-material-translate/releases

Table of Contents

Installation

First you need to install the npm module:

npm install ngx-material-translate --save

Choose the version corresponding to your Angular version:

Angular | ngx-material-translate ------------- | --------------- 14 (ivy only) | 1.x+

Usage

1. Import the MaterialTranslateModule:

Finally, you can use ngx-material-translate in your Angular project. You have to import MaterialTranslateModule in the root NgModule of your application.

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {MaterialTranslateModule, TranslationLoaderService} from 'ngx-material-translate';

@NgModule({
    imports: [
        BrowserModule,
        MaterialTranslateModule.forRoot({
          loader: { provide: TranslationLoaderService, useClass: CustomTranslationLoader }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }
SharedModule

If you use a SharedModule that you import in multiple other feature modules, you can export the MaterialTranslateModule to make sure you don't have to import it in every module.

@NgModule({
    imports: [
      MaterialTranslateModule.forChild({
        loader: { provide: TranslationLoaderService, useClass: CustomTranslationLoader }
      })
    ],
    exports: [
        CommonModule,
        MaterialTranslateModule
    ]
})
export class SharedModule { }

Note: Never call a forRoot static method in the SharedModule. You might end up with different instances of the service in your injector tree. But you can use forChild if necessary.

Lazy loaded modules

When you lazy load a module, you should use the forChild static method to import the MaterialTranslateModule.

Since lazy loaded modules use a different injector from the rest of your application, you can configure them separately with a different loader translations handler.

@NgModule({
    imports: [
        MaterialTranslateModule.forChild({
            loader: { provide: TranslationLoaderService, useClass: CustomTranslationLoader }
        })
    ]
})
export class LazyLoadedModule { }
Configuration

By default, there is no loader available. You can add translations manually using addTranslations but it is better to use a loader. You can write your own loader, or import an existing one. For example you can use the HttpClient that will load translations from files using HttpClient.

Once you've decided which loader to use, you have to setup the MaterialTranslateModule to use it.

Here is how you would use the HttpClient to load translations from "/assets/i18n/[lang].json" ([lang] is the lang that you're using, for english it could be en):

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpClientModule, HttpClient} from '@angular/common/http';
import {MaterialTranslateModule, TranslationLoaderService} from 'ngx-material-translate';
import {AppComponent} from './app.component';

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        MaterialTranslateModule.forRoot({
            loader: { provide: TranslationLoaderService, useFactory: (http: HttpClient) => new TranslationLoaderService(http), deps: [HttpClient] }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }
AoT

If you want to configure a custom TranslationLoaderService while using AoT compilation or Ionic, you must use an exported function instead of an inline function.

export function userTranslationLoader(http: HttpClient) {
    return new TranslationLoaderService(http);
}

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        MaterialTranslateModule.forRoot({
            loader: {
                provide: TranslationLoaderService,
                useFactory: (userTranslationLoader),
                deps: [HttpClient]
            }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

2. Define the default language for the application

@NgModule({
    imports: [
        BrowserModule,
        MaterialTranslateModule.forRoot({
            defaultLanguage: LanguageCode.English
        })
    ],
    providers: [

    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

3. Init the LanguageService and TranslateService for your application:

import {Component} from '@angular/core';
import {INTERNAL_FEATURE, LanguageCode, LanguageService, TranslateService} from 'ngx-material-translate';

@Component({
    selector: 'app',
    template: `
        <div>{{ 'HELLO' | translate }}</div>
    `
})
export class AppComponent {
    constructor(languageService: LanguageService, translateService: TranslateService) {
        languageService.business.selectLanguage(LanguageCode.English);

        //Listen for language change.
        languageService.model.activeLanguageCode$.subscribe(languageCode => {
          languageService.business.translateLanguages();


          //Loads the MatLanguageComponent label translations.
          translateService.business.loadTranslations(INTERNAL_FEATURE);
          //Loads the translations and sets to the custom feature feature.
          translateService.business.loadTranslations('feature');
          //Loads the translations and sets to the default feature 'app'.
          translateService.business.loadTranslations();
        });
    }
}

4. Define the translations:

Once you've imported the MaterialTranslateModule, you can put your translations in a json file that will be imported with the TranslationLoaderService. The following translations should be stored in en.json.

{
    "HELLO": "hello"
}

You can also define your translations manually with addTranslations. Translations will be added to the selected language used.

translate.addTranslations({
    HELLO: 'hello'
});

You can then access the value by using the dot notation, in this case HELLO.

5. Use the service, the pipe, the directive or the component:

You can either use the LanguageService, the TranslateService, the TranslatePipe, the TranslateDirective or the MatLanguageComponent to get your translation values or languages.

With the service, it looks like this:

let selectedLanguage = language.business.getActiveLanguage();

languageService.model.activeLanguage$.subscribe(language => {
  selectedLanguage = language;
});
let translation = translateService.business.getTranslation('app', 'hello');

translationService.model.languageTranslations('app').subscribe(translations => {
  translation = translations['hello']; 
});

This is how you do it with the pipe to display translation:

<div>{{ 'HELLO' | translate }}</div>

This is how you use the directive to display translation:

<div [translate]="'HELLO'"></div>

This is how you use the component to visualize languages:

<mat-language></mat-language>

6. Use HTML tags:

You can easily use raw HTML tags within your translations.

{
    "HELLO": "<strong>Some html text</strong>"
}

To render them, simply use the innerHTML attribute with the pipe on any element.

<div [innerHTML]="'HELLO' | translate"></div>