Class: Vocab::Merger::Rails

Inherits:
Base
  • Object
show all
Defined in:
lib/vocab/merger/rails.rb

Constant Summary collapse

INTERPOLATION_PATTERN =
/%{(.*?)}/

Instance Attribute Summary

Attributes inherited from Base

#locales_dir, #updates_dir

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#merge

Constructor Details

#initialize(locales_dir = nil, updates_dir = nil) ⇒ Rails

Returns a new instance of Rails.



6
7
8
9
# File 'lib/vocab/merger/rails.rb', line 6

def initialize( locales_dir = nil, updates_dir = nil )
  @locales_dir = locales_dir || 'config/locales'
  @updates_dir = updates_dir || 'tmp/translations'
end

Class Method Details

.keys_for_file(path) ⇒ Object



82
83
84
85
86
87
# File 'lib/vocab/merger/rails.rb', line 82

def self.keys_for_file( path )
  en_path = Vocab::Translator::Rails.en_equivalent_path( path )
  translator = Vocab::Translator::Rails.new
  translator.load_file( en_path )
  return translator.flattened_translations.keys
end

.load_english(path) ⇒ Object



89
90
91
92
93
94
# File 'lib/vocab/merger/rails.rb', line 89

def self.load_english( path )
  en_path = Vocab::Translator::Rails.en_equivalent_path( path )
  translator = Vocab::Translator::Rails.new
  translator.load_file( en_path )
  return translator.flattened_translations
end

Instance Method Details

#check_all_interpolations(file, strict = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vocab/merger/rails.rb', line 62

def check_all_interpolations( file, strict = false )
  # list of keys that need to be in the translated file
  keys = Vocab::Merger::Rails.keys_for_file( file )
  english = Vocab::Merger::Rails.load_english( file )

  # existing translations already in the file
  locales_translator = translator( file )
  locales = locales_translator.flattened_translations

  keys.each do |key|
    next if Vocab::Translator::Base.ignore_key?( key )

    value = locales[ key ]
    if value
      check_matching_interpolations( key, english[ key ], value, file, strict )
    end
  end
  return nil
end

#check_matching_interpolations(key, old_value, new_value, locales_path, strict = false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vocab/merger/rails.rb', line 49

def check_matching_interpolations( key, old_value, new_value, locales_path, strict = false )
  old_interpolations = old_value.to_s.scan( INTERPOLATION_PATTERN )
  new_interpolations = new_value.to_s.scan( INTERPOLATION_PATTERN )
  unless strict
    old_interpolations.sort!
    new_interpolations.sort!
  end

  if old_interpolations != new_interpolations
    Vocab.ui.warn( "Interpolation mismatch for key #{key} in #{locales_path}. \n English: #{old_value} Translation: #{new_value}" )
  end
end

#create_if_missing(path) ⇒ Object



125
126
127
128
129
130
# File 'lib/vocab/merger/rails.rb', line 125

def create_if_missing( path )
  return if File.exists?( path )
  locale = File.basename( path, '.yml' )
  Vocab.ui.say( "Creating file #{path}" )
  File.open( path, 'w+' ) { |file| file.write( { locale => {} }.to_yaml ) }
end

#files_to_mergeObject



137
138
139
140
141
142
143
144
145
# File 'lib/vocab/merger/rails.rb', line 137

def files_to_merge
  paths = []
  Dir.glob( "#{@locales_dir}/**/en.yml" ).each do |en_path|
    translation_locales.each do |locale|
      paths << en_path.gsub( /en.yml$/, "#{locale}.yml" )
    end
  end
  return paths
end

#merge_file(locales_path, strict = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vocab/merger/rails.rb', line 11

def merge_file( locales_path, strict = false )
  return unless translatable?( locales_path )
  create_if_missing( locales_path )

  # list of keys that need to be in the translated file
  keys = Vocab::Merger::Rails.keys_for_file( locales_path )
  english = Vocab::Merger::Rails.load_english( locales_path )

  # existing translations already in the file
  locales_translator = translator( locales_path )
  locales = locales_translator.flattened_translations

  # new translations from the translators
  update_path = "#{@updates_dir}/#{locales_translator.locale}.yml"
  unless File.exists?( update_path )
    Vocab.ui.say( "Missing update file: #{update_path} to translate #{locales_path}" )
    return
  end
  updates_translator = translator( update_path )
  updates = updates_translator.flattened_translations


  # apply updated keys to locales hash
  keys.each do |key|
    next if Vocab::Translator::Base.ignore_key?( key )

    value = updates[ key ] || locales[ key ]
    if value
      locales_translator.store( key, value )
      check_matching_interpolations( key, english[ key ], value, locales_path, strict )
    else
      Vocab.ui.warn( "No translation found for key #{key} while merging #{locales_path}" )
    end
  end

  locales_translator.write_file( locales_path )
end

#translatable?(path) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vocab/merger/rails.rb', line 96

def translatable?( path )
  if File.basename( path ) == 'en.yml'
    Vocab.ui.warn( "can't translate english file #{path}" )
    return false
  end

  unless File.exists?( Vocab::Translator::Rails.en_equivalent_path( path ) )
    Vocab.ui.warn( "skipping because no english equivalent for #{path}" )
    return false
  end

  if( File.exists?( path ) )
    extension = File.basename( path, '.yml' )
    contents = YAML.load_file( path ).keys.first.to_s
    if( extension != contents )
      Vocab.ui.warn( "File extension does not match file contents in #{path}" )
      return false
    end
  end

  return true
end

#translation_localesObject

TODO cache this so you don’t hit the FS so much



133
134
135
# File 'lib/vocab/merger/rails.rb', line 133

def translation_locales
  return Dir.glob( "#{@updates_dir}/*.yml" ).collect { |f| File.basename( f, '.yml' ) }
end

#translator(path) ⇒ Object



119
120
121
122
123
# File 'lib/vocab/merger/rails.rb', line 119

def translator( path )
  translator = Vocab::Translator::Rails.new
  translator.load_file( path ) if File.exists?( path )
  return translator
end