
Security News
NVD Concedes Inability to Keep Pace with Surging CVE Disclosures in 2025
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
ngx-translate-messageformat-compiler
Advanced tools
Compiler for ngx-translate that uses messageformat.js to compile translations using ICU syntax for handling pluralization and gender
Compiler for ngx-translate that uses messageformat.js to compile translations using ICU syntax for handling pluralization and gender
This assumes that you've already installed ngx-translate.
Using npm
:
npm install ngx-translate-messageformat-compiler messageformat --save
... or if you use yarn
:
yarn add ngx-translate-messageformat-compiler messageformat
Changed dependencies for v4:
You need to use Angular v6 and ngx-translate v10 for this version.
Intl
is expected to be present by the new messageformat version so the corresponding config option has been dropped.
See CHANGELOG for more details.
Changed dependencies for v3: You need to use Angular v4/v5 and ngx-translate v8/v9 for this version. See CHANGELOG for more details.
Changed setup for v2:
You no longer need to provide a MessageFormat instance.
The compiler will do this. You still need to have messageformat
installed, of course.
See CHANGELOG for more details.
You need to configure TranslateModule
so it uses TranslateMessageFormatCompiler
as the compiler:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { TranslateCompiler, TranslateModule } from '@ngx-translate/core';
import { TranslateMessageFormatCompiler } from 'ngx-translate-messageformat-compiler';
import { AppComponent } from "./app";
@NgModule({
imports: [
BrowserModule,
TranslateModule.forRoot({
compiler: {
provide: TranslateCompiler,
useClass: TranslateMessageFormatCompiler
}
})
],
bootstrap: [AppComponent]
})
export class AppModule {}
MessageFormat instances provide some methods to influence its behaviour, among them setBiDiSupport
, and setStrictNumberSign
. Learn about their meaning here: https://messageformat.github.io/messageformat/MessageFormat
You can override the values used when configuring MessageFormat by providing a configuration object for the MESSAGE_FORMAT_CONFIG
injection token. Here's the default:
{
biDiSupport: false,
strictNumberSign: false
}
This is how you would enable bi-directional support, for example:
import { MESSAGE_FORMAT_CONFIG } from 'ngx-translate-messageformat-compiler';
@NgModule({
// ...
providers: [
{ provide: MESSAGE_FORMAT_CONFIG, useValue: { biDiSupport: true }}
]
})
}
This library implements neither the syntax used for pluralization (et al) nor the "mechanics" for making translations work in your Angular app. The former is MessageFormat, the latter ngx-translate. Before you assume your problem is with ngx-translate-messageformat-compiler, please consult these ressources:
Here's two important differences to ngx-translate's default syntax when using MessageFormat:
'Hello {name.first} {name.last}'
won't work.Hello {name}
This library also exports TranslateMessageFormatDebugCompiler
, which you can use as a drop-in replacement for the regular TranslateMessageFormatCompiler
.
The debug compiler will log to the console whenever a translation string is compiled to an interpolation function, and whenever such a function is called (with interpolation parameters) to compute the final translated string.
The logs may help you figuring out which translation produces an error and the timing of when the individual steps happen.
Here's an example to get you started:
{
"things": "There {count, plural, =0{is} one{is} other{are}} {count, plural, =0{} one{a} other{several}} {count, plural, =0{nothing} one{thing} other{things}}",
"people": "{gender, select, male{He is} female{She is} other{They are}} {how}"
}
<ul>
<li translate [translateParams]="{ count: 0 }">things</li>
<li translate [translateParams]="{ count: 1 }">things</li>
<li>{{'things' | translate:"{ count: 2 }"}}</li>
</ul>
<ul>
<li translate [translateParams]="{ gender: 'female', how: 'influential' }">people</li>
<li translate [translateParams]="{ gender: 'male', how: 'funny' }">people</li>
<li>{{'people' | translate:"{ how: 'affectionate' }"}}</li>
</ul>
Note that this illustrates using both the directives and the pipe provided by ngx-translate. You don't have to mix them, obviously.
- There is nothing
- There is a thing
- There are several things
- She is influential
- He is funny
- They are affectionate
If you're here, you probably know what you're looking for. If you do wonder what this is, here's a brief explanation.
ICU Message Format is a standardized syntax for dealing with the translation of user-visible strings into various languages that may have different requirements for the correct declension of words (e.g. according to number, gender, case) - or to simplify: pluralization.
Messageformat.js is a compliant implementation for Javascript.
Back in AngularJS, angular-translate, formerly by @PascalPrecht, provided support for ICU syntax using messageformat.js. This compiler "plugin" adds the same rich pluralization support to the excellent ngx-translate for Angular (2+). Thanks to @ocombe for his work and his supporting pluggable compilers in the core. Thanks also to @PascalPrecht for suggesting a contribution when I talked to him about this at Jazoon.
[4.1.2] - 2018-05-28
FAQs
> Compiler for ngx-translate that uses messageformat.js to compile translations using ICU syntax for handling pluralization and gender
The npm package ngx-translate-messageformat-compiler receives a total of 42,362 weekly downloads. As such, ngx-translate-messageformat-compiler popularity was classified as popular.
We found that ngx-translate-messageformat-compiler demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.