1

我正在使用翻译文件来管理我的 Rails 应用程序的各种语言:

fr:
  books:
    index:
      title: "Livres"

Rails存储title出现在多个视图中的 a 的方式是什么?

这根本不是 DRY,因此不是我想要的:

fr:
  books:
    index:
      title: "Livres"
    new:
      title: "Livres"
    edit:
      title: "Livres"

现在我正在做这样的事情:

fr:
  books:
    title: "Livres"

但这是约定吗?

我没有在其中指定任何视图的名称,所以我想知道这是否是一种好习惯。

感谢您的任何意见。

4

2 回答 2

1

Say, You need to store translations for books model. You should be storing them under:

en.active_record.models.book

And, all the book attributes should go under:

en.active_record.attributes.book.title

Keeping translations for each view might not be a very good idea as:

1) As you have pointed out, It is not DRY.

2) Translations take much time to load up in memory. So, more of them would increase the server start time.

于 2013-10-05T17:17:40.153 回答
0

我通常喜欢为视图添加一个共享部分,这样您仍然可以使用速记。

fr:
 books:
   shared: &shared
    title: Livres
    thing_with_variable: "%{count} things"
   index:
    <<: *shared
    thing: thing
   new:
    <<: *shared
    other_thing: other thing
   edit:
    <<: *shared

然后在 index、new 等视图中,您都可以使用速记。

<%= translate('.title') %>
<%= translate('.thing_with_variable', count: 2) %>
于 2015-12-01T17:49:58.033 回答