http-loader icon indicating copy to clipboard operation
http-loader copied to clipboard

Multiple lang files

Open fplini opened this issue 7 years ago • 6 comments

Hi, is there a way to split a single lang file? I have my lang files that is going to be very large and I want to split these into multiple files for each lang. Is it possible?

Thanks Fabrizio

fplini avatar Mar 21 '18 09:03 fplini

@fplini The example we have in the README file show exactly how to do this.

tiaguinho avatar Mar 28 '18 10:03 tiaguinho

Hi @tiaguinho thanks for reply. I'm using the TranslateHttpLoader, Do you mean that I can load an array of files (suffix)?

fplini avatar Mar 28 '18 10:03 fplini

Sorry @fplini, I think I have read wrong the first time. I think it's not possible split the translates between files, by default.

Tell me one thing, do you use webpack in your project?

tiaguinho avatar Apr 11 '18 07:04 tiaguinho

Hi @tiaguinho I'm using angular cli to build my project so the answer is "yes"

fplini avatar Apr 11 '18 07:04 fplini

HI, I'm using a multi translate custom class

`import { HttpClient } from '@angular/common/http'; import { TranslateLoader } from '@ngx-translate/core'; import { forkJoin } from 'rxjs'; import { map } from 'rxjs/operators'; import * as R from 'ramda'; import { environment } from '../../environments/environment';

export function translateLoader(http: HttpClient) { return new MultiTranslateHttpLoader(http, [ { prefix: './assets/i18n/', suffix: '.json' }, { prefix: environment.translateDir, suffix: '.json' }, ]); }

export class MultiTranslateHttpLoader implements TranslateLoader { constructor( private http: HttpClient, public resources: { prefix: string, suffix: string }[] = [{ prefix: '/assets/i18n/', suffix: '.json' }]) {} public getTranslation(lang: string): any { return forkJoin(this.resources.map(config => { return this.http.get(${config.prefix}${lang}${config.suffix}); })).pipe(map(response => { return response.reduce((a, b) => { return R.mergeDeepLeft(a, b); // Object.assign(a, b); }); })); } } `

It loads an array of json, and merge those with ramda, If you didnt use nested objects you could also use Object.assign()

danielhanzs avatar Aug 13 '18 13:08 danielhanzs

I've created a lib for this: https://github.com/larscom/ngx-translate-module-loader

larscom avatar Mar 19 '19 10:03 larscom