开发者

Using i18n to change text between update and create

I have a model that uses the same form for both create and update controller actions. Nothing special there. I have the submit button changing its text based on which开发者_JAVA百科 action using en.yml

en:
  helpers:
  submit:
    location:
      create: "Add to map"
      update: "Save changes"

How would I go about changing a body of text on this form, according to the appropriate action?

Eg, if it was create,

<h1>Create new location</h1>

and if update

<h1>Update location</h1>


You could simply add the text to your en.yml file:

en:
  create_new_location: 'Create new location'
  update_location: 'Update location'

And then use the following in your view (create/update)

<h1><%= I18n.t(params[:action] == 'create' ? 'create_new_location' : 'update_location') %></h1>

Or, if you were using new/edit:

<h1><%= I18n.t(params[:action] == 'new' ? 'create_new_location' : 'update_location') %></h1>

You could also make your translation based on the action and embed that in the translation text, by doing something like this:

I18n.t "location.action.#{params[:action]}"

I would recommend against this though, because it's harder to tell what text you are actually translating.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜