import { I18n , Options , TranslationFunction , TranslationOptions , i18n , } from '@typed/i18next' ; import { Stream , map } from 'most' ; import { hold , sync } from 'most-subject' ; export type I18nSource = ( key : string , translationOptions ? : TranslationOptions ) => Stream < string > ; export function makeI18nDriver ( plugins = [ ] , options ? ) { return function i18nDriver ( sink$ ) : I18nSource { const language$ = sink$ . thru ( hold ( 1 ) ) ; language$ . drain ( ) ; const translationFn$ = hold < TranslationFunction > ( 1 , sync < TranslationFunction > ( ) ) ; plugins . reduce ( ( i18next , plugin ) => i18next . use ( plugin ) , i18n ) . init ( options , function ( ) { language$ . observe ( ( language ) => { i18n . changeLanguage ( language , function ( err , t ) { if ( err ) return translationFn$ . error ( err ) ; translationFn$ . next ( t ) ; } ) ; } ) ; } ) ; return function i18nSource ( key , translationOptions ? ) { return map ( ( t ) => t ( key , translationOptions ) , translationFn$ ) ; } ; } ; }