Class: Traduki::Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/traduki/translation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lang = nil) ⇒ Translation

Returns a new instance of Translation.

Raises:



11
12
13
14
15
16
17
18
19
20
# File 'lib/traduki/translation.rb', line 11

def initialize(lang = nil)
  raise EmptyPath, 'Language cannot be nil.' if lang.nil?
  @lang     = lang
  @path     = Traduki.config.langdir + '/' + lang + '.json'
  @metapath = Traduki.config.langdir + '/' + 'metadata.json'
  @contrast = {}
  @meta_contrast = []
  load_data
  load_meta
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/traduki/translation.rb', line 9

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/traduki/translation.rb', line 8

def path
  @path
end

Class Method Details

.[](lang) ⇒ Object



22
23
24
# File 'lib/traduki/translation.rb', line 22

def self.[](lang)
  new lang
end

Instance Method Details

#[](dotkey) ⇒ Object



26
27
28
# File 'lib/traduki/translation.rb', line 26

def [](dotkey)
  HashTool.get_value @data, dotkey
end

#add(dotkey, path, desc = nil, placeholders = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/traduki/translation.rb', line 30

def add(dotkey, path, desc = nil, placeholders = nil)
  occurences = []
  occurences = @meta[dotkey][:occurences] if @meta[dotkey] && @meta[dotkey][:occurences]
  @meta[dotkey] = {
    occurences: (occurences + [path]).uniq,
    placeholders: placeholders,
    desc: desc
  }
  @meta_contrast.push dotkey
  HashTool.safe_set @data, dotkey
  HashTool.safe_set @contrast, dotkey
end

#cleanObject



47
48
49
50
51
52
53
# File 'lib/traduki/translation.rb', line 47

def clean
  @data = HashTool.clean @data, @contrast
  new_meta = {}
  @meta_contrast.each { |m| new_meta[m] = @meta[m] }
  @meta = new_meta
  self
end

#clean_contrastObject



55
56
57
# File 'lib/traduki/translation.rb', line 55

def clean_contrast
  @contrast = {}
end

#key?(dotkey) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/traduki/translation.rb', line 43

def key?(dotkey)
  HashTool.key? @data, dotkey
end

#load_dataObject



74
75
76
77
78
79
80
# File 'lib/traduki/translation.rb', line 74

def load_data
  @data = JSON.parse File.read @path
rescue Errno::ENOENT
  @data = {}
rescue Errno::EACCES
  raise FileCannotAccess
end

#load_metaObject



82
83
84
85
86
87
88
89
90
# File 'lib/traduki/translation.rb', line 82

def load_meta
  @meta = JSON.parse File.read @metapath
  @keys = @meta.keys
rescue Errno::ENOENT
  @meta = {}
  @keys = []
rescue Errno::EACCES
  raise FileCannotAccess
end

#saveObject



59
60
61
62
63
64
65
# File 'lib/traduki/translation.rb', line 59

def save
  dir = File.split(@path).first
  FileUtils.mkpath dir unless Dir.exist? dir
  # File.open(@path, 'w') { |f| f.write "// Please DON'T edit this file.\n" }
  File.open(@path, 'w') { |f| f.write JSON.generate @data }
  save_meta
end

#save_metaObject



67
68
69
70
71
72
# File 'lib/traduki/translation.rb', line 67

def save_meta
  dir = File.split(@metapath).first
  FileUtils.mkpath dir unless Dir.exist? dir
  # File.open(@metapath, 'w') { |f| f.write "// Please DON'T edit this file.\n" }
  File.open(@metapath, 'w') { |f| f.write JSON.generate @meta }
end