Translate the action_name controller in Rails 4

I want to translate the views of my applications and, since I use partial display of headers for each view, for example:

<%=t "#{controller.controller_name.capitalize} #{controller.action_name}" %>

... I mixed them up. How to translate controller.action_name into a custom translation file?

I tried to access the action names as follows:

  parkings: index: "Parkings index" new: "New %{model}" 

And many different variations, but each of them failed. could you help me?

This is a fragment of my controller:

  def new @parking = Parking.new end def create @parking = Parking.new(parking_params) if @parking.save redirect_to @parking, notice: t(:parking_created) else render action: 'new' end end 

Thanks.

+6
source share
2 answers

You must have translations in your locale file. Add an underscore or hyphen to separate words in a key

eg:

 # config/locales/en.yml en: parkings_index: Parkings index parkings_new: Parkings new page 

view file

 <%=t "#{controller_name}_#{action_name}" %> 
+2
source

First of all, when you say #{controller.controller_name} , it means that you have an object called controller , accessible from your view, which is incorrect. Even if you manage to access the controller and the name of its action, I do not think that it is worth the effort and time.

Instead, you can structure your translation file like this:

 views: model_name (parkings): "Parkings" action_1_name (index): "Parkings Index" action_2_name (new): "New Parking" ... 

and, in your opinion, say (for example) <%= link_to (t "views.model_name.action_name"), :action %>

+1
source

Source: https://habr.com/ru/post/978677/


All Articles