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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Alain, this helped me a lot !! On Mar 8, 12:01 pm, "Alain Ravet" <alain.ra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ? On Mar 8, 12:01 pm, "Alain Ravet" <alain.ra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---