@@ -7,11 +7,14 @@ import localeData from 'dayjs/plugin/localeData';
77import relativeTime from 'dayjs/plugin/relativeTime' ;
88import utc from 'dayjs/plugin/utc' ;
99import timezone from 'dayjs/plugin/timezone' ;
10+ import { NotificationTranslationTopic , TranslationBuilder } from './TranslationBuilder' ;
1011import { defaultTranslatorFunction , predefinedFormatters } from './utils' ;
12+
1113import type { TFunction } from 'i18next' ;
1214import type momentTimezone from 'moment-timezone' ;
1315import type { TranslationLanguages } from 'stream-chat' ;
1416
17+ import type { TranslationTopicConstructor } from './TranslationBuilder' ;
1518import type { UnknownType } from '../types/types' ;
1619import type { CustomFormatters , PredefinedFormatters , TDateTimeParser } from './types' ;
1720
@@ -90,7 +93,7 @@ Dayjs.updateLocale('fr', {
9093 lastWeek : 'dddd [dernier à] LT' ,
9194 nextDay : '[Demain à] LT' ,
9295 nextWeek : 'dddd [à] LT' ,
93- sameDay : ' [Aujourd’ hui à] LT' ,
96+ sameDay : " [Aujourd' hui à] LT" ,
9497 sameElse : 'L' ,
9598 } ,
9699} ) ;
@@ -258,15 +261,16 @@ export type Streami18nOptions = {
258261 formatters ?: Partial < PredefinedFormatters > & CustomFormatters ;
259262 language ?: TranslationLanguages ;
260263 logger ?: ( message ?: string ) => void ;
264+ translationBuilderTopics ?: Record < string , TranslationTopicConstructor > ;
261265 parseMissingKeyHandler ?: ( key : string , defaultValue ?: string ) => string ;
262266 timezone ?: string ;
263267 translationsForLanguage ?: Partial < typeof enTranslations > ;
264268} ;
265269
266270/**
267- * Wrapper around [i18next](https://www.i18next.com/) class for Stream related translations .
268- * Instance of this class should be provided to Chat component to handle translations .
269- * Stream provides following list of in-built translations :
271+ * Wrapper around [i18next](https://www.i18next.com/) class for Stream related i18n .
272+ * Instance of this class should be provided to Chat component to handle i18n .
273+ * Stream provides following list of in-built i18n :
270274 * 1. English (en)
271275 * 2. Dutch (nl)
272276 * 3. Russian (ru)
@@ -330,7 +334,7 @@ export type Streami18nOptions = {
330334 * </Chat>
331335 * ```
332336 *
333- * ## Datetime translations
337+ * ## Datetime i18n
334338 *
335339 * Stream react chat components uses [dayjs](https://day.js.org/en/) internally by default to format datetime stamp.
336340 * e.g., in ChannelPreview, MessageContent components.
@@ -422,10 +426,24 @@ const defaultStreami18nOptions = {
422426 disableDateTimeTranslations : false ,
423427 language : 'en' as TranslationLanguages ,
424428 logger : ( message ?: string ) => console . warn ( message ) ,
429+ /**
430+ * Key in the translationBuilderTopics has to match postProcessorName in the translation value.
431+ *
432+ * {
433+ * "key": "{{value, postProcessorName}}"
434+ * }
435+ *
436+ * At least the default topics will be supported.
437+ */
438+ translationBuilderTopics : {
439+ notification : NotificationTranslationTopic ,
440+ } ,
425441} ;
426442
427443export class Streami18n {
428444 i18nInstance = i18n . createInstance ( ) ;
445+ translationBuilder : TranslationBuilder ;
446+ private translationBuilderTopics : Record < string , TranslationTopicConstructor > = { } ;
429447 Dayjs = null ;
430448 setLanguageCallback : ( t : TFunction ) => void = ( ) => null ;
431449 initialized = false ;
@@ -477,6 +495,7 @@ export class Streami18n {
477495 lng : string ;
478496 nsSeparator : false ;
479497 parseMissingKeyHandler ?: ( key : string , defaultValue ?: string ) => string ;
498+ postProcess ?: string [ ] ;
480499 } ;
481500 /**
482501 * A valid TZ identifier string (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
@@ -519,6 +538,11 @@ export class Streami18n {
519538 this . DateTimeParser = finalOptions . DateTimeParser ;
520539 this . timezone = finalOptions . timezone ;
521540 this . formatters = { ...predefinedFormatters , ...options ?. formatters } ;
541+ this . translationBuilder = new TranslationBuilder ( this . i18nInstance ) ;
542+ this . translationBuilderTopics = {
543+ ...defaultStreami18nOptions . translationBuilderTopics ,
544+ ...options . translationBuilderTopics ,
545+ } ;
522546
523547 try {
524548 if ( this . DateTimeParser && isDayJs ( this . DateTimeParser ) ) {
@@ -565,6 +589,12 @@ export class Streami18n {
565589 nsSeparator : false ,
566590 } ;
567591
592+ const postProcess = Object . keys ( this . translationBuilderTopics ) ;
593+
594+ if ( postProcess . length > 0 ) {
595+ this . i18nextConfig . postProcess = postProcess ;
596+ }
597+
568598 if ( finalOptions . parseMissingKeyHandler ) {
569599 this . i18nextConfig . parseMissingKeyHandler = finalOptions . parseMissingKeyHandler ;
570600 }
@@ -624,6 +654,12 @@ export class Streami18n {
624654 this . i18nInstance . services . formatter ?. add ( name , formatterFactory ( this ) ) ;
625655 } ) ;
626656 }
657+ // Register post-processors after initialization
658+ Object . entries ( this . translationBuilderTopics ) . forEach (
659+ ( [ topic , TranslationTopic ] ) => {
660+ this . translationBuilder . registerTopic ( topic , TranslationTopic ) ;
661+ } ,
662+ ) ;
627663 } catch ( error ) {
628664 this . logger ( `Something went wrong with init: ${ JSON . stringify ( error ) } ` ) ;
629665 }
0 commit comments