I18n best practice for filters
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 11.9k
- Forks
- 1.5k
- Avg merge
- 17h 55m
- Merged PRs (30d)
- 3
Description
After reading #111 and #158 creating a custom date filter seems the way to go to translate day and month names.
My current solution contains passing the locale through the registers option on Template#render:
liquid = Liquid::Template.parse(source)
liquid.render(assigns, filters: [Extensions::Liquid::DateFilter], registers: { locale: :de })
and then I'm able to access it via the @context variable in the filter:
module Extensions
module Liquid
module DateFilter
def date(input, format)
return input if format.to_s.empty?
return input unless date = to_date(input)
I18n.l(date, format: format.to_s, locale: @context.registers[:locale])
end
end
end
end
My question is: is this the "correct" way to achieve the desired result? Or is there a "better" intended way to do this?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read issues #111 and #158 first to understand the existing guidance on translating day and month names. Then review the custom date filter and the Template#render registers option shown here; the work is done when the intended i18n approach is established and documented clearly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- internationalization
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100