i have such a filter in my application_controller

def set_locale
    session[:locale] = cookies[:locale] = params[:locale] if
params[:locale] and LOCALES.include?(params[:locale].to_sym)
    locale = (session[:locale] || cookies[:locale] ||
(request.env["HTTP_ACCEPT_LANGUAGE"] || '').scan(/[^,;]+/).find{|l|
LOCALES.include?(l)} || 'en')
    I18n.locale = locale.to_sym
  end

but i cant make the validation like:

validates_email_format_of :email, :message =>
I18n.t(:"validations.no_valid_email", :locale => session[:locale])

any ideas?

On Sep 30, 1:03 pm, "Iain Hecker" <[EMAIL PROTECTED]> wrote:
> I18n.locale reverts back to I18n.default_locale on every request.
> You'll need a before_filter that saves and sets the locale in, say, a
> session.
>
> e.g. in application controller:
>
> before_filter :set_locale
> private
> def set_locale
>   I18n.locale = params[:locale] or session[:locale] or I18n.default_locale
>   session[:locale] = I18n.locale
> end
>
> On Tue, Sep 30, 2008 at 12:35, fwalter <[EMAIL PROTECTED]> wrote:
>
> > i changed the function to
>
> > def validates_email_format_of(*attr_names)
> >        configuration = { :message =>
> > I18n.t(:"validations.no_valid_email"),
> >                          :on => :save,
> >                          :with => RegexCollection::EMAIL_REGEX }
>
> >        configuration.update(attr_names.pop) if attr_names.last.is_a?
> > (Hash)
>
> >        validates_each(attr_names, configuration) do |record,
> > attr_name, value|
> >          record.errors.add(attr_name, configuration[:message]) unless
> > value.to_s =~ configuration[:with]
> >        end
> >      end
>
> > but it isnt translated.
>
> > if i insert a debug message:
> > p I18n.locale
>
> > when i start the server, it displays: :"en-US"
>
> > now i change my locale to "de", all my translations are now displayed
> > in german.
>
> > now i submit the form,
> > again the debugmessage displayes :"en-US"
>
> > so the plugin doesn't recognise the change of locale.
>
> > thanx
>
> > florian
>
> > On Sep 30, 12:08 pm, iain hecker <[EMAIL PROTECTED]> wrote:
> >> Then the plugin needs to be adjusted. I've never used it, but it
> >> should be easy.
>
> >> All it really needs to do is call errors.add(attr, :message_key,
> >> options).
>
> >> So just remove the hardcoded english messages and make them into
> >> symbols. errors.add takes care of the rest!
>
> >> --
> >> Iain Heckerhttp://iain.nl/
>
> >> On Sep 30, 11:45 am, fwalter <[EMAIL PROTECTED]> wrote:
>
> >> > thanks
>
> >> > but I am using the model_validators plugin:
>
> >> > there are some validations: like
>
> >> > validates_email_format_of :email
>
> >> > the default message is hardcoded englisch, so i would like to make it
> >> > I18n conform.
>
> >> > so I first tried a quick and dirty stuff and changed the default
> >> > message inside the model_validators plugin function
>
> >> > but this plugin is initialized at server startup. with the base_locale
> >> > en-US.
>
> >> > and now at runtime, i choose a different locale, but get the
> >> > translations in base_locale en-US
>
> >> > so i tried to override the default message with my own error_message.
> >> > I18n.t(:"validations.no_valid_email")
>
> >> > but if i only write
>
> >> > validates_email_format_of :email, :message =>
> >> > I18n.t(:"validations.no_valid_email")
> >> > <<<<<<<<<<<<<
> >> > i only get the en-US translation. not the translation depending on the
> >> > current_locale.
>
> >> > thats why i did the ugly stuff:
>
> >> > validates_email_format_of :email, :message =>
> >> > I18n.t(:"validations.no_valid_email", :locale => "en-US") , :if =>
> >> > Proc.new { |c| I18n.locale.to_s == "en" }
> >> > validates_email_format_of :email, :message =>
> >> > I18n.t(:"validations.no_valid_email", :locale => "de-DE") , :if =>
> >> > Proc.new { |c| I18n.locale.to_s == "de" }
> >> > <<<<<<<<<<<<
>
> >> > thanks,
> >> > florian
>
> >> > On Sep 30, 11:32 am, iain hecker <[EMAIL PROTECTED]> wrote:
>
> >> > > There is a nicer way! Translating ActiveRecord validation messages is
> >> > > built into Rails, so you don't have to make a I18n call at all.
>
> >> > > Have a look at my post in how to do 
> >> > > this:http://iain.nl/2008/09/translating-activerecord/
> >> > > Chapter 5 and onwards are about validations.
>
> >> > > Good luck,
>
> >> > > Iain
>
> >> > > On Sep 30, 11:21 am, fwalter <[EMAIL PROTECTED]> wrote:
>
> >> > > > hi
>
> >> > > > I am using custom error messages. but I only get the translation of
> >> > > > the base_locale (en)
>
> >> > > > validates_presence_of :email, :message =>
> >> > > > I18n.t(:"validations.no_valid_email")
>
> >> > > > The way to get other translations looks like bullshit:
> >> > > > I tried to solve it that way:
>
> >> > > > validates_presence_of :email, :message =>
> >> > > > I18n.t(:"validations.no_valid_email", :locale => "en-US") , :if =>
> >> > > > Proc.new { |c| I18n.locale.to_s == "en" }
> >> > > > validates_presence_of :email, :message =>
> >> > > > I18n.t(:"validations.no_valid_email", :locale => "de-DE") , :if =>
> >> > > > Proc.new { |c| I18n.locale.to_s == "de" }
>
> >> > > > is there a nicer way to get the translations in correct language?
>
> >> > > > thanks for help
>
> >> > > > florian
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to