<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>blog.knapo.net - Programming</title>
  <id>tag:blog.knapo.net,2010:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://blog.knapo.net/feed" rel="self" type="application/atom+xml"/>
  <link href="http://blog.knapo.net/" rel="alternate" type="text/html"/>
  <updated>2010-03-06T10:35:22Z</updated>
  <entry xml:base="http://blog.knapo.net/">
    <author>
      <name>knapo</name>
    </author>
    <id>tag:blog.knapo.net,2010-01-21:14</id>
    <published>2010-01-21T19:37:00Z</published>
    <updated>2010-03-06T10:35:22Z</updated>
    <link href="http://blog.knapo.net/2010/1/21/string-tr-vs-string-gsub" rel="alternate" type="text/html"/>
    <title>String#tr vs String#gsub</title>
<content type="html">
            &lt;p&gt;I&#8217;ve always used &lt;em&gt;gsub&lt;/em&gt; method for all simle string replacements. But recently I found &lt;em&gt;tr&lt;/em&gt; method. It&#8217;s not so powerful, as it doesn&#8217;t support regexps, but in most common cases can do the same work as gsub.&lt;/p&gt;


To check difference in execution time, I did simple benchmark:
&lt;pre&gt;
Benchmark.bm(10) do |b|
  b.report(&quot;tr&quot;)   { 1.upto(10000){ &quot;heloo knapo&quot;.tr('o','i') } }
  b.report(&quot;gsub&quot;) { 1.upto(10000){ &quot;heloo knapo&quot;.gsub('o','i') } }
end
&lt;/pre&gt;

	&lt;p&gt;And the result     surprised me a little bit. I supposed that &lt;em&gt;gsub&lt;/em&gt; is slower, but did not that more than 2 times:&lt;/p&gt;


&lt;pre&gt;
                user     system      total        real
tr          0.030000   0.000000   0.030000 (  0.047241)
gsub        0.070000   0.010000   0.080000 (  0.109662)
&lt;/pre&gt;

	&lt;p&gt;So now, I have clear choice with simple string replacements &#8211; especially that &lt;em&gt;tr&lt;/em&gt; is shorter ;)&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.knapo.net/">
    <author>
      <name>knapo</name>
    </author>
    <id>tag:blog.knapo.net,2010-01-12:11</id>
    <published>2010-01-12T22:23:00Z</published>
    <updated>2010-01-21T07:38:33Z</updated>
    <category term="i18n"/>
    <category term="rails"/>
    <link href="http://blog.knapo.net/2010/1/12/my-improvements-on-latest-i18n-gem" rel="alternate" type="text/html"/>
    <title>my improvements on latest i18n gem</title>
<content type="html">
            &lt;p&gt;In recent weeks I spent a lot of time on i18n stuff in project I currently working on, so I was really happy, after I read José Valim&#8217;s &lt;a href=&quot;http://blog.plataformatec.com.br/2009/12/run-i18n-run/article&quot;&gt;article&lt;/a&gt; about new i18n gem, which has been very improved and introduces new features (Thanks José for great article and benchmarks).&lt;/p&gt;


	&lt;p&gt;Unfortunately, after few days of using it I noticed few issues (which I reported on &lt;a href=&quot;http://github.com/svenfuchs/i18n/issues&quot;&gt;http://github.com/svenfuchs/i18n/issues&lt;/a&gt;.  They were related to &lt;em&gt;I18n::Backend::Fallbacks&lt;/em&gt;, which handled only translation method, and does not localize, pluralize and &lt;em&gt;:default&lt;/em&gt; option, also &lt;em&gt;I18n:Backend::Fast&lt;/em&gt; threw an exception when there were no translations for locale or one of locale fallbacks. Because I need all these 3 backends, they blocked using new i18n :(&lt;/p&gt;


To not wait until they will be fixed, I forked i18n &lt;a href=&quot;http://github.com/knapo/i18n&quot;&gt;http://github.com/knapo/i18n&lt;/a&gt; and fixed them by myself. So, I installed it as a plugin (using braid to easily upgrade its in future):
&lt;pre&gt;
$ braid add -p git@github.com:knapo/i18n.git
&lt;/pre&gt;

And there&#8217;s a trick (which i18n&#8217;s &lt;span class=&quot;caps&quot;&gt;README&lt;/span&gt; hopefully says about) &#8211; to use i18n as a plugin I had to add to my &lt;em&gt;initializers/i18n.rb&lt;/em&gt;  &lt;em&gt;reload_i18n!&lt;/em&gt; method which replaces bundled i18n gem with plugin: 
&lt;pre&gt;
def reload_i18n!
  $:.grep(/i18n/).each { |path| $:.delete(path) }
  I18n::Backend.send :remove_const, &quot;Simple&quot; 
  $: &amp;lt;&amp;lt; Rails.root.join('vendor/plugins/i18n/lib').to_s
end

reload_i18n!
&lt;/pre&gt;

and created my own backend (I preffer that than including all modules to &lt;em&gt;&lt;span class=&quot;caps&quot;&gt;I18&lt;/span&gt;::Backend::Simple&lt;/em&gt;) 
&lt;pre&gt;
module I18n
  module Backend
    class Knapo &amp;lt; Simple
      include I18n::Backend::Pluralization
      include I18n::Backend::Fallbacks
      include I18n::Backend::Fast
      include I18n::Backend::InterpolationCompiler
    end
  end
I18n.backend = I18n::Backend::Knapo.new
&lt;/pre&gt;

The only disadvantage of having i18n as a plugin is resetting &lt;em&gt;I18n.load_path&lt;/em&gt;, so all default translations loaded by Rails are being removed, I mean:
&lt;pre&gt;
actionpack-2.3.5/lib/action_view/locale/en.yml
activesupport-2.3.5/lib/active_support/locale/en.yml
activerecord-2.3.5/lib/active_record/locale/en.yml
&lt;/pre&gt;

	&lt;p&gt;But actually it might be a feature for some devs (e.g. me:) ), I copied them into my locales dir to active_support, active_record and action_view subdirs, and have all translations file in one place, and it&#8217;s better to have default (en) Rails ones there to easily compare other locale files with them, and/or edit them.&lt;/p&gt;


I also added extra extensions for &lt;em&gt;I18n&lt;/em&gt;:
&lt;ul&gt;
  &lt;li&gt;
  &lt;em&gt;I18n::LoadPath&lt;/em&gt; (based and inspired by Globalize::LoadPath::I18n) which makes I18n.load_path more handy:
  &lt;pre&gt;
    I18n.load_path = I18n::LoadPath.new(I18n.load_path)
    I18n.load_path &amp;lt;&amp;lt; Rails.root.join('locales/pl.yml') # loads single translation file
    I18n.load_path &amp;lt;&amp;lt; Rails.root.join('locales') # loads all translation data files in specific directory
    I18n.load_path &amp;lt;&amp;lt; Rails.root.join('locales/*.{yml,rb}') # loads all translation data files  by given pattern
  &lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;em&gt;I18n.wih_locale&lt;/em&gt; which executes block in given locale set
   &lt;pre&gt;
     I18n.with_locale(&quot;pl-PL&quot;) do
        # This code are being executed with pl-PL locale
     end
  &lt;/pre&gt;
  &lt;/li&gt;
   &lt;li&gt;
      Making &lt;em&gt;MissingTranslation&lt;/em&gt; message thrown by &lt;em&gt;I18n.localize&lt;/em&gt; method complete, as previously when translation was missing it returned partial message e.g. for &lt;em&gt;I18n.l Time.now, :format =&amp;gt; :missing&lt;/em&gt; I was getting: &lt;em&gt;translation missing: en, long_ordinal&lt;/em&gt;, what was misleading and now I&#8217;ve got: &lt;em&gt;translation missing: en, time, formats, long_ordinal&lt;/em&gt;
   &lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;p&gt;The only thing I really don&#8217;t like in new i18n is forcing I18n.locale to Symbol &#8211; why? it should absolutely be a String&lt;/p&gt;

&lt;h4&gt;Anyway, thanks to Rails i18n team for great improvements on i18n, and looking forward for fixed issues in next gem version (and Rails3, of course).&lt;/h4&gt;
          </content>  </entry>
</feed>
