Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
liquid + i18n from rails
# lib/liquid_i18n_rails.rb
module LiquidI18nRails
def t(string)
# config/initializers/liquid.rb
require 'liquid_i18n_rails'
Liquid::Template.register_filter LiquidI18nRails
# and then:
{{ 'string' | t }}
# where 'string' is name of sym for I18n.t
# it will do .to_sym automatically

This comment has been minimized.

Copy link

@phlegx phlegx commented Aug 12, 2014

Hi! Great gist! How can I use it to get months of dates translated (December, January,..)?


This comment has been minimized.

Copy link

@passalini passalini commented Dec 4, 2015

@phlegx I18n.l


This comment has been minimized.

Copy link

@NealJMD NealJMD commented Jan 22, 2016

Thanks! this was super helpful in connecting the filter to the I18n API.

I found that our translations needed to be able to do interpolation, so I expanded this class to allow it to be used like {{ 'fundraiser.donate' | val: 'amount', my_amount | t }}. I also edited it to raise errors in development when a liquid template is rendered with missing translations. I've added a gist of it with a spec in another gist.


This comment has been minimized.

Copy link

@saadbinakhlaq saadbinakhlaq commented Jun 21, 2017

I couldn't find anything on liquid for translations with arguments so I did something like this

module LiquidI18nRails
  def t(string, arguments = nil)
    I18n.t(string.to_sym, arguments.with_indifferent_access)

now can translate it like this

{{ '' | t: data: 'yourname' }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.