#701 Localized messages

tompalmer Wed 29 Jul 2009

What's the right way to put value strings into a localized message in Fan? For example, say my message is this in English:

Hello, $name!

But in backwards-English-land (just to avoid the effort of digging for a real example in a real language), it's:

$name, hello!

How would I store that message template and then insert the (possibly localized) runtime value into the message at the right place? I would expect to use say MessageFormat in Java or even things like printf, but I'm not sure what the right way is in Fan.

And my apologies in advance for missing it if it's something really obvious. I'm good at overlooking things sometimes.

brian Wed 29 Jul 2009

It is not done yet, I started on it back in May and eventually arrived at symbol design.

My plan is to add a new method to String called format or something:

Str<|hello $name|>.format(["Brian"])  =>  "hello Brian"

// or more likely...
@localizedHello.format(["Brian"])

Although now that the strings will be declared as symbol string literals versus prop files the $ will be annoying, so we need to decide another char to use for fill-in-the-blanks. Suggestions?

tompalmer Wed 29 Jul 2009

Thanks for the info. I think a map might be nice too (for robustness and clarity):

Str<|hello $name|>.format(["name": "Brian"])

Understood on the $ concern (both what's good and bad about it). I'm not sure what might work best. One thought that comes to mind is that maybe you could make it symbol-oriented:

"hello @name".format([@name: "Brian"])

If multiple symbols with the same name were in the message, you'd have to use the pod qualifier perhaps. Just some thoughts. Not sure it's ideal.

tompalmer Wed 29 Jul 2009

Oh, and having to define symbols for every message slot could be both annoying and helpful for consistency. I'm not sure if it would be a net win or loss. Maybe could use strings or symbols for the keys in the replacement set, even if the message format looks like symbols?

"hello @name".format(["name": "Brian"])

Mostly still just brainstorming here.

tompalmer Wed 29 Jul 2009

And hopefully my last spam for the moment. In some systems, {} surround such fields:

"hello {name}".format(["name": "Brian"])

I do like the original idea of $ as it looks like ordinary interpolation still. Could be both helpful (familiar) for learning and misleading (since it doesn't mean quite the same thing) for newcomers.

Login or Signup to reply.