website stat

Rails/Globalize tip

I'm absolutely burdened in work so there's no time for blogging or whatever might refrain me from producing (few exceptions apply — am I becoming a workaholic? nevermind the rhetoric question, I've always been one). But this tip is quick and very interesting.

If you're using the i18n/l13n Globalize plugin for your Ruby on Rails projects and you need to escape something from being translated in the middle of a sentence, you do not need to split it and then concatenate it as follows:

RUBY:
  1. "to be translated ".t + user.name + " more to be translated".t

You can simply use Ruby's sugar candy to the string format function.

RUBY:
  1. "to be translated %s more to be translated".t % user.name

Just make sure your translators don't mess up the %s placeholder.


1 Response to “Rails/Globalize tip”

  1. Martin
    Published at July 18th, 2008 at 8:40 am

    Globalize can’t pass arguments ?

    In globalite you can do this ;
    :str.l_with_args({ :name => name })

    And in your translation’s file :
    str: Hello {name}