Sam Sungshik Kong wrote in [ruby-talk:102028]:
s = “My name is %(name)s and my age is %(age)d.” % {“name”:
“Sam”, “age”: 34}
I know that ruby has “#{name}” expression. But that requires a
variable named “name” in advance. I want to bind the format
string and data later.
I proposed that feature once in [ruby-dev:16351], though
rejected, but still I think that it would be useful for I18N. Is
it worth for RCR?
Do you have an example on how would you use this? I ask because I
would also vote strongly against it.
For I18N, a facility like this is essential, as different languages
may require words in different orders. My standard I18N technique
these days is to have a Messages hash that is replaced by a language
module (cf. Ruwiki’s lib/ruwiki/lang/en.rb). In it, I have:
:no_template_found =>
“No template of %s found in template set %s.”
In lib/ruwiki/config.rb, I have:
raise ConfigError, message[:no_template_found] %
[kind.inspect, @template_set] unless TEMPLATES.include?(kind)
# kind = :edit
# @template_set = "default"
Basically, this entirely depends on the word order for the template
set and the kind of template always being the same. If, instead, I
were to change my :no_template_found value to:
“Template set %s does not include template %s.”
My output would no longer be correct. If, however, I were able to
do:
“Template set %(set) does not include template %(kind).”
This would not be an issue, because I would simply change my call to
be:
raise ConfigError, message[:no_template_found] %
{“kind” => kind.inspect, “set” => @template_set} unless
TEMPLATES.include?(kind)
I have had to resort to some rather artificial and stilted wordings
because of the limitations inherent in the current approach. I’m not
sure that the form %(key) is the best – one loses the best part of
%-formatting (easy tabular format) unless one can include full format
specifiers:
%5.2f{money} OR %{money}5.2f
I think that whatever solution is reached, that should be used –
but I desparately need the ability to format this stuff, because the
language files are separate and must stay that way to be properly
I18Ned.
Nothing about this would change that the #{foo} form is preferred
for many things in Ruby, and it has nothing to do with clarity: it
has everything to do, from my perspective, with I18N and L10N
issues. For that reason alone, we need this in core.
-austin
···
–
austin ziegler * austin.ziegler@evault.com