Gathering detailed insights and metrics for ngx-translate-messageformat-compiler
Gathering detailed insights and metrics for ngx-translate-messageformat-compiler
Gathering detailed insights and metrics for ngx-translate-messageformat-compiler
Gathering detailed insights and metrics for ngx-translate-messageformat-compiler
ngx-translate-formatjs-compiler
[Format.js](https://formatjs.io/) based [@ngx/translate](https://github.com/ngx-translate/core) compiler.
@kimondd/ngx-translate-messageformat-compiler
> Compiler for ngx-translate that uses messageformat.js to compile translations using ICU syntax for handling pluralization and gender
ngx-translate-messageformat-compiler-ivy
Compiler for ngx-translate that uses messageformat.js to compile translations using ICU syntax for handling pluralization and gender
ngx-translate-messageformat-compiler-custom
> Compiler for ngx-translate that uses messageformat.js to compile translations using ICU syntax for handling pluralization and gender
Advanced pluralization (and more) for ngx-translate, using standard ICU syntax which is compiled with the help of messageformat.js.
npm install ngx-translate-messageformat-compiler
Typescript
Module System
Node Version
NPM Version
TypeScript (92.39%)
JavaScript (7.61%)
Total Downloads
7,107,716
Last Day
1,020
Last Week
38,755
Last Month
174,867
Last Year
1,886,233
MIT License
97 Stars
169 Commits
30 Forks
6 Watchers
1 Branches
13 Contributors
Updated on Jan 29, 2025
Minified
Minified + Gzipped
Latest Version
7.1.0
Package Id
ngx-translate-messageformat-compiler@7.1.0
Unpacked Size
29.18 kB
Size
8.75 kB
File Count
10
NPM Version
10.9.2
Node Version
23.5.0
Published on
Dec 28, 2024
Cumulative downloads
Total Downloads
Last Day
-15.6%
1,020
Compared to previous day
Last Week
-11.8%
38,755
Compared to previous week
Last Month
-3.9%
174,867
Compared to previous month
Last Year
38.2%
1,886,233
Compared to previous year
1
3
Compiler for ngx-translate that uses messageformat.js to compile translations using ICU syntax for handling pluralization and gender
Example App (StackBlitz)
This assumes that you've already installed ngx-translate.
Using npm
:
1npm install ngx-translate-messageformat-compiler @messageformat/core --save
... or if you use yarn
:
1yarn add ngx-translate-messageformat-compiler @messageformat/core
Something to be aware of if you deploy to strict production environments: Fundamentally, messageformat is a compiler that turns ICU MessageFormat input into JavaScript, and we do this at runtime. This means calling new Function
under the hood, which requires allowing unsafe-eval
for the script-src
Content Security Policy (CSP).
In the current version, this library supports Angular versions 13+, ngx-translate versions 14+ and messageformat 3. Older versions of this library support older versions of these peer dependencies.
You need to configure TranslateModule
so it uses TranslateMessageFormatCompiler
as the compiler:
1import { NgModule } from "@angular/core"; 2import { BrowserModule } from "@angular/platform-browser"; 3import { TranslateCompiler, TranslateModule } from "@ngx-translate/core"; 4import { TranslateMessageFormatCompiler } from "ngx-translate-messageformat-compiler"; 5 6import { AppComponent } from "./app"; 7 8@NgModule({ 9 imports: [ 10 BrowserModule, 11 TranslateModule.forRoot({ 12 compiler: { 13 provide: TranslateCompiler, 14 useClass: TranslateMessageFormatCompiler, 15 }, 16 }), 17 ], 18 bootstrap: [AppComponent], 19}) 20export class AppModule {}
Check the ngx-translate documentation for an example when using a "standalone" setup.
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:
1{ 2 biDiSupport: false, 3 formatters: {}, 4 strictNumberSign: false, 5 currency: "USD", 6 strictPluralKeys: true, 7 throwOnError: false, 8 fallbackPrefix: undefined 9}
MessageFormat instances provide some options to influence its behaviour, among them customFormatters
, biDiSupport
and strict
. Learn about their meaning here: https://messageformat.github.io/messageformat/api/core.messageformatoptions/ (The names used in the MESSAGE_FORMAT_CONFIG object are slightly different for backward-compatibility reasons.)
This is how you would enable bi-directional support and add a custom formatter, for example:
1import { MESSAGE_FORMAT_CONFIG } from 'ngx-translate-messageformat-compiler'; 2 3@NgModule({ 4 // ... 5 providers: [{ 6 provide: MESSAGE_FORMAT_CONFIG, 7 useValue: { 8 biDiSupport: true, 9 formatters: { upcase: v => v.toUpperCase() } 10 } 11 }]
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}
If you have to transition on a message-by-message basis, you can do so by configuring a prefix that, if found on the message, will cause the compiler to "ignore" the message. This has the effect of falling back on ngx-translate's default message interpolation.
1import { MESSAGE_FORMAT_CONFIG } from 'ngx-translate-messageformat-compiler'; 2 3@NgModule({ 4 // ... 5 providers: [{ 6 provide: MESSAGE_FORMAT_CONFIG, 7 useValue: { 8 fallbackPrefix: 'your_choice::' 9 } 10 }]
1{ 2 "uses-messageformat-syntax": "{ COUNT, plural, =0 {There are no results.} one {There is one result.} other {There are # results.}", 3 "uses-default-syntax": "'your_choice::Hello {{name}}." 4}
There are two stages in the translation process:
Hello {name}
) to a function: this fails if the MessageFormat syntax is incorrect, for example.Linda
as the name in the above message): this fails if the parameters don't "fit" the message.By default, the errors that get thrown in these two stages are caught and logged to the console, and the original message is returned as the translation. If you do not want this behaviour, pass throwOnError: true
in MESSAGE_FORMAT_CONFIG
(see above). (Note that this may make all translations fail if there's a syntax error in any message.)
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:
1{ 2 "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}}", 3 "people": "{gender, select, male{He is} female{She is} other{They are}} {how}" 4}
1<ul> 2 <li translate [translateParams]="{ count: 0 }">things</li> 3 <li translate [translateParams]="{ count: 1 }">things</li> 4 <li>{{'things' | translate:"{ count: 2 }"}}</li> 5</ul> 6<ul> 7 <li translate [translateParams]="{ gender: 'female', how: 'influential' }"> 8 people 9 </li> 10 <li translate [translateParams]="{ gender: 'male', how: 'funny' }">people</li> 11 <li>{{'people' | translate:"{ how: 'affectionate' }"}}</li> 12</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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 2/12 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
16 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More