Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I perform interpolation in I18n locales?

Is there a way to do something like this:

en:
  welcome:
    hello there, #{current_user.first_name}!  It's nice to see you again.

That obviously won't work, and apparently "#{" is invalid characters in yaml, because that string shows up as just "hello there, " when I pull it out.

The best I could do was something like:

en:
  welcome:
    hello there, (name)!  It's nice to see you again.

....

t(:welcome).gsub("(name)", current_user.first_name)

But I am not crazy about that... There must be a better way to do this sort of thing.

like image 849
patrick Avatar asked Jun 30 '11 14:06

patrick


1 Answers

Replace your en.yml like this

en:
  welcome:
    "hello there, %{name}!  It's nice to see you again."

and your view like this

<%=t(:welcome, :name=> current_user.first_name) %>

Basically it is passed as a named argument. You can find more at Rails Guides 18n Interpolation

like image 57
felix Avatar answered Sep 23 '22 08:09

felix