よたらぼ
自分の興味の赴くままにIT技術系のネタを取りとめもなくメモっています。
Ruby言語やLinuxのネタが多いです。

May 12, 2008 [おもひで]

[Ruby] Ruby-GetText-Package-1.91.0 リリース!

まず、1.90.0からはRails-2.0.xのみのサポートとなってます。1.90.0をリリースしてから1.2.xで試して動かねーと言うレポートを何回か頂いたのですが、Railsの内部構造が変わった関係で(深く追えていないのですが)、とりあえず2.0.x向けに修正+テストしてます。

#どなたか1.2.xでも動作するようにしていただけたら助かります。

さて、本題。今回、仕様上の追加はほとんどしてません。しかし、1.90.0でメモリリークしていたのを修正したので1.90.0を使っている方はぜひアップデートしてください。

また、メモリリークを修正する過程で多少ですが速度改善できました。

こんなベンチマークを試してみます。

% cat benchmark.rb 
require 'benchmark'
require 'gettext'
include GetText
Benchmark.bm(18){|x|
  x.report("bindtextdomain"){ 50000.times{|i|
    bindtextdomain "test1"
  } }
  x.report("set_locale"){ 50000.times{|i|
    set_locale "ja_JP.UTF-8"
  } }
  set_locale "ja_JP.UTF-8"
  x.report("_() ja found"){ 50000.times{|i|
    _("language")
  } }
  x.report("_() ja not found"){ 50000.times{|i|
    _("language2")
  } }
  set_locale "en"
  x.report("_() en found"){ 50000.times{|i|
    _("language")
  } }
  x.report("_() en not found"){ 50000.times{|i|
    _("language2")
  } }
}

まずは1.10.0の実行結果。

                        user     system      total        real
bindtextdomain      1.290000   0.010000   1.300000 (  1.298361)
set_locale          3.530000   0.000000   3.530000 (  3.595346)
_() ja found        0.750000   0.010000   0.760000 (  0.763718)
_() ja not found    0.760000   0.000000   0.760000 (  0.761279)
_() en found        0.760000   0.000000   0.760000 (  0.768678)
_() en not found    0.770000   0.000000   0.770000 (  0.771336)

1.90.0でだいぶ改善します。が、メモリリークが・・・(しくしく)

                        user     system      total        real
bindtextdomain      0.900000   0.010000   0.910000 (  0.935536)
set_locale          3.730000   0.060000   3.790000 (  3.895379)
_() ja found        0.520000   0.000000   0.520000 (  0.550447)
_() ja not found    0.590000   0.000000   0.590000 (  0.585841)
_() en found        0.670000   0.000000   0.670000 (  0.684824)
_() en not found    0.500000   0.000000   0.500000 (  0.513706)

そして1.91.0。

                        user     system      total        real
bindtextdomain      0.930000   0.000000   0.930000 (  0.929744)
set_locale          2.720000   0.000000   2.720000 (  2.721422)
_() ja found        0.490000   0.000000   0.490000 (  0.492720)
_() ja not found    0.490000   0.000000   0.490000 (  0.486137)
_() en found        0.490000   0.000000   0.490000 (  0.489535)
_() en not found    0.490000   0.000000   0.490000 (  0.537117)

set_locale以外では1.90.0と1.91.0の違いは誤差みたいなものですが、1.10.0に比べると1.5倍以上になっています。ぜひお試しあれ。

今回の唯一の仕様追加ですが、ActionView::Helpers::FormBuilder#labelをローカライズできるようにしました。これは、cracchoさんのアイデア&コードをほとんどそのまま頂きました(ありがとうございます)。例えば以下のように使います。

  <% form_for(@article) do |f| %>
     <p><%= f.label :lastupdate %></p>
  <% end %>

で、実行結果(日本語出力の例)

<p><label for="article_lastupdate">最終更新日</label></p>

上記の例ではmsgidが"Article|Lastupdate"が表示文字列として使われます。もちろん、2つめの引数を取れば表示文字列は任意に指定できます。

  <% form_for(@article) do |f| %>
     <p><%= f.label :title, _("Foo Bar") %></p>
  <% end %>


編集