@@ -64,8 +64,8 @@ export class ReactLocalization {
6464 ) : string {
6565 const bundle = this . getBundle ( id ) ;
6666 if ( bundle ) {
67- const msg = bundle . getMessage ( id ) ;
68- if ( msg && msg . value ) {
67+ const msg = bundle . getMessage ( id ) ! ;
68+ if ( msg . value ) {
6969 let errors : Array < Error > = [ ] ;
7070 let value = bundle . formatPattern ( msg . value , vars , errors ) ;
7171 for ( let error of errors ) {
@@ -74,26 +74,51 @@ export class ReactLocalization {
7474 return value ;
7575 }
7676 } else {
77- if ( this . areBundlesEmpty ( ) ) {
78- this . reportError (
79- new Error (
80- "Attempting to get a string when no localization bundles are " +
81- "present."
82- )
83- ) ;
84- } else {
85- this . reportError (
86- new Error (
87- `The id "${ id } " did not match any messages in the localization ` +
88- "bundles."
89- )
90- ) ;
91- }
77+ const msg = this . areBundlesEmpty ( )
78+ ? "Attempting to get a string when no localization bundles are present."
79+ : `The id "${ id } " did not match any messages in the localization bundles.` ;
80+ this . reportError ( new Error ( msg ) ) ;
9281 }
9382
9483 return fallback || id ;
9584 }
9685
86+ getFormattedMessage (
87+ id : string ,
88+ vars ?: Record < string , FluentVariable > | null
89+ ) : {
90+ value : string | null ;
91+ attributes ?: Record < string , string > ;
92+ } {
93+ const bundle = this . getBundle ( id ) ;
94+ if ( bundle === null ) {
95+ const msg = this . areBundlesEmpty ( )
96+ ? "Attempting to get a localized message when no localization bundles are present."
97+ : `The id "${ id } " did not match any messages in the localization bundles.` ;
98+ this . reportError ( new Error ( msg ) ) ;
99+ return { value : null } ;
100+ }
101+
102+ let value : string | null = null ;
103+ let attributes : Record < string , string > | null = null ;
104+ const msg = bundle . getMessage ( id ) ! ;
105+ let errors : Array < Error > = [ ] ;
106+ if ( msg . value ) {
107+ value = bundle . formatPattern ( msg . value , vars , errors ) ;
108+ }
109+ if ( msg . attributes ) {
110+ attributes = Object . create ( null ) as Record < string , string > ;
111+ for ( const [ name , pattern ] of Object . entries ( msg . attributes ) ) {
112+ attributes [ name ] = bundle . formatPattern ( pattern , vars , errors ) ;
113+ }
114+ }
115+ for ( let error of errors ) {
116+ this . reportError ( error ) ;
117+ }
118+
119+ return attributes ? { value, attributes } : { value } ;
120+ }
121+
97122 getElement (
98123 sourceElement : ReactElement ,
99124 id : string ,
@@ -105,26 +130,13 @@ export class ReactLocalization {
105130 ) : ReactElement {
106131 const bundle = this . getBundle ( id ) ;
107132 if ( bundle === null ) {
108- if ( ! id ) {
109- this . reportError (
110- new Error ( "No string id was provided when localizing a component." )
111- ) ;
112- } else if ( this . areBundlesEmpty ( ) ) {
113- this . reportError (
114- new Error (
115- "Attempting to get a localized element when no localization bundles are " +
116- "present."
117- )
118- ) ;
119- } else {
120- this . reportError (
121- new Error (
122- `The id "${ id } " did not match any messages in the localization ` +
123- "bundles."
124- )
125- ) ;
126- }
127-
133+ // eslint-disable-next-line no-nested-ternary
134+ const msg = ! id
135+ ? "No string id was provided when localizing a component."
136+ : this . areBundlesEmpty ( )
137+ ? "Attempting to get a localized element when no localization bundles are present."
138+ : `The id "${ id } " did not match any messages in the localization bundles.` ;
139+ this . reportError ( new Error ( msg ) ) ;
128140 return createElement ( Fragment , null , sourceElement ) ;
129141 }
130142
0 commit comments