Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Last active September 26, 2017 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenharman/5f5d65b835f20ecae86f8044bb810a46 to your computer and use it in GitHub Desktop.
Save stevenharman/5f5d65b835f20ecae86f8044bb810a46 to your computer and use it in GitHub Desktop.
Loading I18n translation files outside of Rails (for example, as part of an isolated/non-Rails spec)
# frozen_string_literal: true
module I18nHelpers
def self.included(_mod)
locale_dir = Pathname.new(__dir__) + '../../config/locales/'
[:en].each do |locale|
locale_file = locale_dir + "#{locale}.yml"
I18n.backend.store_translations(locale, YAML.load_file(locale_file.open)[String(locale)])
end
end
end
# spec/widget_spec.rb
require 'app/widget'
RSpec.describe Widget do
include 'i18n_helpers'
# ✨ I18n :en translation is now loaded!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment