2012-03-10 33 views
11

Ruby 1.9.3p0でRails 3.1.3を使用する。Railsロケールファイルで補間された変数の大文字小文字を変更するにはどうすればよいですか?

私は、デフォルトでフォームのボタンに文例を使用しないことを発見しました。たとえば、「ユーザーの更新」ボタンの代わりに、「ユーザーの更新」ボタンを生成します。

ボタン名はActionView locale fileに由来します。モデル名を変更するデフォルトを作成する方法はありますか?これはInterpolationのRuby on Rails Guides i18nセクションではカバーされていないので、おそらく不可能です。以下は動作しません:

en: 
    helpers: 
    submit: 
     update: 'Update %{model}.downcase' 

は一般的に、私は、ロケールYAMLファイルの構文については言及を見つけるために喜んでいると思います。 i18nガイドはいくつかの構文をカバーしていますが、すべての感嘆符、さまざまな日付/時刻書式などのドキュメントを見つけると役に立ちます。あるいは、YAMLファイルの代わりにRuby Hashを使用する必要がありますか?

答えて

8

さらに調査した結果、少なくともYAMLロケールファイルを使用すると、このような補間値の操作は不可能であると結論づけました。

YAMLがここに文書化され、文字列操作をサポートしていません:
http://www.yaml.org/spec/1.2/spec.html

Rubyのローカライズのメインページはこちらです:
http://ruby-i18n.org/wiki

そこから、我々はデフォルトの国際化のためのコードを見つけますgemと補間コードまでドリルダウンします。これは、補間を行うためにsprintfを使用しています。
https://github.com/svenfuchs/i18n/blob/master/lib/i18n/interpolate/ruby.rb

このコードが「重く正夫武藤のgettext文字列の補間の拡張子に基づいて」されています。それ拡張子が番号をフォーマットする例があり
http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb

For strings. 
"%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"} 

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

拡張子は、[Ruby] "Kernel::sprintf"というフォーマット文字列の詳細を参照しています。 sprintf上のドキュメントで http://www.ruby-doc.org/core-1.9.2/Kernel.html#method-i-sprintf


、数値をフォーマットする方法の多くが、文字列の大文字と小文字を変更するための操作があります。

1

I18n補間を変更することでこの問題を解決しました。あなたの初期化子ディレクトリに以下のコードを入れてください:

create: 'Opprett %{model.downcase}' 

し、それをテストします:

module I18n 
    # Implemented to support method call on translation keys 
    INTERPOLATION_WITH_METHOD_PATTERN = Regexp.union(
    /%%/, 
    /%\{(\w+)\}/,        # matches placeholders like "%{foo}" 
    /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/, # matches placeholders like "%<foo>.d" 
    /%\{(\w+)\.(\w+)\}/,       # matches placeholders like "%{foo.upcase}" 
) 

    class << self 
    def interpolate_hash(string, values) 
     string.gsub(INTERPOLATION_WITH_METHOD_PATTERN) do |match| 
     if match == '%%' 
      '%' 
     else 
      key = ($1 || $2 || $4).to_sym 
      value = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(values, string)) 
      value = value.call(values) if value.respond_to?(:call) 
      $3 ? sprintf("%#{$3}", value) : ($5 ? value.send($5) : value) 
     end 
     end 
    end 
    end 
end 

、これはあなたのロケールファイルに何ができるかである

require 'test_helper' 

class I18nTest < ActiveSupport::TestCase 

    should 'interpolate as usual' do 
    assert_equal 'Show Customer', I18n.interpolate("Show %{model}", model: 'Customer') 
    end 

    should 'interpolate with number formatting' do 
    assert_equal 'Show many 100', I18n.interpolate("Show many %<kr>2d", kr: 100) 
    assert_equal 'Show many abc', I18n.interpolate("Show many %<str>3.3s", str: 'abcde') 
    end 

    should 'support method execution' do 
    assert_equal 'Show customer', I18n.interpolate("Show %{model.downcase}", model: 'Customer') 
    end 

end 
関連する問題