Skip to content

Feature Request: String? getMessageByKey(String messageKey) #12

@henry2man

Description

@henry2man

TD;LR Read the title, I just want to have a dynamic way to resolve messages (if they exists) during runtime.

Full story:

I have a backend that is going to send both info & error messages using message-keys, ex: "{'id':302, 'error': 'itemNotFound'}". I want to dynamically resolve those keys using generated code but, for now, I don't have a method that can be used in order to solve the error message.

It would be great to have a method in the S class that would allow me to get the translated message if it exists, and if it doesn't, I can manually generate a generic message:

...
} catch BackendError {
  String? message  = getMessageByKey(errorKey);
  if(message == null) {
     print("ERROR: Unknown error: $errorKey")
  } else {
     print("ERROR: $message");
  }
}

Having such method probably will require the generation of a Map<String, String Function()> static member that will look like something like this:

{ 
  'itemNotFound': () => this.itemNotFound,
  'forbidden': () => this.forbidden,
  'notAuthorized': () => this.notAuthorized,
  ....
}

I've done something similar in bdlukaa/fluent_ui@b53288b#diff-ab7c9e25f588ae440e0de59542af84db24d60f230b9c7143d42b1afa9bc45da4R47 (In that case the motivation to generate a map was also to be able to search for the "icons" by name)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions