Created
June 15, 2022 10:06
-
-
Save mateuszwrobel/d4f0194c99da8d2ba8601108af06e273 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { langs } from '../../../../../transloco.config'; | |
export const scopeLoader = (importer, root = 'i18n') => { | |
return langs.reduce((acc, lang) => { | |
acc[lang] = () => importer(lang, root); | |
return acc; | |
}, {}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
langs: ['en', 'pl'], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment