Class: TinyI18n

Inherits:
Object
  • Object
show all
Includes:
TinyI18nVersion
Defined in:
lib/tiny_i18n.rb

Overview

TinyI18n

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ TinyI18n

Builder



30
31
32
33
34
35
# File 'lib/tiny_i18n.rb', line 30

def initialize file
  # Default locale
  self.locale ||= :en
  # Load translations
  self.file = YAML.load_file(file)
end

Instance Attribute Details

#fileObject

File



23
24
25
# File 'lib/tiny_i18n.rb', line 23

def file
  @file
end

#localeObject

Locale



20
21
22
# File 'lib/tiny_i18n.rb', line 20

def locale
  @locale
end

Instance Method Details

#translate(text, args = {}) ⇒ Object

Translate



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tiny_i18n.rb', line 42

def translate text, args = {}
  # Translation
  translation = self.file[self.locale]
  # Converting key.subkey into key[subkey] and returning the translation
  translation = text.split(".").map do |key|
    translation = translation[key]
  end.last
  # Variables
  args.each_pair do |key, value|
    # Replace
    translation = translation.to_s.gsub("%{#{key}}", value.to_s)
  end
  # Return
  translation
end