Replies: 3 comments 1 reply
-
var sf = Smart.CreateDefaultSmartFormat(new SmartSettings()
{
CaseSensitivity = CaseSensitivityType.CaseInsensitive,
Formatter = new FormatterSettings { ErrorAction = FormatErrorAction.OutputErrorInResult },
Parser = new ParserSettings { ErrorAction = ParseErrorAction.OutputErrorInResult }
});
var dyn = new { Id = 4, Name = "MailSender" };
string msgNames = "An error #{Id} occurred in module '{name}'."; //named format - changed "id" to "Id"
string msgNumbers = "An error #{0} occurred in module '{1}'."; //numbered format
string expected = "An error #4 occurred in module 'MailSender'.";
string ResultNamedAnonymous = sf.Format(msgNames, dyn);
Assert.AreEqual(expected, ResultNamedAnonymous);
string ResultNumberedList = sf.Format(msgNumbers, dyn.Id, dyn.Name);
Assert.AreEqual(expected, ResultNumberedList);
string ResultNamedList = sf.Format(msgNames, dyn); // can't be numbered args here
Assert.AreEqual(expected, ResultNamedList); |
Beta Was this translation helpful? Give feedback.
-
If neglecting everything behind the ":" of the |
Beta Was this translation helpful? Give feedback.
-
Thank you for the idea about custom formatting! I have tried and it works somehow. I was not brave enough to write full formating, so I translated only message from -{id}{name}- to -{0}{1}- and then applied 2nd pass with arguments. However there is no argument number in TryEvaluateFormat() or TryEvaluateSelector() arguments (or I missed it) so it wasn't strightforward. |
Beta Was this translation helpful? Give feedback.
-
@alexheb Continue as discussion from #325 (comment)
We are thinking about using SmartFormat in a web application to solve 2 problems. We want to write messages for translations in the same format, which can be used for localisation with plurals, and also for NLog structured formatting. Some messages are used both ways. SmartFormat looks like a good aspirant. However SmartFormat does not accept NLog-style params object[] arguments for messages with named arguments - example:
Just replacing named arguments using their order would be handy (as 90% of messages have one argument), keeping named arguments in messages is essential for NLog post analysis (like Seq provides).
So I look for 2 tasks maybe:
how to force SmartFormat to pass above test,
how to create simple converter to strip additional argument formatting from message, i.e. “Error #{id:6}” becomes “Error #{id}” - just for an easy NLog application.
I would like to know if something like the above SmartFormat already contains (or if it can be somehow easily implemented (assuming that there is a parser already).
Beta Was this translation helpful? Give feedback.
All reactions