Top Level Namespace

Defined Under Namespace

Modules: TolgeeFilter, TolgeeLiquid Classes: Translate, ZeroWidthCharacterEncoder

Instance Method Summary collapse

Instance Method Details

#with_tolgee(method_name) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tolgee_liquid.rb', line 30

def with_tolgee(method_name)
  original_method = instance_method(method_name)
  define_method(method_name) do |*args, &fn|
    mode = @context.registers[:mode] || 'production'
    locale = @context.registers[:locale] || I18n.default_locale

    if mode == 'development'
      message = { k: args[0] }.to_json
      hidden_message = ZeroWidthCharacterEncoder.new.execute(message)

      t = Translate.new
      dict = t.remote_dict(locale)
      value = t.fetch_translation(dict, args[0])
      return args[0] if value.nil?

      translation = MessageFormat.new(value, locale.to_s).format(args[1]&.transform_keys(&:to_sym))
      "#{translation}#{hidden_message}"
    else
      original_method.bind(self).call(*args, &fn)
    end
  end
end