module TT::I18nSync::Utils
Public Instance Methods
flat_file(*args)
click to toggle source
# File lib/t_t/i18n_sync.rb, line 18 def flat_file(*args) flat_hash(load_file(*args)) end
flat_hash(value, key = nil)
click to toggle source
# 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| ... }
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
sync_level(st_level, source, review, mark)
click to toggle source
# File lib/t_t/i18n_sync.rb, line 46 def sync_level(st_level, source, review, mark) level = st_level.inject({}) do |r, (key, st_node)| node = source[key] r[key] = case st_node when Hash sub_review = {} sub_level = sync_level(st_node, (node.is_a?(Hash) ? node : {}), sub_review, mark) review[key] = sub_review unless sub_review.empty? sub_level when Array then node.is_a?(Array) ? node : st_node.map { |v| "#{ mark }#{ v }" } else node.nil? ? "#{ mark }#{ st_node }" : node end r end (source.keys - st_level.keys).each { |key| review[key] = source[key] } level end