Forum: Ruby on Rails Noob: HOWTO Externalize Strings ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0ce57b30c6f84b74443ddf4f682e6d6e?d=identicon&s=25 Thogra (Guest)
on 2007-03-08 11:25
(Received via mailing list)
Hi all,

I'm totally new to Rails and was wondering how to externalize message
strings in a file so I can use them throughout my application. Is
there something already provided with Rails ?

Thanks

Thogra
Ba3a00606eb530dcab2c4a6a59bf366d?d=identicon&s=25 Alain Ravet (Guest)
on 2007-03-08 12:02
(Received via mailing list)
Thogra

  > I'm totally new to Rails and was wondering how to externalize
message
  > strings in a file

In environment.rb:
# load the strings file :
APP_TEXT   = YAML::load(File.open("#{RAILS_ROOT}/config/messages.yml"))
rescue {}

In config/message.yml:
db_save:
    success: Success
    failure: Failure


In views/controller: example:
    ...
    flash[:notice] = tx_('db_save/success')
    ...


In application.rb

  def dev?
      @is_dev||=(ENV['RAILS_ENV']=='development')
  end
  def tx_(key)
      a,b = key.split('/')
      APP_TEXT[a][b]+(dev? ? '°' : '')
  end
  helper_method :tx_, :dev?


Note: this version adds a little ° after externalized string when in
dev mode => you can see what's hardcoded, and what's externalized.

Alain
0ce57b30c6f84b74443ddf4f682e6d6e?d=identicon&s=25 Thogra (Guest)
on 2007-03-08 13:26
(Received via mailing list)
Thanks Alain, this helped me a lot !!
0ce57b30c6f84b74443ddf4f682e6d6e?d=identicon&s=25 Thogra (Guest)
on 2007-03-08 16:42
(Received via mailing list)
I was excpecting the 'tx_(key)' method to be available in functional
test as well if defined in application.rb. But it isn't.

Do I have to duplicate the method(s) ? If, what would be the right
place for it ? test_helper.rb ?
This topic is locked and can not be replied to.