i18nizer

どんな文字列もi18n な表記に変えてしまうスクリプトRubyで書いてみた.

#!/usr/bin/env ruby

# i18nのような表記に変換するブロック
i18nize=lambda do | token |
	if token.length > 2
		num=token[1..token.length-2].length
		token[0,1] + num.to_s + token[token.length-1,1]
	else
		token
	end
end

# メイン部分
print $_.scan(/[A-Za-z0-9]+|\.|,/).map(&i18nize).join(" ")+"\n" while gets

これをi18nizer.rbとして,サンプル文字列(test.txt)

This is a sample.
Kore ha Test desu.

という文字列を入力として実行してみる.実行方法は次の通り.

 % ./i18nizer < test.txt

すると,

T2s is a s4e .
K2e ha t2t d2u .

という結果が出力される.