Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding more values available to i18n #1633

Closed
ramontayag opened this Issue · 1 comment

2 participants

@ramontayag

I can't seem to find an easy way to create a message like this when a user logs in: "Hey, Ramon!" Searching for "devise set_flash_message" doesn't yield any results with an easy solution.

What do you think about adding more exposed fields to the Devise model?

devise.rb initializer

# `scope`, `default`, and `resource_name` are already set by Devise. To add more fields,
# add them below
config.extra_exposed_model_methods_for_i18n = [:first_name, :last_name, :email]

That means the set_flash_message will look more like this:

def set_flash_message(key, kind, options={})
  options[:scope] = "devise.#{controller_name}"
  options[:default] = Array(options[:default]).unshift(kind.to_sym)
  options[:resource_name] = resource_name

  # Add the extra methods to options here
  Devise.extra_exposed_model_methods_for_i18n.each do |method_name|
    options[method_name] = resource.send(method_name)
  end

  message = I18n.t("#{resource_name}.#{kind}", options)
  flash[key] = message if message.present?
end

With this, I should be able to use

devise:
  sessions:
    create: "%{first_name}, that's totally awesome!"

What do you think? Do you recommend another way?

@ramontayag

I just realized after coding something up that it's not that straightforward. For example, in the Sessions controller, resource is not User but Session. Which doesn't have a first_name. Let me think about this more.

@josevalim josevalim closed this in fb8e093
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.