Module: TT::I18nSync::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/t_t/i18n_sync.rb

Instance Method Summary collapse

Instance Method Details

#flat_file(*args) ⇒ Object



18
19
20
# File 'lib/t_t/i18n_sync.rb', line 18

def flat_file(*args)
  flat_hash(load_file(*args))
end

#flat_hash(value, key = nil) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/t_t/i18n_sync.rb', line 9

def flat_hash(value, key = nil)
  case value
  when Hash
    value.inject({}) { |r, (k, v)| r.merge! flat_hash(v, [key, k].compact.join('/')) }
  else
    { key => value }
  end
end

#load_file(path, locale) {|content| ... } ⇒ Object

Yields:

  • (content)


22
23
24
25
26
27
28
# File 'lib/t_t/i18n_sync.rb', line 22

def load_file(path, locale, &block)
  content = YAML.load_file(path)
  yield content if block_given?
  content.fetch(locale.to_s) do
    TT.raise_error "expected #{ path } should contain `#{ locale }` translations"
  end
end

#sync_file(path, locale, standard, mark) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/t_t/i18n_sync.rb', line 30

def sync_file(path, locale, standard, mark)
  old_review = {}
  source = load_file(path, locale) { |content| old_review.merge!(content.fetch('review', {})) }
  new_review = {}
  content = { locale => sync_level(standard, source, new_review, mark) }
  review = old_review.merge(flat_hash(new_review))
  content['review'] = review unless review.empty?
  write_file(path, content)
end

#write_file(path, content) ⇒ Object



40
41
42
# File 'lib/t_t/i18n_sync.rb', line 40

def write_file(path, content)
  File.open("#{ path }", "wb") { |stream| YAML.dump(content, stream, line_width: 1000) }
end