Class: Vocab::Merger::Rails
- Inherits:
-
Base
- Object
- Base
- Vocab::Merger::Rails
show all
- Defined in:
- lib/vocab/merger/rails.rb
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.
5
6
7
8
|
# File 'lib/vocab/merger/rails.rb', line 5
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
Instance Method Details
#create_if_missing(path) ⇒ Object
81
82
83
84
85
86
|
# File 'lib/vocab/merger/rails.rb', line 81
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_merge ⇒ Object
93
94
95
96
97
98
99
100
101
|
# File 'lib/vocab/merger/rails.rb', line 93
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) ⇒ Object
10
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
|
# File 'lib/vocab/merger/rails.rb', line 10
def merge_file( locales_path )
return unless translatable?( locales_path )
create_if_missing( locales_path )
keys = Vocab::Merger::Rails.keys_for_file( locales_path )
locales_translator = translator( locales_path )
locales = locales_translator.flattened_translations
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
keys.each do |key|
next if Vocab::Translator::Base.ignore_key?( key )
value = updates[ key ] || locales[ key ]
if value
locales_translator.store( key, value )
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/vocab/merger/rails.rb', line 52
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_locales ⇒ Object
TODO cache this so you don’t hit the FS so much
89
90
91
|
# File 'lib/vocab/merger/rails.rb', line 89
def translation_locales
return Dir.glob( "#{@updates_dir}/*.yml" ).collect { |f| File.basename( f, '.yml' ) }
end
|
#translator(path) ⇒ Object
75
76
77
78
79
|
# File 'lib/vocab/merger/rails.rb', line 75
def translator( path )
translator = Vocab::Translator::Rails.new
translator.load_file( path ) if File.exists?( path )
return translator
end
|