Issue
In ngx-translate
library, is there a way how to change the path where TranslateService
looks for localization files (en-GB
etc.)? The default one is src/assets/i18n/[lang].json
.
I followed this tutorial.
Solution
Yes, you can change the path to the translation files by creating a loader in your app.module.ts, as described in the documentation:
https://github.com/ngx-translate/core#configuration
i.e:
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpClientModule, HttpClient} from '@angular/common/http';
import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
import {AppComponent} from './app';
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: httpTranslateLoader,
deps: [HttpClient]
}
})
],
bootstrap: [AppComponent]
})
export class AppModule { }
// Set the path here
export function httpTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/locales/', '.json');
}
I would suggest using the environments to set the locales path:
export function httpTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, environment.languageFilesPath, '.json');
}
Answered By - Enrique González
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.