Class: Dugway::Drops::TranslationsDrop

Inherits:
BaseDrop
  • Object
show all
Defined in:
lib/dugway/liquid/drops/translations_drop.rb

Defined Under Namespace

Classes: MissingTranslationError

Instance Attribute Summary

Attributes inherited from BaseDrop

#params, #request, #source

Instance Method Summary collapse

Methods inherited from BaseDrop

#before_method, #cart, #context=, #error, #errors, #method_missing, #store, #theme

Constructor Details

#initialize(settings, definitions = {}) ⇒ TranslationsDrop

Returns a new instance of TranslationsDrop.

Parameters:

  • settings (Hash)

    Theme customization values (resolved settings).

  • definitions (Hash) (defaults to: {})

    Raw theme setting definitions (from settings.json).



10
11
12
13
14
15
16
17
18
# File 'lib/dugway/liquid/drops/translations_drop.rb', line 10

def initialize(settings, definitions = {})
  @settings = settings || {}
  @definitions = definitions || {} # Store definitions
  # Read settings using both symbol and string keys for flexibility.
  @translation_mode = (@settings[:translation_mode] || @settings['translation_mode'])&.to_s
  # Use explicit theme setting for locale if present, otherwise default to current I18n.locale
  @translation_locale = (@settings[:translation_locale] || @settings['translation_locale'])&.to_s.presence || I18n.locale.to_s
  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dugway::Drops::BaseDrop

Instance Method Details

#[](key) ⇒ String?

Liquid access: ‘translations }` Returns the translation string (which could be an empty string), or a “[MISSING TRANSLATION: …]” string if the key is not found or its value is nil.

Parameters:

  • key (String, Symbol)

    The translation key.

Returns:

  • (String, nil)

    The translation or missing translation indicator.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dugway/liquid/drops/translations_drop.rb', line 26

def [](key)
  key_str = key.to_s
  translation = find_translation(key_str)

  if translation.nil?
    # Key is missing, or explicitly nil in manual mode - treat both as missing.
    log_missing_translation(key_str)
    return missing_translation_message(key_str)
  end

  # Key found, return its value.
  translation
end