i18n-polyfill: staticInjectorError[InjectionToken TranslationsFormat] when building with AOT
Hi
I am in the progress of testing i18n-polyfill.
After setting up my ts files with the i18n-polyfill I can extract all marked parts and build the project with:
ng xi18n -of i18n/source.xlf -f xlf --locale en
node ./node_modules/@ngx-translate/i18n-polyfill/extractor/ngx-extractor -i "src/**/*.ts" -f xlf -o src/i18n/source.xlf
After this step I inserted the “<target>…</target>” parts into src/i18n/source.xlf and saved it as src/i18n/source.en.xlf.
ng build --i18nFile src/i18n/source.en.xlf --i18nFormat xlf --locale en
The build process finished with no errors.
When opening the project with the Webbrowser I get the following error:
ERROR Error: StaticInjectorError[InjectionToken TranslationsFormat]:
StaticInjectorError[InjectionToken TranslationsFormat]:
NullInjectorError: No provider for InjectionToken TranslationsFormat!
at _NullInjector.webpackJsonp.../../../core/esm5/core.js._NullInjector.get (core.js:993)
at resolveToken (core.js:1281)
at tryResolveToken (core.js:1223)
at StaticInjector.webpackJsonp.../../../core/esm5/core.js.StaticInjector.get (core.js:1094)
at resolveToken (core.js:1281)
at tryResolveToken (core.js:1223)
at StaticInjector.webpackJsonp.../../../core/esm5/core.js.StaticInjector.get (core.js:1094)
at resolveNgModuleDep (core.js:10883)
at _createClass (core.js:10928)
at _createProviderInstance$1 (core.js:10894)
View_AppComponent_Host_0 @ AppComponent_Host.ngfactory.js? [sm]:1
proxyClass @ compiler.js:14642
webpackJsonp.../../../core/esm5/core.js.DebugContext_.logError @ core.js:15031
webpackJsonp.../../../core/esm5/core.js.ErrorHandler.handleError @ core.js:1488
(anonymous) @ core.js:5688
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:392
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run @ zone.js:142
webpackJsonp.../../../core/esm5/core.js.NgZone.runOutsideAngular @ core.js:4704
(anonymous) @ core.js:5688
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:392
onInvoke @ core.js:4756
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:391
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run @ zone.js:142
(anonymous) @ zone.js:873
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:425
onInvokeTask @ core.js:4747
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:424
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask @ zone.js:192
drainMicroTaskQueue @ zone.js:602
Promise resolved (async)
scheduleMicroTask @ zone.js:585
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ zone.js:414
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleTask @ zone.js:236
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleMicroTask @ zone.js:256
scheduleResolveOrReject @ zone.js:871
ZoneAwarePromise.then @ zone.js:981
webpackJsonp.../../../core/esm5/core.js.PlatformRef.bootstrapModule @ core.js:5574
../../../../../src/main.ts @ main.ts:11
__webpack_require__ @ bootstrap 6c8bf590f265d8ba9ec9:54
0 @ main.ts:12
__webpack_require__ @ bootstrap 6c8bf590f265d8ba9ec9:54
webpackJsonpCallback @ bootstrap 6c8bf590f265d8ba9ec9:25
(anonymous) @ main.bundle.js:1
AppComponent_Host.ngfactory.js? [sm]:1 ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 1, nodeDef: {…}, elDef: {…}, elView: {…}}
View_AppComponent_Host_0 @ AppComponent_Host.ngfactory.js? [sm]:1
proxyClass @ compiler.js:14642
webpackJsonp.../../../core/esm5/core.js.DebugContext_.logError @ core.js:15031
webpackJsonp.../../../core/esm5/core.js.ErrorHandler.handleError @ core.js:1493
(anonymous) @ core.js:5688
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:392
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run @ zone.js:142
webpackJsonp.../../../core/esm5/core.js.NgZone.runOutsideAngular @ core.js:4704
(anonymous) @ core.js:5688
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:392
onInvoke @ core.js:4756
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke @ zone.js:391
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.run @ zone.js:142
(anonymous) @ zone.js:873
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:425
onInvokeTask @ core.js:4747
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask @ zone.js:424
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.runTask @ zone.js:192
drainMicroTaskQueue @ zone.js:602
Promise resolved (async)
scheduleMicroTask @ zone.js:585
webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.scheduleTask @ zone.js:414
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleTask @ zone.js:236
webpackJsonp.../../../../zone.js/dist/zone.js.Zone.scheduleMicroTask @ zone.js:256
scheduleResolveOrReject @ zone.js:871
ZoneAwarePromise.then @ zone.js:981
webpackJsonp.../../../core/esm5/core.js.PlatformRef.bootstrapModule @ core.js:5574
../../../../../src/main.ts @ main.ts:11
__webpack_require__ @ bootstrap 6c8bf590f265d8ba9ec9:54
0 @ main.ts:12
__webpack_require__ @ bootstrap 6c8bf590f265d8ba9ec9:54
webpackJsonpCallback @ bootstrap 6c8bf590f265d8ba9ec9:25
(anonymous) @ main.bundle.js:1
main.ts:12 Error: StaticInjectorError[InjectionToken TranslationsFormat]:
StaticInjectorError[InjectionToken TranslationsFormat]:
NullInjectorError: No provider for InjectionToken TranslationsFormat!
at _NullInjector.webpackJsonp.../../../core/esm5/core.js._NullInjector.get (core.js:993)
at resolveToken (core.js:1281)
at tryResolveToken (core.js:1223)
at StaticInjector.webpackJsonp.../../../core/esm5/core.js.StaticInjector.get (core.js:1094)
at resolveToken (core.js:1281)
at tryResolveToken (core.js:1223)
at StaticInjector.webpackJsonp.../../../core/esm5/core.js.StaticInjector.get (core.js:1094)
at resolveNgModuleDep (core.js:10883)
at _createClass (core.js:10928)
at _createProviderInstance$1 (core.js:10894)
I tried this with a new angular project (ng new i18nTest). The changed files are the following: app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { I18n } from '@ngx-translate/i18n-polyfill';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [I18n],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts
import {Component} from '@angular/core';
import {I18n} from '@ngx-translate/i18n-polyfill';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title: string;
constructor(i18n: I18n) {
this.title = i18n('app');
}
}
app.component.html
<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
<h1 i18n>
Welcome to {{ title }}!
</h1>
<img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
</div>
<h2 i18n>Here are some links to help you start: </h2>
<ul>
<li>
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial" i18n>Tour of Heroes</a></h2>
</li>
<li>
<h2><a target="_blank" rel="noopener" href="https://github.com/angular/angular-cli/wiki" i18n>CLI Documentation</a></h2>
</li>
<li>
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/" i18n>Angular blog</a></h2>
</li>
</ul>
As I don’t have a idea where a problem on my siede could be I fear it is a problem with the polyfill. Any Ideas?
Regards Bernd
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 4
- Comments: 18 (9 by maintainers)
Yes sorry, I will update the docs, you need to define those if you don’t use the angular cli + aot:
Those are the exact same options that you need to define for angular i18n, see the docs on angular.io: https://angular.io/guide/i18n#merge-with-the-jit-compiler
What if you add the providers (for translations, …) to your main module as well?
@vqoph Not sure if you got it working. If not, or maybe for the next person to find this thread looking for help, here’s what finally worked for me.
I’m passing in the
locale
andi18nFormat
as args to my build/serve commands and found I don’t need to add them to myapp.module.ts
’sproviders
. I only ended up needing to provideTRANSLATIONS
as @ocombe mentioned. I also needed and couldn’t find a documented way to load the translation files dynamically so I used a factory loader like:My project is structured like:
To test out translations I can serve it like:
ng serve --aot --i18nFile=src/locale/messages.fr.xlf --i18nFormat=xlf --locale=fr
it’s working with JIT compilation ! thanks a lot 😃 I’m now trying with aot
my app.module.ts (for others) :
This is nice! Could you improve the documentation of the project (readme) to explain all this?
I use ng cli (tested with 1.5.0 and 1.6.0 version and Angular 5.0.0 and 5.1.0 version) and aot compiler, and i still have an error
I tried with the i18n example from
https://angular.io/guide/i18n
. Added my translations tested on both aot and jit with specific language providers . Ran the project with commands below and everything is working as expected (if I don’t inject the I18n service)note that
MISSING_TRANSLATION_STRATEGY
seems to not already exist on@angular/core
, I needed to import it from@ngx-translate/i18n-polyfill
then I inject
i18n
in my component controller, as this :add the provider
providers: [I18n],
in myapp.module.ts
and on both JIT and AOT (tried with and without additional providers). I have this error now :Does this help ? What am i doing wrong ?