简体   繁体   中英

Rails controller concern with custom translation keys

I am building an advanced search feature that would be easily adopted for various entities. This feature would apply to the #index action of desired controllers, hence I've decided to go with a controller concern, hooking a generic partial to each #index of each controller I include AdvancedSearchFeature .

Everything works smooth until I found out that translations are quite an issue. Each advanced search uses different translation keys which should be supplied somewhere, but...I have no clue where it's best to do this.

I've thought:

  • in the concern, not, because it's meant to be generic;
  • in the controller where I include the concern, not, because controllers shouldn't deal with translations directly;
  • in the model linked to the controller, not, because it doesn't feel natural to provide a Model.advanced_search_translation_keys which then should be assigned to a variable to be used in the corresponding view;
  • in the #index view associated to the controller, not, because a view shouldn't be polluted with translation hashes which should then be passed to the advanced search feature generic partial.

That's why, I've ended up having these translations in...helpers. These helpers are invoked from the advanced search feature generic partial, and if one exists for the desired translation key, it is taken from there. But...I really don't like the idea of creating helpers specifically to return a translation.

Do you have a better idea on how to do this? Maybe you have stumbled upon this issue and solved it somehow else?

Best to nest your translation keys under a namespace, that references your module, and use those keys in a generic view. There won't be any passing needed and your view will be more explicit as to where translations are to be expected to come from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM