Frimlik
Frimlik

Reputation: 429

Ngx-translate: Change path to localization files

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.

Upvotes: 2

Views: 4687

Answers (2)

Enrique González
Enrique González

Reputation: 241

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');
}

Upvotes: 6

Zepse Wolf
Zepse Wolf

Reputation: 1

Ok i did a little digging and realize that ngx-translate uses the browser's default naming convention. If you look at ngx-translate/core/src/lib/translate.service.ts Line 506

So if u want to change it's naming conversion you will have to do a lot of editing to the original ngx-translate module.

I will recommend you to continue using it's recommended naming convention. Or write a script to adopt to it's naming convention as the module is using the name convention of browsers.

I hope this will help you, good luck.

Upvotes: 0

Related Questions