Class: AwesomeTranslations::GlobalTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_translations/global_translator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args:, blk:, call:, key:) ⇒ GlobalTranslator

Returns a new instance of GlobalTranslator.



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

def initialize(args:, blk:, call:, key:)
  @args = args
  @blk = blk
  @call = call
  @key = key
  @translation_args = args[:translation_args] || []
  @translation_opts = args[:translation_opts] || {}
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



25
26
27
# File 'lib/awesome_translations/global_translator.rb', line 25

def args
  @args
end

#blkObject (readonly)

Returns the value of attribute blk.



25
26
27
# File 'lib/awesome_translations/global_translator.rb', line 25

def blk
  @blk
end

#callObject (readonly)

Returns the value of attribute call.



25
26
27
# File 'lib/awesome_translations/global_translator.rb', line 25

def call
  @call
end

#keyObject (readonly)

Returns the value of attribute key.



25
26
27
# File 'lib/awesome_translations/global_translator.rb', line 25

def key
  @key
end

#translation_argsObject (readonly)

Returns the value of attribute translation_args.



25
26
27
# File 'lib/awesome_translations/global_translator.rb', line 25

def translation_args
  @translation_args
end

#translation_optsObject (readonly)

Returns the value of attribute translation_opts.



25
26
27
# File 'lib/awesome_translations/global_translator.rb', line 25

def translation_opts
  @translation_opts
end

Class Method Details

.call_information(caller_number) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/awesome_translations/global_translator.rb', line 2

def self.call_information(caller_number)
  # This is much faster than the other solution
  call = caller_locations(caller_number + 2, caller_number + 2).first

  {
    method: call.label,
    path: call.absolute_path,
    line_no: call.lineno
  }
end

.translate(key, args, &blk) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/awesome_translations/global_translator.rb', line 13

def self.translate(key, args, &blk)
  caller_number = args[:caller_number] || 0

  if args[:call]
    call = args.fetch(:call)
  else
    call = call_information(caller_number)
  end

  new(key: key, args: args, blk: blk, call: call).translation
end

Instance Method Details

#translationObject



36
37
38
39
40
41
# File 'lib/awesome_translations/global_translator.rb', line 36

def translation
  # Change key to full path.
  @key = translation_key if @key.is_a?(String) && @key.start_with?(".")

  I18n.t(@key, *translation_args, **translation_opts, &blk)
end