Localizing dates and time with Rails’ I18n using procs

Ruby on Rails I18n infrastructure did a great job to internationalization in Rails applications. Most things work right out of the box.

Daily Fratze is fully internationalized and i wanted to use ordinal day numbers in the English version. Pity, there is no template for strftime that works that way.

As i already hat monkey patched a “t” method to all date and time related classes, i came up with the following solution:

Parallel to “en.yml” i now have “en.rb” in my locales folder which gets loaded in environment.rb via

config.i18n.load_path += Dir[File.join(RAILS_ROOT, 'app', 'locales', '*.{yml,rb}')]

The en.rb files defines some procs as translation values like so:

{
  :'en' => {
    :date => {
      :formats => {
        :dmy_with_long_month              => lambda { |date| "%B #{date.day.ordinalize}, %Y" },
      }
    },
    :time => {
      :formats => {
        :dmy_with_long_month                       => lambda { |date| "%B #{date.day.ordinalize}, %Y" },        
        :dmy_with_full_day_and_long_month_and_time => lambda { |date| "%A, %B #{date.day.ordinalize}, %Y at %H:%M" }
      }
    }
  }
}

Used with the standard I18n tools you’ll end up with the string representation of the proc object. Useless 😉

So time for monkeypatching like so:

module DateTimeSupport
  def t(format = nil)
    type = self.respond_to?(:sec) ? 'time' : 'date'
    formats = I18n.translate(:"#{type}.formats")
    format = formats[format.to_sym] if formats && formats[format.to_sym]
 
    I18n.localize(self, :format => format.respond_to?(:call) ? format.call(self) : format)
  end
end
 
class Time
  include DateTimeSupport
end
 
class Date
  include DateTimeSupport
end       
 
class DateTime
  include DateTimeSupport
end
 
class ActiveSupport::TimeWithZone
  include DateTimeSupport
end

Code resides in a file in config/initializers and gets loaded automatically. It adds a t method to all date and time related classes. The method tries to look up the translation of format just like I18n/Simple does.

If it is proc, it gets called and then passed to I18n, otherwise it the original parameter is used.

That way the t method can use “dmy_with_long_month”, :dmy_with_long_month and any other arbitrary format like “%B %Y” that is not defined in any language file.

| Comments (0) »

12-Feb-09


Grails, Hibernate, Current Session Context

Graeme was so kind to help me with this problem. My app worked well with Grails 1.0.4 but not in 1.1-beta3 any more.

My first wild guess leading to a

HibernateException: contains is not valid without active transaction

exception was my use of annotated Hibernate classes together with additional constraints in Grails, but Graeme figured out that it was the current session context i configured in hibernate.cfg.xml like so:

<property name="hibernate.current_session_context_class">thread</property>

I used this fragment for various JUnit test and in one case in a J2SE application where the same hibernate classes are needed. Together with a JTA Manager, this fragment is not needed and in case of Grails it has to go.

To run my tests i added the following statement right before opening my session:

final Properties nonJtaEnv = new Properties();
nonJtaEnv.put("hibernate.current_session_context_class", "thread");
 
sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").addProperties(nonJtaEnv).buildSessionFactory();

| Comments (4) »

12-Feb-09


Grails startup parameter

Just some snippets from the doku that I tend to forget:

Run grails on a different port:

grails -Dserver.port=9090 run-app

Run grails with a different environment:

grails prod run-app // production
grails -Dgrails.env=mycustomenv run-app  // mycustomenv

| Comments (0) »

09-Feb-09


Different day, same shit, today: Java 5 on Oracle Enterprise Linux 5

Worlds collide: Oracle and Sun JDK. Perfect start to ruin a not so bad Monday morning.

Background: Need to have a Tomcat Server deployed on a Oracle Enterprise Linux 5 system.

I was happy, when i saw a tomcat5 package in the repositories. Great, i thought. All i need. Well. Not.

Under Windows you’ll get between one and ten JVM versions installed with on Oracle product (slight exaggerated), within the OEL5 there was only one ancient 1.4.2 JDK. *sigh* Did i mention that the application that is supposed to run on that thing uses a buttload of Java 5 features?

“yum search java-“… No Java 5. WtF?

There is no Java 5.

Again, do it yourself:

This is a nice entry that describes howto build rpms for the “official” Sun Java 5 jdk.

I used the following steps to build my rpms:

  • Downloaded this rpm
  • Downloaded jdk-1_5_0_15-linux-i586.bin from the Sun JDK archive page
  • Put the later one into /usr/src/redhat/SOURCES/
  • Built the rpms with rpmbuild –rebuild java-1.5.0-sun-1.5.0.15-1jpp.nosrc.rpm. If rpmbuild is not installed, it’s hidden in the package rpm-build, not rpmbuild.
  • Installed missing libXp
  • Installed the rpms:
    rpm -Uvh /usr/src/redhat/RPMS/i586/java-1.5.0-sun-1.5.0.15-1jpp.i586.rpm
    rpm -Uvh /usr/src/redhat/RPMS/i586/java-1.5.0-sun-devel-1.5.0.15-1jpp.i586.rpm
    rpm -Uvh /usr/src/redhat/RPMS/i586/java-1.5.0-sun-src-1.5.0.15-1jpp.i586.rpm
    rpm -Uvh /usr/src/redhat/RPMS/i586/java-1.5.0-sun-demo-1.5.0.15-1jpp.i586.rpm
    rpm -Uvh /usr/src/redhat/RPMS/i586/java-1.5.0-sun-plugin-1.5.0.15-1jpp.i586.rpm
    rpm -Uvh /usr/src/redhat/RPMS/i586/java-1.5.0-sun-fonts-1.5.0.15-1jpp.i586.rpm
    rpm -Uvh /usr/src/redhat/RPMS/i586/java-1.5.0-sun-alsa-1.5.0.15-1jpp.i586.rpm
    rpm -Uvh /usr/src/redhat/RPMS/i586/java-1.5.0-sun-jdbc-1.5.0.15-1jpp.i586.rpm
  • Last step: Choose the right java version with alternatives –config java

After that, everything could be fine. Well, it wasn’t:

sun.misc.InvalidJarIndexException: Invalid index

Jehova! Finally not a NPE but something new, at least to me. Sometimes i wonder why i always run into bugs like these.

Some script changes and repackages all jar files in some weird ways so that a standard JDK has funny problems.

My solution to it: Reindex everything in /usr/share/java after you’ve chosen your newly installed java with the following command:

find /usr/share/java/ -iname "jakarta*commons*.jar" -exec jar -i {} \;

I opted to reindex only the jakarta commons files, that got Tomcat up and running with Java 5.

One last note: The /usr/bin/dtomcat5 is broken imho, at least when run from /etc/init.d/tomcat5. In ignores /etc/tomcat5/tomcat5.conf and therefore cannot stop Tomcat.

My solution: Replace

if [ -z "$CATALINA_HOME" ]; then
    TOMCAT_CFG="/etc/tomcat5/tomcat5.conf"
fi

(in line 55 on my setup) with

if [ -z "$CATALINA_HOME" ]; then
    TOMCAT_CFG="/etc/tomcat5/tomcat5.conf"
    [ -r "$TOMCAT_CFG" ] && . "${TOMCAT_CFG}"
fi

and remove

if [ -z "$CATALINA_HOME" ]; then
    [ -r "$TOMCAT_CFG" ] && . "${TOMCAT_CFG}"
fi

(in line 105 on my setup).

I wonder why simple things like these always are a pain in the ass. Stupid nuisances that keeps people from getting their work done. *sigh* Not a good start for the week.

| Comments (5) »

19-Jan-09