-
Notifications
You must be signed in to change notification settings - Fork 74
/
Rakefile
52 lines (41 loc) · 1.2 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
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
44
45
46
47
48
49
50
51
52
require "bundler"
require "rake"
require "rspec/core"
require "rspec/core/rake_task"
require "yaml"
begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList["spec/**/*_spec.rb"]
end
def en_locale
@en_locale ||= YAML.load_file(File.join("rails", "locales", "en.yml"))
end
def merge_locales(source, target)
source.inject({}) do |locale, (key, value)|
if value.is_a?(Hash)
locale[key] = merge_locales(value, target[key] || {})
else
locale[key] = target[key].nil? ? source[key] : target[key]
end
locale
end
end
task default: :spec
task :fill do
Dir.glob(File.join("rails", "locales", "*.yml")).each do |file|
next if File.basename(file) == "en.yml"
puts "Processing #{File.basename(file)}"
locale = YAML.load_file(file)
lang = locale.keys.first.dup
merged_locale = merge_locales(en_locale.fetch("en"), locale.fetch(lang))
File.open(file, "w") do |f|
f.write(YAML.dump({ lang => merged_locale }, line_width: -1).gsub("---\n", ''))
end
end
end