Thanks for your replies. Guess I wasn't sure on how to use with_options.
I made a small plugin with the methods I asked for. I tried to put in on git hub (first time working with) under http://github.com/DonDarone/localization_scopes/tree/master So check it out! All kinds of construcitve comments appreciated :-) Don On 30 Dez., 17:39, "Iain Hecker" <[email protected]> wrote: > Hi, > > I've experimented with automatic scopes for views, but I lacked a good > way of deciding which scope to use, especially in the case of > partials. Say you have a product partial in the products controller. > How should that be scoped? You can scope per action, which can be > desirable (the submit button gets another text if it is edit or new) > or general for one that partial, but that means that you'd have to > translate double. If it is dependent on the file it is in, you cannot > make a distinction any more. > > The first can be done with something like: > > def t_with_view_scope(key, options = {}) > t_without_view_scope(key, options) if options[:scope] > scope = __FILE__.sub(/.*\/app\//, "").sub(/\..*/,'').gsub(/\//,".") > # remove directory structure untill app and remove extension and > replace dir-separators with dots > t_without_view_scope(key, options.merge(:scope => scope)) > end > > The second: > > def t_with_view_scope(key, options = {}) > t_without_view_scope(key, options) if options[:scope] > scope = params[:controller] + '.' + params[:action] > t_without_view_scope(key, options.merge(:scope => scope)) > end > > alias_method_chain :t, :view_scope > > (just out of the top of my head, so there might be problems with this code) > > Cheers, > iain > > On Tue, Dec 30, 2008 at 13:49, Sven Fuchs <[email protected]> wrote: > > > Hi Don, > > > not sure, but here's a quick answer. > > > How about using with_options? > > > with_options :scope => :scope do |s| > > s.t(:key) > > end > > > calls t(:key, :scope => :scope) > > > On 30.12.2008, at 13:14, Don Darone wrote: > > > > Hi, > > > > we recently switched to Rails 2.2 and with that, also from the > > > simple_localization plugin to Rails core localization. > > > > Since the plugin had two nice features that I couldn't find yet in > > > Rails core, I was wondering whether some of you already addressed > > > them. > > > > In Rails localization you can add a scope to a single translation > > > using t(:my_key, :scope => 'general.scope') > > > > 1. Scope blocks > > > In simple_localization you could open a block and let every > > > translation in there have the same scope, which was quite helpful > > > sometimes: > > > l_scope 'general_scope' do > > > t(:my_key) > > > t(:next_key) > > > t(:other_key) > > > end > > > (I omitted the Erb stuff to just give the idea). > > > > 2. Scope contexts > > > In simple_localization you could use another method called lc to use > > > the scope of the context you are in, which was determined by the file > > > it was in. For example in a settings_controller, you could use lc > > > (:heading) which would correspond to t(:heading, :scope => 'settings') > > > for Rails core. > > > > In a views/settings/list.html.erb you could to lc(:heading) to > > > automatically get a scope 'settings.list', like calling t > > > (:heading, :scope => 'settings.list') > > > > I found these features very cool and was just wondering whether some > > > of you would miss them, too ;-) > > > > Thanks for your comments! > > > > Don > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "rails-i18n" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rails-i18n?hl=en -~----------~----~----~----~------~----~------~--~---
