Shopify / Shopify/liquid

I18n best practice for filters

Open
#822 1 comment 2 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.