A Ruby gem that integrates the Tolgee Platform with Shopify's Liquid template language.
If you are developing a multilingual web application using the Liquid template language, check out the Tolgee Platform (translation as a service). It enhances your translation workflow, making it more efficient and accurate.
This gem focuses on the "In-Context" translation integration. See the example project at https://github.com/cccccroge/tolgee-rails-liquid.
Make sure you have liquid installed.
Add this line to your application's Gemfile:
gem 'tolgee_liquid'
And then execute:
$ bundle
Add the following block to your application initialization. If you are using Rails, place it in config/initializers/tolgee.rb:
TolgeeLiquid.configure do |config|
config.api_url = 'https://example.tolgee.io'
config.api_key = <TOLGEE_API_KEY>
config.project_id = <TOLGEE_PROJECT_ID>
end
This allows the gem to fetch data from the corresponding Tolgee project in development mode.
There are two ways to setup.
If you already have your own translation method t
defined.
module MyFilterContainsTranslationMethod
def t(name, vars = {})
# Own implementation
end
end
Liquid::Template.register_filter(MyFilterContainsTranslationMethod)
Add the prefix with_tolgee
on translation method:
module MyFilterContainsTranslationMethod
with_tolgee def t(name, vars = {})
# Own implementation
end
end
Note that the arguments of the original t method must match the format above (the first argument being the translation key, and an optional hash argument providing variables for string interpolation). Otherwise, it won't work correctly.
You can use the t method provided by the gem, which utilizes ICU MessageFormat patterns under the hood.
Liquid::Template.register_filter(TolgeeFilter)
Example Liquid snippet:
<div>{{ 'key.nested_key' | t: fruit: 'Pitaya' }}</div>
Example application's controller code for Rails project:
class PagesController < ApplicationController
def index
# get liquid template file...
template = Liquid::Template.parse(liquid)
# provide translations and meta data
tolgee_registers = TolgeeLiquid.registers({
locale: 'en',
mode: 'development',
static_data: {
en: YAML.load_file(Rails.root.join('config', 'locales', 'en.yml')),
},
})
html = template.render({}, registers: tolgee_registers)
# put html to view...
end
end
Example translation yaml file:
key:
nested_key: "I like {fruit}!"
- Run
install dependencies
to install dependencies. - Run
rake spec
to run the tests. - Run
bin/console
for an interactive prompt that will allow you to experiment. - Run
rubocop
to find code smells. - Run
bin/deploy
to release a new version
Bug reports and pull requests are welcome.
The gem is available as open source under the terms of the MIT License.