Module: Datacaster::SubstituteI18n

Defined in:
lib/datacaster/substitute_i18n.rb

Class Method Summary collapse

Class Method Details

.exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/datacaster/substitute_i18n.rb', line 7

def self.exists?(key)
  !fetch(key).nil?
end

.fetch(key) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/datacaster/substitute_i18n.rb', line 11

def self.fetch(key)
  keys = [locale] + key.split('.')

  @translations.each do |hash|
    result = hash.dig(*keys)
    return result unless result.nil?
  end
  nil
end

.load_pathObject



21
22
23
# File 'lib/datacaster/substitute_i18n.rb', line 21

def self.load_path
  @load_path
end

.load_path=(array) ⇒ Object



25
26
27
28
# File 'lib/datacaster/substitute_i18n.rb', line 25

def self.load_path=(array)
  @load_path = array
  @translations = array.map { |x| YAML.load_file(x) }
end

.localeObject



30
31
32
# File 'lib/datacaster/substitute_i18n.rb', line 30

def self.locale
  'en'
end

.locale=Object

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/datacaster/substitute_i18n.rb', line 34

def self.locale=(*)
  raise NotImplementedError.new("Setting locale is not supported, use ruby-i18n instead of datacaster's built-in")
end

.t(key, **args) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/datacaster/substitute_i18n.rb', line 38

def self.t(key, **args)
  string = fetch(key)
  return "Translation missing #{key}" unless string

  args.each do |from, to|
    string = string.gsub("%{#{from}}", to.to_s)
  end
  string
end