Last active
September 26, 2017 01:27
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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