Class: Voltron::Translate::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/voltron/translate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale) ⇒ Translator

Returns a new instance of Translator.



46
47
48
# File 'lib/voltron/translate.rb', line 46

def initialize(locale)
  @locale = locale.to_s
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



44
45
46
# File 'lib/voltron/translate.rb', line 44

def locale
  @locale
end

Instance Method Details

#destroyObject



54
55
56
57
# File 'lib/voltron/translate.rb', line 54

def destroy
  File.unlink(path) if File.exists?(path)
  reload
end

#full_listObject

A google hash object representing the translation file, where the key is the original text, and the value is the translated text



84
85
86
87
88
89
90
91
# File 'lib/voltron/translate.rb', line 84

def full_list
  reload if has_been_modified?
  @@full_list ||= begin
    hsh = ::GoogleHashDenseRubyToRuby.new
    data.each { |k,v| hsh[k] = v }
    hsh
  end
end

#last_modifiedObject

The last time (in microseconds) the translation file was updated



94
95
96
# File 'lib/voltron/translate.rb', line 94

def last_modified
  Rails.cache.fetch("translations/data/modified/#{locale}") { modified }
end

#listObject

Almost the same as full_list, but does not include translations whose from/to are identical, since that would just be unnecessarily inflating the size of the hash



73
74
75
76
77
78
79
80
# File 'lib/voltron/translate.rb', line 73

def list
  reload if has_been_modified?
  @@list ||= begin
    hsh = ::GoogleHashDenseRubyToRuby.new
    data.each { |k,v| hsh[k] = v unless k == v }
    hsh
  end
end

#pathObject



59
60
61
# File 'lib/voltron/translate.rb', line 59

def path
  @path ||= Rails.root.join('config', 'locales', "#{locale}.csv")
end

#translate(text, **args) ⇒ Object



50
51
52
# File 'lib/voltron/translate.rb', line 50

def translate(text, **args)
  phrase(text, args) % args
end

#write(text) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/voltron/translate.rb', line 63

def write(text)
  # If the full list of translations does not contain the given text content, add it
  unless full_list.has_key?(text)
    CSV.open(path, 'a', force_quotes: true) { |f| f.puts [text, text] }
    Voltron.log "Added translation for text '#{text}' (Locale: #{locale})", 'Translate', ::Voltron::Translate::LOG_COLOR
  end
end