@@ -742,70 +742,70 @@ JSON.stringify(strings.reduce((acc: object, s: { en_string: string }): object =>
742742 // in this plugin we will use plugin to fill the database with missing language messages
743743 if ( ! this . externalAppOnly ) {
744744 this . tryProcessAndWatch ( adminforth ) ;
745- }
746745
747- adminforth . tr = async ( msg : string | null | undefined , category : string , lang : string , params , pluralizationNumber : number ) : Promise < string > => {
748- if ( ! msg ) {
749- return msg ;
750- }
746+ adminforth . tr = async ( msg : string | null | undefined , category : string , lang : string , params , pluralizationNumber : number ) : Promise < string > => {
747+ if ( ! msg ) {
748+ return msg ;
749+ }
751750
752- if ( category === 'frontend' ) {
753- throw new Error ( `Category 'frontend' is reserved for frontend messages, use any other category for backend messages` ) ;
754- }
755- // console.log('🪲tr', msg, category, lang);
751+ if ( category === 'frontend' ) {
752+ throw new Error ( `Category 'frontend' is reserved for frontend messages, use any other category for backend messages` ) ;
753+ }
754+ // console.log('🪲tr', msg, category, lang);
756755
757- // if lang is not supported , throw
758- if ( ! this . options . supportedLanguages . includes ( lang as LanguageCode ) ) {
759- lang = 'en' ; // for now simply fallback to english
756+ // if lang is not supported , throw
757+ if ( ! this . options . supportedLanguages . includes ( lang as LanguageCode ) ) {
758+ lang = 'en' ; // for now simply fallback to english
760759
761- // throwing like line below might be too strict, e.g. for custom apis made with fetch which don't pass accept-language
762- // throw new Error(`Language ${lang} is not entered to be supported by requested by browser in request headers accept-language`);
763- }
760+ // throwing like line below might be too strict, e.g. for custom apis made with fetch which don't pass accept-language
761+ // throw new Error(`Language ${lang} is not entered to be supported by requested by browser in request headers accept-language`);
762+ }
764763
765- let result ;
766- // try to get translation from cache
767- const cacheKey = `${ resourceConfig . resourceId } :${ category } :${ lang } :${ msg } ` ;
768- const cached = await this . cache . get ( cacheKey ) ;
769- if ( cached ) {
770- result = cached ;
771- }
772- if ( ! result ) {
773- const resource = adminforth . resource ( resourceConfig . resourceId ) ;
774- const translation = await resource . get ( [ Filters . EQ ( this . enFieldName , msg ) , Filters . EQ ( this . options . categoryFieldName , category ) ] ) ;
775- if ( ! translation ) {
776- await resource . create ( {
777- [ this . enFieldName ] : msg ,
778- [ this . options . categoryFieldName ] : category ,
779- } ) ;
780- this . updateUntranslatedMenuBadge ( ) ;
764+ let result ;
765+ // try to get translation from cache
766+ const cacheKey = `${ resourceConfig . resourceId } :${ category } :${ lang } :${ msg } ` ;
767+ const cached = await this . cache . get ( cacheKey ) ;
768+ if ( cached ) {
769+ result = cached ;
781770 }
771+ if ( ! result ) {
772+ const resource = adminforth . resource ( resourceConfig . resourceId ) ;
773+ const translation = await resource . get ( [ Filters . EQ ( this . enFieldName , msg ) , Filters . EQ ( this . options . categoryFieldName , category ) ] ) ;
774+ if ( ! translation ) {
775+ await resource . create ( {
776+ [ this . enFieldName ] : msg ,
777+ [ this . options . categoryFieldName ] : category ,
778+ } ) ;
779+ this . updateUntranslatedMenuBadge ( ) ;
780+ }
782781
783- // do this check here, to faster register missing translations
784- // also not cache it - no sense to cache english strings
785- if ( lang === 'en' ) {
786- // set to cache to return faster next time
787- result = msg ;
788- } else {
789- result = translation ?. [ this . trFieldNames [ lang ] ] ;
790- if ( ! result ) {
791- // return english
782+ // do this check here, to faster register missing translations
783+ // also not cache it - no sense to cache english strings
784+ if ( lang === 'en' ) {
785+ // set to cache to return faster next time
792786 result = msg ;
787+ } else {
788+ result = translation ?. [ this . trFieldNames [ lang ] ] ;
789+ if ( ! result ) {
790+ // return english
791+ result = msg ;
792+ }
793793 }
794+ // cache so even if key does not exist, we will not hit database
795+ await this . cache . set ( cacheKey , result ) ;
796+ }
797+ // if msg has '|' in it, then we need to aplly pluralization
798+ if ( msg . includes ( '|' ) ) {
799+ result = this . applyPluralization ( result , pluralizationNumber , lang ) ;
794800 }
795- // cache so even if key does not exist, we will not hit database
796- await this . cache . set ( cacheKey , result ) ;
797- }
798- // if msg has '|' in it, then we need to aplly pluralization
799- if ( msg . includes ( '|' ) ) {
800- result = this . applyPluralization ( result , pluralizationNumber , lang ) ;
801- }
802801
803- if ( params ) {
804- for ( const [ key , value ] of Object . entries ( params ) ) {
805- result = result . replace ( `{${ key } }` , value ) ;
802+ if ( params ) {
803+ for ( const [ key , value ] of Object . entries ( params ) ) {
804+ result = result . replace ( `{${ key } }` , value ) ;
805+ }
806806 }
807+ return result ;
807808 }
808- return result ;
809809 }
810810 }
811811
0 commit comments