How to change case of interpolated variables in Rails locale file?

前端 未结 2 1061
南笙
南笙 2021-02-08 04:50

Using Rails 3.1.3 with Ruby 1.9.3p0.

I\'ve discovered that by default, Rails does not use sentence case for form buttons. For example, instead of an \"Update user\" but

2条回答
  •  灰色年华
    2021-02-08 05:18

    After further research, I've concluded that this kind of operation on interpolated values is not possible, at least using a YAML locale file.

    YAML is documented here and doesn't support string operations:
    http://www.yaml.org/spec/1.2/spec.html

    The main page on Ruby localization is here:
    http://ruby-i18n.org/wiki

    From there, we find the code for the default I18n gem and drill down to the interpolation code. It uses sprintf to do the interpolation:
    https://github.com/svenfuchs/i18n/blob/master/lib/i18n/interpolate/ruby.rb

    That code is "heavily based on Masao Mutoh's gettext String interpolation extension":
    http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb

    That extension has an example of formatting numbers:

    For strings.
    "%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
    
    With field type to specify format such as d(decimal), f(float),...
    "%d, %.1f" % {:age => 10, :weight => 43.4}
    

    The extension refers to the [Ruby] "Kernel::sprintf for details of the format string":
    http://www.ruby-doc.org/core-1.9.2/Kernel.html#method-i-sprintf

    In that doc on sprintf, there are lots of ways to format numbers, but no operations for changing the case of strings.

提交回复
热议问题