locale_assistant 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/locale_assistant.rb +184 -0
  2. metadata +70 -0
@@ -0,0 +1,184 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ begin
6
+ require Dir.pwd+'/config/l_assistant.conf.rb'
7
+ rescue LoadError
8
+ puts 'Needs a config file located at CURRENT_DIR/config/l_assistant.conf.rb'
9
+ puts 'something like this:'
10
+ puts %Q|
11
+ module LocaleAssistant
12
+ Files = ['config/locales/#lang#.yml',
13
+ 'config/locales/#lang#_models.yml',
14
+ 'vendor/engines/contract_management/config/locales/#lang#.yml',
15
+ 'vendor/engines/contract_management/config/locales/#lang#_models.yml' ]
16
+ Languages = ['hu','en','he']
17
+ IgnoreList = []
18
+ end
19
+ |
20
+ end
21
+
22
+ def load_file(fn)
23
+ arr = []
24
+ hash = {}
25
+
26
+ keystore = []
27
+ indentstore = []
28
+ spaces = -1
29
+
30
+ f = File.open(fn)
31
+ f.each_line do |line|
32
+ curr_spaces = (line.size - line.lstrip.size)
33
+ key,val = line.strip.split(/:/,2)
34
+ val = val.strip
35
+
36
+ if curr_spaces > spaces
37
+ indentstore.push(curr_spaces-spaces)
38
+ spaces = curr_spaces
39
+ keystore.push(key)
40
+ elsif curr_spaces == spaces
41
+ keystore.pop
42
+ keystore.push(key)
43
+ elsif curr_spaces < spaces
44
+ keystore.pop
45
+ while curr_spaces < spaces
46
+ spaces -= indentstore.pop
47
+ keystore.pop
48
+ end
49
+ keystore.push(key)
50
+ end
51
+
52
+ unless val == ''
53
+ x = keystore.join('.')
54
+ hash[x] = val
55
+ arr.push([x,val])
56
+ end
57
+
58
+ end
59
+ f.close
60
+
61
+ arr.sort!
62
+ return [arr,hash]
63
+ end
64
+
65
+ def write_out(fn,array,hash,country_code)
66
+ f = File.new(fn,'w+')
67
+ prev_x = []
68
+ indent = 0
69
+
70
+ array.each do |key,val|
71
+ next if LocaleAssistant::IgnoreList.include?(key)
72
+ x = key.split('.')
73
+ x[0] = country_code
74
+
75
+ counter = 0
76
+ prev_x.each do |fragment|
77
+ counter +=1
78
+ break if x[counter-1] != fragment
79
+ end
80
+
81
+ indent = 0
82
+ counter -= 1 if counter > 1
83
+ x[counter..-1].each do |fragment|
84
+ f << (' '*(counter+indent)) + fragment + ':'
85
+ if indent+1 == x[counter..-1].size
86
+ value2 = hash[country_code+key[2..-1]]
87
+ if value2.nil?
88
+ f << ' "TODO ('+val.to_s+')"'
89
+ else
90
+ f << ' "' + value2.to_s + '"'
91
+ end
92
+ end
93
+ f << "\n"
94
+ indent += 1
95
+ end
96
+
97
+ prev_x = x
98
+ end
99
+ end
100
+
101
+ def process_file(path,file,source,inspecting_mode,destructive,use_tempfile)
102
+ arrays = {}
103
+ hashes = {}
104
+ LocaleAssistant::Languages.each do |lang|
105
+ arrays[lang],hashes[lang] = load_file(path + file.gsub('#lang#',lang))
106
+ end
107
+
108
+ arrays[source].each do |key,val|
109
+ general_key = key[2..-1]
110
+ (LocaleAssistant::Languages-[source]).each do |lang|
111
+ localized_key = lang+general_key
112
+ unless hashes[lang].include?(localized_key)
113
+ if LocaleAssistant::IgnoreList.include?(localized_key)
114
+ #puts fn_beg+lang+fn_end + ' ignoring:' + localized_key
115
+ else
116
+ arrays[lang] << [localized_key,'??? '+val.to_s]
117
+ puts file.gsub('#lang#',lang) + ' MISSING: ' + localized_key
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ #ignore lists
124
+ LocaleAssistant::IgnoreList.each do |key|
125
+ lang = key[0..1]
126
+ base_key = key[3..-1]
127
+ arrays[lang] << [key,hashes[lang][base_key]]
128
+ end
129
+
130
+ if inspecting_mode == false
131
+ LocaleAssistant::Languages.each do |lang|
132
+ (arrays[lang]).sort! {|a,b| a[0] <=> b[0]}
133
+ language_to_use = lang
134
+ language_to_use = source if destructive
135
+
136
+ file_suffix = ''
137
+ file_suffix = '.temp' if use_tempfile
138
+
139
+ write_out(path + file.gsub('#lang#',lang)+file_suffix,arrays[language_to_use],hashes[lang],lang)
140
+ end
141
+ end
142
+ end
143
+
144
+ def parse_command_line
145
+ options = {}
146
+
147
+ optparse = OptionParser.new do |opts|
148
+ opts.banner = "Usage: #{__FILE__} [options] source_language"
149
+
150
+ options[:destructive] = false
151
+ opts.on( '-D', '--destructive', 'If you want to clear keys that are not in the source language' ) do
152
+ options[:destructive] = true
153
+ end
154
+
155
+ options[:tempfile] = false
156
+ opts.on( '-t', '--tempfile', 'Use .temp files for inspecting before overwriting' ) do
157
+ options[:tempfile] = true
158
+ end
159
+
160
+ options[:inspecting_mode] = false
161
+ opts.on( '-i', '--inspecting-mode', 'Do not make changes, just list the missing keys' ) do
162
+ options[:inspecting_mode] = true
163
+ end
164
+
165
+ opts.on( '-h', '--help', 'Display this screen' ) do
166
+ puts opts
167
+ exit
168
+ end
169
+ end
170
+
171
+ optparse.parse!
172
+ return options
173
+ end
174
+
175
+ options = parse_command_line
176
+
177
+ if LocaleAssistant::Languages.include?(ARGV[0])
178
+ puts "using " + ARGV[0] + " as source language"
179
+ LocaleAssistant::Files.each do |file|
180
+ process_file(Dir.pwd+'/',file,ARGV[0],options[:inspecting_mode],options[:destructive],options[:tempfile])
181
+ end
182
+ else
183
+ puts "Usage: #{__FILE__} [options] source_language"
184
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: locale_assistant
3
+ version: !ruby/object:Gem::Version
4
+ hash: 59
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 0
10
+ version: 0.9.0
11
+ platform: ruby
12
+ authors:
13
+ - jsaak
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-03-04 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: |-
23
+ locale_assistant is lightweight commandline tool
24
+ which helps you manage those big yaml files when using rails and multiple languages
25
+ if you want to keep things simple and do not need online services like localeapp
26
+ email: jsaak@email.to
27
+ executables:
28
+ - locale_assistant.rb
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - bin/locale_assistant.rb
35
+ has_rdoc: true
36
+ homepage: https://github.com/jsaak/locale_assistant
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.7
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Helps you manage those big locale yaml files
69
+ test_files: []
70
+