Module: I18nOnSteroids::TranslationHelper

Defined in:
lib/i18n_on_steroids/translation_helper.rb

Constant Summary collapse

INTERPOLATION_SPLIT_PATTERN =

Pre-compiled regex patterns for better performance

/(\$\{[^}]+\}|%\{[^}]+\}|\{\{[^}]+\}\})/.freeze
DOLLAR_BRACE_PATTERN =
/^\$\{([^}]+)\}$/.freeze
PERCENT_BRACE_PATTERN =
/^%\{([^}]+)\}$/.freeze
DOUBLE_BRACE_PATTERN =
/^\{\{([^}]+)\}\}$/.freeze
PARAM_INTERPOLATION_PATTERN =
/^%\{([^}]+)\}$/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_pipesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/i18n_on_steroids/translation_helper.rb', line 83

def available_pipes
  built_in = %w[
    number_with_delimiter pluralize truncate round upcase downcase capitalize html_safe format
    titleize humanize parameterize strip squish
    currency date_format time_format default
  ]
  custom = custom_pipes.keys

  {
    built_in: built_in,
    custom: custom
  }
end

.clear_pipe_cache!Object



64
65
66
# File 'lib/i18n_on_steroids/translation_helper.rb', line 64

def clear_pipe_cache!
  @pipe_cache = {}
end

.custom_pipesObject



39
40
41
# File 'lib/i18n_on_steroids/translation_helper.rb', line 39

def custom_pipes
  @custom_pipes ||= {}
end

.pipe_aliasesObject



43
44
45
46
47
48
49
# File 'lib/i18n_on_steroids/translation_helper.rb', line 43

def pipe_aliases
  @pipe_aliases ||= {
    "trim" => "truncate",
    "limit" => "truncate",
    "shorten" => "truncate"
  }
end

.pipe_cacheObject



60
61
62
# File 'lib/i18n_on_steroids/translation_helper.rb', line 60

def pipe_cache
  @pipe_cache ||= {}
end

.pipe_separatorObject



51
52
53
# File 'lib/i18n_on_steroids/translation_helper.rb', line 51

def pipe_separator
  @pipe_separator ||= "|"
end

.pipe_separator=(separator) ⇒ Object



55
56
57
58
# File 'lib/i18n_on_steroids/translation_helper.rb', line 55

def pipe_separator=(separator)
  @pipe_separator = separator
  clear_pipe_cache! # Clear cache when separator changes
end

.register_pipe(name, callable, namespace: nil) ⇒ Object



68
69
70
71
72
# File 'lib/i18n_on_steroids/translation_helper.rb', line 68

def register_pipe(name, callable, namespace: nil)
  pipe_key = namespace ? "#{namespace}.#{name}" : name.to_s
  custom_pipes[pipe_key] = callable
  clear_pipe_cache! # Clear cache when pipes are registered
end

.register_pipe_alias(alias_name, pipe_name) ⇒ Object



74
75
76
77
# File 'lib/i18n_on_steroids/translation_helper.rb', line 74

def register_pipe_alias(alias_name, pipe_name)
  pipe_aliases[alias_name.to_s] = pipe_name.to_s
  clear_pipe_cache! # Clear cache when aliases are registered
end

.resolve_pipe_name(name) ⇒ Object



79
80
81
# File 'lib/i18n_on_steroids/translation_helper.rb', line 79

def resolve_pipe_name(name)
  pipe_aliases[name.to_s] || name.to_s
end

Instance Method Details

#translate(key, **options) ⇒ Object Also known as: t



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/i18n_on_steroids/translation_helper.rb', line 98

def translate(key, **options)
  translation = if defined?(super)
                  super(key, **options)
                else
                  I18n.translate(key, **options)
                end

  if translation.is_a?(String) && (translation.include?("%{") || translation.include?("${") || translation.include?("{{"))
    debug_log "Processing translation for key: #{key}"
    process_mixed_translation(translation, options)
  else
    translation
  end
end