@@ -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,14 @@ const defaultStreami18nOptions = {
422426 disableDateTimeTranslations : false ,
423427 language : 'en' as TranslationLanguages ,
424428 logger : ( message ?: string ) => console . warn ( message ) ,
429+ translationBuilderTopics : {
430+ notifications : NotificationTranslationTopic ,
431+ } ,
425432} ;
426433
427434export class Streami18n {
428435 i18nInstance = i18n . createInstance ( ) ;
436+ translationBuilder : TranslationBuilder ;
429437 Dayjs = null ;
430438 setLanguageCallback : ( t : TFunction ) => void = ( ) => null ;
431439 initialized = false ;
@@ -477,6 +485,7 @@ export class Streami18n {
477485 lng : string ;
478486 nsSeparator : false ;
479487 parseMissingKeyHandler ?: ( key : string , defaultValue ?: string ) => string ;
488+ translationBuilderTopics : Record < string , TranslationTopicConstructor > ;
480489 } ;
481490 /**
482491 * A valid TZ identifier string (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
@@ -519,6 +528,7 @@ export class Streami18n {
519528 this . DateTimeParser = finalOptions . DateTimeParser ;
520529 this . timezone = finalOptions . timezone ;
521530 this . formatters = { ...predefinedFormatters , ...options ?. formatters } ;
531+ this . translationBuilder = new TranslationBuilder ( this . i18nInstance ) ;
522532
523533 try {
524534 if ( this . DateTimeParser && isDayJs ( this . DateTimeParser ) ) {
@@ -563,6 +573,10 @@ export class Streami18n {
563573 keySeparator : false ,
564574 lng : this . currentLanguage ,
565575 nsSeparator : false ,
576+ translationBuilderTopics : {
577+ ...defaultStreami18nOptions . translationBuilderTopics ,
578+ ...options . translationBuilderTopics ,
579+ } ,
566580 } ;
567581
568582 if ( finalOptions . parseMissingKeyHandler ) {
@@ -624,6 +638,12 @@ export class Streami18n {
624638 this . i18nInstance . services . formatter ?. add ( name , formatterFactory ( this ) ) ;
625639 } ) ;
626640 }
641+ // Register post-processors after initialization
642+ Object . entries ( this . i18nextConfig . translationBuilderTopics ) . forEach (
643+ ( [ topic , TranslationTopic ] ) => {
644+ this . translationBuilder . register ( topic , TranslationTopic ) ;
645+ } ,
646+ ) ;
627647 } catch ( error ) {
628648 this . logger ( `Something went wrong with init: ${ JSON . stringify ( error ) } ` ) ;
629649 }
0 commit comments