Module: DummyLocale

Defined in:
lib/dummy_locale.rb,
lib/dummy_locale/locale.rb,
lib/dummy_locale/version.rb

Overview

DummyLocale builds a complete copy of the source locale at runtime, modifying each translated string in a way that allows you to easily distinguish internationalized strings from hard-coded strings when doing UI testing.

Defined Under Namespace

Classes: Locale

Constant Summary collapse

VERSION =
"0.1.0".freeze

Class Method Summary collapse

Class Method Details

.generate(source_locale: :en, destination_locale: :zz) ⇒ Object

Creates the dummy locale from the source locale and hooks ‘I18n.reload!`

When Rails or other code calls ‘I18n.reload!` to pick up new and modified entries, DummyLocale will regenerate the locale from the updated source. This helps us seamlessly pick up translation changes in development.



16
17
18
19
20
21
22
23
24
# File 'lib/dummy_locale.rb', line 16

def self.generate(source_locale: :en, destination_locale: :zz)
  dummy_locale = Locale.new(
    source_locale: source_locale,
    destination_locale: destination_locale
  )

  dummy_locale.generate
  I18n.backend.class.prepend(reloader_for(dummy_locale))
end

.reloader_for(dummy_locale) ⇒ Object

Generates a module that is aware of DummyLocale



27
28
29
30
31
32
33
34
# File 'lib/dummy_locale.rb', line 27

def self.reloader_for(dummy_locale)
  Module.new do
    define_method "reload!" do
      super()
      dummy_locale.generate
    end
  end
end