简体   繁体   English

Rails控制器与自定义转换键有关

[英]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 . 此功能将应用于所需控制器的#index动作,因此,我决定考虑控制器问题,将通用部分与我include AdvancedSearchFeature的每个控制器的每个#index挂钩。

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; 在链接到控制器的模型中,不是,因为提供Model.advanced_search_translation_keys并不自然,然后应将其分配给要在相应视图中使用的变量;
  • 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. 而不是与控制器关联的#index视图中,因为视图不应被翻译散列污染,然后应将其传递给高级搜索功能通用部分。

That's why, I've ended up having these translations in...helpers. 这就是为什么,我最终在... 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. 不需要任何通过,您的观点将更加明确地说明翻译的预期来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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