如何在 Rails 5.2 中呈现 class/controller 部分的特定翻译?

How to render class/controller specific translations on a partial in Rails 5.2?

我的应用程序管理几个 objects,但我尝试使用部分组件和它们的某种通用结构来 DRY:所有 objects 都有 ID、代码、名称、描述,[=至少 34=] 和 updated_at 列。

从这里开始,所有索引看起来都一样,并且依赖于相同的部分:

app/views/business_areas/index.html.erb

<% provide( :title, t('ManagingBusinessAreas')) %>
<% provide( :page_heading, t('BusinessAreas')) %>
<% provide :navigation do %>
  <%= render partial: "shared/index_view_navigation", locals: {this_object: BusinessArea} %>
<% end %>
<%= render partial: "shared/object_index", locals: {this_index: @business_areas} %>

.

app/views/shared/_object_index.html.erb

<table class="table table-hover table-clickable" id="indexTable">
</table>

<script>
  $(document).ready( function () {
    $('#indexTable').DataTable({
      ajax:{
        url: '<%= request.fullpath %>',
        dataSrc: ''
      },
      pageLength: 25,
      createdRow: function( row, data, dataIndex ) {
        $(row).attr("data-href", "<%= request.fullpath %>/" + data.id)
      },
      columns: [
        { title: '<%= t('.Code') %>', data: 'code' },
        { title: '<%= t('.Name') %>', data: 'translated_name' },
        { title: '<%= t('.Description') %>', data: 'translated_description' },
        { title: '<%= t('.UpdatedBy') %>', data: 'updated_by' },
        { title: '<%= t('.UpdatedAt') %>', render: function( data, type, row ) {
          return (row['updated_at']).substring(0,10)}
        }
      ]
    });
  } );
</script>

我尝试实现翻译的惰性查找,并为 object 的每个 class 组织了翻译文件。尽管 object 的第一列相同,但业务人员对它们的调用方式并不相同。即“代码”列可以称为代码、简称、技术名称。从数据的角度来看,它们都是相同的,并且受益于相同的验证和功能。

所以我在 config/local/BusinessAre 文件夹等中整理了翻译文件。我将 I18n load_path 配置为 config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]

# Code for language as declared in i18n configuration
fr:
  # Considered object from a global view
  BusinessAreas:                              "Business areas"
  BusinessArea:                               "Business area"
  ManagingBusinessAreas:                      "Gérer les Business areas"
  NewBusinessArea:                            "Nouvelle Business area"
  BusinessAreaRejected:                       "La Business area a été rejetée"
  # Considered object from controller's methods. In the controller, the relative path for the term is marked by the point (.)
  # Use generic terms, excluding objec name (ie Manage instead of ManageBusinessArea )
  business_areas: 
    index:
      # Columns titles
      Code:                                   "Code"
      Name:                                   "Nom"
      Description:                            "Description"
      Owner:                                  "Propriétaire"
      UpdatedBy:                              "Mis à jour par"
      UpdatedAt:                              "Mis à jour le"

但在显示索引页时,headers栏实际上显示的是自定义标题的人性化形式。将鼠标悬停在标题上,提示说:

translation missing: fr.shared.object:index.Name

这意味着 Rails 在共享文件夹中查找翻译,所有翻译 - 每次使用部分 - 都是相同的!

有没有办法根据调用控制器而不是部分名称进行翻译?

如果您希望您的 i18n 键依赖于控制器和操作而不依赖于视图名称,您可以在查找键时使用 controller_nameaction_name 方法。

例如:

<li>Code: <%= t("#{controller_name}.#{action_name}.Code") %></li>