Class: Traduki::Translation
- Inherits:
-
Object
- Object
- Traduki::Translation
- Defined in:
- lib/traduki/translation.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #[](dotkey) ⇒ Object
- #add(dotkey, path, desc = nil, placeholders = nil) ⇒ Object
- #clean ⇒ Object
- #clean_contrast ⇒ Object
-
#initialize(lang = nil) ⇒ Translation
constructor
A new instance of Translation.
- #key?(dotkey) ⇒ Boolean
- #load_data ⇒ Object
- #load_meta ⇒ Object
- #save ⇒ Object
- #save_meta ⇒ Object
Constructor Details
#initialize(lang = nil) ⇒ Translation
Returns a new instance of Translation.
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 end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
9 10 11 |
# File 'lib/traduki/translation.rb', line 9 def data @data end |
#path ⇒ Object (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 |
#clean ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/traduki/translation.rb', line 47 def clean @data = HashTool.clean @data, @contrast = {} @meta_contrast.each { |m| [m] = @meta[m] } @meta = self end |
#clean_contrast ⇒ Object
55 56 57 |
# File 'lib/traduki/translation.rb', line 55 def clean_contrast @contrast = {} end |
#key?(dotkey) ⇒ Boolean
43 44 45 |
# File 'lib/traduki/translation.rb', line 43 def key?(dotkey) HashTool.key? @data, dotkey end |
#load_data ⇒ Object
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_meta ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/traduki/translation.rb', line 82 def @meta = JSON.parse File.read @metapath @keys = @meta.keys rescue Errno::ENOENT @meta = {} @keys = [] rescue Errno::EACCES raise FileCannotAccess end |
#save ⇒ Object
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 } end |
#save_meta ⇒ Object
67 68 69 70 71 72 |
# File 'lib/traduki/translation.rb', line 67 def 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 |