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
npm install ngx-translate-messageformat-compiler
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
93 Stars
156 Commits
30 Forks
7 Watching
1 Branches
12 Contributors
Updated on 30 Jun 2024
TypeScript (92.39%)
JavaScript (7.61%)
Cumulative downloads
Total Downloads
Last day
-0.1%
7,410
Compared to previous day
Last week
9%
39,355
Compared to previous week
Last month
10.9%
155,862
Compared to previous month
Last year
30.1%
1,609,663
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 {}
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' }">people</li> 8 <li translate [translateParams]="{ gender: 'male', how: 'funny' }">people</li> 9 <li>{{'people' | translate:"{ how: 'affectionate' }"}}</li> 10</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 dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/11 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
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
51 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
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@ngx-translate/core
Translation library (i18n) for Angular
@messageformat/core
PluralFormat and SelectFormat Message and i18n Tool - A JavaScript Implemenation of the ICU standards.
@ngx-translate/http-loader
http loader for dynamically loading translation files for @ngx-translate/core
ngx-translate-messageformat-compiler-ivy
Compiler for ngx-translate that uses messageformat.js to compile translations using ICU syntax for handling pluralization and gender