Skip to content

Instantly share code, notes, and snippets.

@mateuszwrobel
Created June 15, 2022 10:06
Show Gist options
  • Save mateuszwrobel/d4f0194c99da8d2ba8601108af06e273 to your computer and use it in GitHub Desktop.
Save mateuszwrobel/d4f0194c99da8d2ba8601108af06e273 to your computer and use it in GitHub Desktop.
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TRANSLOCO_SCOPE, TranslocoModule } from '@ngneat/transloco';
@NgModule({
imports: [
TranslocoModule,
],
declarations: [],
providers: [
{
provide: TRANSLOCO_SCOPE,
useValue: {
scope: 'scopeName',
loader: scopeLoader((lang, root) => import(`./${root}/${lang}.json`)),
isLoaded: false
},
multi: true
}
]
})
export class AngularModule {}
import {
translate,
TRANSLOCO_SCOPE,
TranslocoService
} from '@ngneat/transloco';
export class AngularService {
isTranslationsLoaded = false;
private translationSubscription: Subscription;
constructor(
private translateService: TranslocoService,
@Inject(TRANSLOCO_SCOPE) private scope
) {
if (this.scope.isLoaded) {
this.isTranslationsLoaded = true;
return;
}
this.translationSubscription = this.translateService.events$
.pipe(
filter(e => {
const scopes = this.scope.map(i => i.scope);
return (
e.payload.scope === 'scopeName' &&
e.type === 'translationLoadSuccess' &&
scopes.includes(e.payload.scope)
);
})
)
.subscribe(() => {
this.isTranslationsLoaded = true;
this.scope.isLoaded = true;
});
}
}
import { langs } from '../../../../../transloco.config';
export const scopeLoader = (importer, root = 'i18n') => {
return langs.reduce((acc, lang) => {
acc[lang] = () => importer(lang, root);
return acc;
}, {});
};
module.exports = {
langs: ['en', 'pl'],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment