Class: TinyI18n
Overview
TinyI18n
Instance Attribute Summary collapse
-
#file ⇒ Object
File.
-
#locale ⇒ Object
Locale.
Instance Method Summary collapse
-
#initialize(file) ⇒ TinyI18n
constructor
Builder.
-
#translate(text, args = {}) ⇒ Object
Translate.
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
#file ⇒ Object
File
23 24 25 |
# File 'lib/tiny_i18n.rb', line 23 def file @file end |
#locale ⇒ Object
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 |