My application manages several objects, but I try to DRY by using partials, and somehow common structure for them: all objects have id, code, name, description, updated_by and updated_at columns, at minimum.
Starting with this, all indexes look the same, and rely on the same partial:
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>
I tried to implement lazy lookup for translations, and organised translation files for each class of object. Eventhough the first column of the objects are the same, they are not called the same way by the business people. i.e the "code" column may be called a Code, Short name, Technical name. From the data point of view, they are all identical, and benefit from the same validations and features.
So I organised translation files in the config/local/BusinessAre folder etc. I configured the I18n load_path with 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"
But when displaying the index page, column headers actually show the humanized form of defined title. Hovering the title, a hint says:
translation missing: fr.shared.object:index.Name
This means Rails looks up for tranlsations in the shared folder, and all translations - for each use of the partial - would be the same!
Is there a way to base the translations on the calling controller, instead of the partial name ?