如何最好地 DRY will_paginate 选项 [英] How best to DRY will_paginate options
问题描述
我有 8 个控制器使用 will_paginate 对其索引页进行分页.我想覆盖每个上一个"和下一个"的默认值,而不必指定相同的选项 8 次.有没有办法只覆盖默认值一次(可能通过子类化 will_paginate).
I have 8 controllers using will_paginate to paginate their index pages. I'd like to override the defaults for "Previous" and "Next" on each without having to specify the same options 8 times. Is there a way to override the defaults only once (perhaps by subclassing will_paginate).
推荐答案
will_paginate 使用 I18n 所以你可以直接使用它.鉴于您使用英语作为默认语言环境,application.rb
中应出现以下行:
will_paginate uses I18n so you can just use that.
Given you use English as the default locale the following line should be present in application.rb
:
config.i18n.default_locale = :en
然后您可以通过将以下内容添加到 config/locales/will_paginate.en.yml
来更改分页链接的文本:
you can then change the text of the pagination links by adding the following to config/locales/will_paginate.en.yml
:
en:
will_paginate:
previous_label: "← go back"
next_label: "go forward →"
或者,您可以将它们添加到您的默认翻译文件中:config/locales/en.yml
但我发现它很快变得难以处理.
Alternatively you can add them to your default translations file: config/locales/en.yml
but I have found that it quickly becomes to large to handle.
注意:如果您使用其他语言环境,例如 es
,为此您至少需要将 YAML 文件中的 en:
键替换为 es:
并且要简洁地命名您的文件,使用 config/locales/will_paginate.es.yml
或 config/locales/es.yml
.
Note: If you use another locale, for instance es
, for this to work you need to at least replace the en:
keys within the YAML files to es:
and to be concise about naming your files,
use either config/locales/will_paginate.es.yml
or config/locales/es.yml
.
这篇关于如何最好地 DRY will_paginate 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!