Class: Vocab::Translator::Rails

Inherits:
Base
  • Object
show all
Defined in:
lib/vocab/translator/rails.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

ignore_key?

Constructor Details

#initialize(locale = :en) ⇒ Rails

Returns a new instance of Rails.



12
13
14
15
# File 'lib/vocab/translator/rails.rb', line 12

def initialize( locale = :en )
  @backend = I18n::Backend::Simple.new
  @locale = locale
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



10
11
12
# File 'lib/vocab/translator/rails.rb', line 10

def locale
  @locale
end

Class Method Details

.en_equivalent_path(path) ⇒ Object



54
55
56
# File 'lib/vocab/translator/rails.rb', line 54

def self.en_equivalent_path( path )
  return "#{File.dirname( path )}/en.yml"
end

Instance Method Details

#fetch(key) ⇒ Object



50
51
52
# File 'lib/vocab/translator/rails.rb', line 50

def fetch( key )
  return flattened_translations[ key.to_sym ]
end

#flattened_translations(options = {}) ⇒ Object



39
40
41
# File 'lib/vocab/translator/rails.rb', line 39

def flattened_translations( options = {} )
  return @backend.flatten_translations( @locale, translations( options ), true, false )
end

#load_dir(dir) ⇒ Object



17
18
19
20
# File 'lib/vocab/translator/rails.rb', line 17

def load_dir( dir )
  I18n.load_path = Dir.glob( "#{dir}/**/*.{yml,rb}" )
  load_translations
end

#load_file(file) ⇒ Object



22
23
24
25
26
# File 'lib/vocab/translator/rails.rb', line 22

def load_file( file )
  @locale = File.basename( file ).gsub( /\..*$/, '' ).to_sym
  I18n.load_path = [ file ]
  load_translations
end

#store(key, value) ⇒ Object



43
44
45
46
47
48
# File 'lib/vocab/translator/rails.rb', line 43

def store( key, value )
  keys = I18n.normalize_keys( @locale, key, [], nil)
  keys.shift # remove locale
  data = keys.reverse.inject( value ) { |result, _key| { _key => result } }
  @backend.store_translations( @locale, data )
end

#translations(options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/vocab/translator/rails.rb', line 33

def translations( options = {} )
  t = @backend.send(:translations)
  trans = t[ @locale ]
  return options[ :prefix ] == true ? { @locale => trans } : trans
end

#write_file(file) ⇒ Object



28
29
30
31
# File 'lib/vocab/translator/rails.rb', line 28

def write_file( file )
  yaml = { @locale => self.translations }.to_yaml
  File.open( file, 'w+' ) { |f| f.write( yaml ) }
end