forked from solidusio/solidus_i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
81 lines (68 loc) · 2.4 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'bundler'
Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :default do
if Dir["spec/dummy"].empty?
Rake::Task[:test_app].invoke
Dir.chdir("../../")
end
Rake::Task[:spec].invoke
end
require 'spree/testing_support/common_rake'
desc 'Generates a dummy app for testing'
task :test_app do
ENV['LIB_NAME'] = 'solidus_i18n'
Rake::Task['common:test_app'].invoke
end
require 'solidus_i18n'
namespace :solidus_i18n do
desc 'Update by retrieving the latest Solidus locale files'
task :update_default do
puts "Fetching latest Solidus locale file to #{locales_dir}"
require 'uri'
require 'net/https'
location = 'https://raw.github.com/solidusio/solidus/master/core/config/locales/en.yml'
begin
uri = URI.parse(location)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
puts "Getting from #{uri}"
request = Net::HTTP::Get.new(uri.request_uri)
case response = http.request(request)
when Net::HTTPRedirection then location = response['location']
when Net::HTTPClientError, Net::HTTPServerError then response.error!
end
end until Net::HTTPSuccess == response
FileUtils.mkdir_p(default_dir) unless File.directory?(default_dir)
File.open("#{default_dir}/solidus_core.yml", 'w') { |file| file << response.body }
end
desc 'Syncronize translation files with latest en (adds comments with fallback en value)'
task :sync do
puts 'Starting syncronization...'
words = translation_keys
Dir["#{locales_dir}/*.yml"].each do |filename|
basename = File.basename(filename, '.yml')
(comments, other) = SolidusI18n::Utils.read_file(filename, basename)
# Initializing hash variable as en fallback if it does not exist
words.each { |k, _v| other[k] ||= "#{words[k]}" }
# Remove if not defined in en locale
other.delete_if { |k, _v| !words[k] }
SolidusI18n::Utils.write_file(filename, basename, comments, other, false)
end
end
def translation_keys
(dummy_comments, words) = SolidusI18n::Utils.read_file(File.dirname(__FILE__) + '/default/solidus_core.yml', 'en')
words
end
def locales_dir
File.join File.dirname(__FILE__), 'config/locales'
end
def default_dir
File.join File.dirname(__FILE__), 'default'
end
def env_locale
ENV['LOCALE'].presence
end
end