web_translate_it 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -7
- data/history.md +7 -0
- data/lib/web_translate_it.rb +2 -10
- data/lib/web_translate_it/configuration.rb +1 -1
- data/lib/web_translate_it/tasks.rb +17 -36
- data/lib/web_translate_it/translation_file.rb +28 -30
- data/lib/web_translate_it/util.rb +5 -9
- data/spec/web_translate_it/translation_file_spec.rb +45 -1
- data/version.yml +4 -0
- metadata +3 -2
data/README.md
CHANGED
@@ -11,7 +11,7 @@ This gem provides your app with:
|
|
11
11
|
|
12
12
|
* For each environment you want to use the gem, add to your config/environment/development.rb:
|
13
13
|
|
14
|
-
`config.gem 'web_translate_it', :version => '~> 1.4.
|
14
|
+
`config.gem 'web_translate_it', :version => '~> 1.4.1', :source => 'http://gemcutter.org'`
|
15
15
|
|
16
16
|
* Then, run:
|
17
17
|
|
@@ -56,13 +56,13 @@ Fetch the latest translations for all your files for all languages defined in We
|
|
56
56
|
|
57
57
|
Fetch the latest translations for all the languages defined in Web Translate It’s interface. It takes the locale name as a parameter
|
58
58
|
|
59
|
-
rake trans:
|
59
|
+
rake trans:upload[fr_FR]
|
60
60
|
|
61
|
-
|
61
|
+
Upload to Web Translate It your files in a specific locale defined in Web Translate It’s interface.
|
62
62
|
|
63
|
-
rake trans:
|
64
|
-
|
65
|
-
|
63
|
+
rake trans:config
|
64
|
+
|
65
|
+
Copy a `translation.yml` file to `config/translation.yml` if the file doesn’t exist.
|
66
66
|
|
67
67
|
### Automatically fetch new language files
|
68
68
|
|
@@ -77,7 +77,7 @@ Use the rack middleware!
|
|
77
77
|
This is very much specific to your app, this is left as an exercise to the reader. You can inspire yourself from
|
78
78
|
Ryan Tomakyo’s [locale.rb](http://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/locale.rb).
|
79
79
|
You can also find an example of a very simple middleware using the `locale` parameter in
|
80
|
-
|
80
|
+
[examples/locale.rb](http://github.com/AtelierConvivialite/webtranslateit/blob/master/examples/locale.rb).
|
81
81
|
|
82
82
|
* The next step is to setup the `autofetch` middleware. Add in `config/environments/development.rb` and any other
|
83
83
|
environments you want to autofetch this line:
|
data/history.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## Edge
|
2
|
+
|
3
|
+
* Rename `rake trans:send[fr_Fr]` to `rake trans:upload[fr_FR]`
|
4
|
+
* Remove `rake trans:version` task. Instead, version number is displayed in the welcome banner.
|
5
|
+
* Code refactoring
|
6
|
+
* More tests
|
7
|
+
|
1
8
|
## Version 1.4.0 / 2010-01-06
|
2
9
|
|
3
10
|
* The plugin is now a gem
|
data/lib/web_translate_it.rb
CHANGED
@@ -4,19 +4,11 @@ require File.join(File.dirname(__FILE__), 'web_translate_it', 'translation_file'
|
|
4
4
|
require File.join(File.dirname(__FILE__), 'web_translate_it', 'auto_fetch')
|
5
5
|
|
6
6
|
module WebTranslateIt
|
7
|
-
def self.version
|
8
|
-
hash = YAML.load_file File.join(File.dirname(__FILE__), '../version.yml')
|
9
|
-
[hash[:major], hash[:minor], hash[:patch]].join('.')
|
10
|
-
end
|
11
|
-
|
12
7
|
def self.fetch_translations
|
13
8
|
config = Configuration.new
|
14
9
|
locale = I18n.locale.to_s
|
15
10
|
return if config.ignore_locales.include?(locale)
|
16
|
-
puts "Looking for #{locale} translations
|
17
|
-
config.files.each
|
18
|
-
response_code = file.fetch(locale)
|
19
|
-
puts "Done. Response code: #{response_code}"
|
20
|
-
end
|
11
|
+
puts "Looking for #{locale} translations…"
|
12
|
+
config.files.each {|file| puts "Done. Response: #{file.fetch(locale)}" }
|
21
13
|
end
|
22
14
|
end
|
@@ -4,18 +4,11 @@ namespace :trans do
|
|
4
4
|
desc "Fetch translation files from Web Translate It"
|
5
5
|
task :fetch, :locale do |t, args|
|
6
6
|
welcome_message
|
7
|
-
colour_puts "<b>Fetching file for locale #{args.locale}
|
7
|
+
colour_puts "<b>Fetching file for locale #{args.locale}…</b>"
|
8
8
|
configuration = WebTranslateIt::Configuration.new
|
9
9
|
configuration.files.each do |file|
|
10
10
|
response_code = file.fetch(args.locale)
|
11
|
-
|
12
|
-
when 200
|
13
|
-
colour_puts "<green>#{file.file_path_for_locale(args.locale)}: 200 OK. Saving changes</green>"
|
14
|
-
when 304
|
15
|
-
colour_puts "<green>#{file.file_path_for_locale(args.locale)}: 304 Not Modified</green>"
|
16
|
-
else
|
17
|
-
colour_puts "<red>#{file.file_path_for_locale(args.locale)}: Error, unhandled response: #{response_code}</red>"
|
18
|
-
end
|
11
|
+
handle_response(file.file_path_for_locale(args.locale), response_code)
|
19
12
|
end
|
20
13
|
end
|
21
14
|
|
@@ -28,49 +21,39 @@ namespace :trans do
|
|
28
21
|
configuration.ignore_locales.each do |ignore|
|
29
22
|
locales.delete(ignore)
|
30
23
|
end
|
31
|
-
colour_puts "<b>Fetching all files for all locales
|
24
|
+
colour_puts "<b>Fetching all files for all locales…</b>"
|
32
25
|
locales.each do |locale|
|
33
26
|
configuration.files.each do |file|
|
34
27
|
response_code = file.fetch(locale)
|
35
|
-
|
36
|
-
when 200
|
37
|
-
colour_puts "<green>#{file.file_path_for_locale(locale)}: 200 OK.</green>"
|
38
|
-
when 304
|
39
|
-
colour_puts "<green>#{file.file_path_for_locale(locale)}: 304 Not Modified</green>"
|
40
|
-
else
|
41
|
-
colour_puts "<red>#{file.file_path_for_locale(locale)}: Error, unhandled response: #{response_code}</red>"
|
42
|
-
end
|
28
|
+
handle_response(file.file_path_for_locale(locale), response_code)
|
43
29
|
end
|
44
30
|
end
|
45
31
|
end
|
46
32
|
end
|
47
33
|
|
48
|
-
desc "
|
49
|
-
task :
|
34
|
+
desc "Upload a translation file to Web Translate It"
|
35
|
+
task :upload, :locale do |t, args|
|
50
36
|
welcome_message
|
51
|
-
colour_puts "<b>
|
37
|
+
colour_puts "<b>Uploading file for locale #{args.locale}…</b>"
|
52
38
|
configuration = WebTranslateIt::Configuration.new
|
53
39
|
configuration.files.each do |file|
|
54
|
-
response_code = file.
|
55
|
-
|
56
|
-
when 200
|
57
|
-
colour_puts "<green>#{file.file_path_for_locale(args.locale)} uploaded OK.</green>"
|
58
|
-
else
|
59
|
-
colour_puts "<red>#{file.file_path_for_locale(args.locale)}: Error uploading, unhandled response: #{response_code}</red>"
|
60
|
-
end
|
40
|
+
response_code = file.upload(args.locale)
|
41
|
+
handle_response(file.file_path_for_locale(args.locale), response_code)
|
61
42
|
end
|
62
43
|
end
|
63
44
|
|
64
45
|
desc "Install Web Translate It for your application"
|
65
46
|
task :config do
|
66
47
|
welcome_message
|
67
|
-
WebTranslateIt::Configuration.
|
48
|
+
WebTranslateIt::Configuration.create_config_file
|
68
49
|
end
|
69
50
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
51
|
+
def handle_response(file_path, response_code)
|
52
|
+
if response_code < 400
|
53
|
+
colour_puts "<green>#{file_path}: #{response_code}, OK</green>"
|
54
|
+
else
|
55
|
+
colour_puts "<red>#{file_path}: #{response_code}, Problem!</red>"
|
56
|
+
end
|
74
57
|
end
|
75
58
|
|
76
59
|
def welcome_message
|
@@ -84,9 +67,7 @@ namespace :trans do
|
|
84
67
|
private
|
85
68
|
|
86
69
|
WELCOME_SCREEN = <<-EO_WELCOME
|
87
|
-
|
88
|
-
<banner>Web Translate It</banner>
|
70
|
+
<banner>Web Translate It v#{WebTranslateIt::Util.version}</banner>
|
89
71
|
|
90
72
|
EO_WELCOME
|
91
|
-
|
92
73
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module WebTranslateIt
|
2
2
|
class TranslationFile
|
3
3
|
require 'net/https'
|
4
|
+
require 'net/http/post/multipart'
|
4
5
|
require 'time'
|
5
6
|
|
6
7
|
attr_accessor :id, :file_path, :api_key
|
@@ -11,46 +12,43 @@ module WebTranslateIt
|
|
11
12
|
self.api_key = api_key
|
12
13
|
end
|
13
14
|
|
14
|
-
def fetch(locale)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
http.
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
request.add_field('If-Modified-Since', File.mtime(File.new(file_path_for_locale(locale), 'r')).rfc2822)
|
23
|
-
end
|
24
|
-
response = http.request(request)
|
25
|
-
response_code = response.code.to_i
|
26
|
-
|
27
|
-
if response_code == 200 and not response.body == ''
|
28
|
-
locale_file = File.new(file_path_for_locale(locale), 'w')
|
29
|
-
locale_file.puts(response.body)
|
30
|
-
locale_file.close
|
15
|
+
def fetch(locale, force = false)
|
16
|
+
http_connection do |http|
|
17
|
+
request = Net::HTTP::Get.new(api_url(locale))
|
18
|
+
request.add_field('If-Modified-Since', File.mtime(File.new(file_path, 'r')).rfc2822) if File.exist?(file_path) and !force
|
19
|
+
response = http.request(request)
|
20
|
+
response_code = response.code.to_i
|
21
|
+
File.open(file_path_for_locale(locale), 'w'){ |f| f << response.body } if response_code == 200 and !response.body == ''
|
22
|
+
response_code
|
31
23
|
end
|
32
|
-
response_code
|
33
24
|
end
|
34
25
|
|
35
|
-
def
|
26
|
+
def upload(locale)
|
36
27
|
File.open(file_path_for_locale(locale)) do |file|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
request = Net::HTTP::Put::Multipart.new(api_url(locale), "file" => UploadIO.new(file, "text/plain", file.path))
|
43
|
-
response = http.request(request)
|
44
|
-
response.code.to_i
|
28
|
+
http_connection do |http|
|
29
|
+
request = Net::HTTP::Put::Multipart.new(api_url(locale), "file" => UploadIO.new(file, "text/plain", file.path))
|
30
|
+
response = http.request(request)
|
31
|
+
response.code.to_i
|
32
|
+
end
|
45
33
|
end
|
46
34
|
end
|
47
35
|
|
48
36
|
def file_path_for_locale(locale)
|
49
37
|
self.file_path.gsub("[locale]", locale)
|
50
38
|
end
|
39
|
+
|
40
|
+
protected
|
51
41
|
|
52
|
-
|
53
|
-
|
54
|
-
|
42
|
+
def http_connection
|
43
|
+
http = Net::HTTP.new('webtranslateit.com', 443)
|
44
|
+
http.use_ssl = true
|
45
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
46
|
+
http.read_timeout = 10
|
47
|
+
yield http
|
48
|
+
end
|
49
|
+
|
50
|
+
def api_url(locale)
|
51
|
+
"/api/projects/#{api_key}/files/#{self.id}/locales/#{locale}"
|
52
|
+
end
|
55
53
|
end
|
56
54
|
end
|
@@ -3,6 +3,11 @@ module WebTranslateIt
|
|
3
3
|
DEFAULT_TERMINAL_COLORS = "\e[0m\e[37m\e[40m"
|
4
4
|
MONOCHROME_OUTPUT = "\\1"
|
5
5
|
|
6
|
+
def self.version
|
7
|
+
hash = YAML.load_file File.join(File.dirname(__FILE__), '..', '..' '/version.yml')
|
8
|
+
[hash[:major], hash[:minor], hash[:patch]].join('.')
|
9
|
+
end
|
10
|
+
|
6
11
|
def self.colourise_output?
|
7
12
|
@colourise_output = !!(RUBY_PLATFORM !~ /mswin/ || defined?(Win32::Console::ANSI)) if @colourise_output.nil?
|
8
13
|
@colourise_output
|
@@ -16,14 +21,5 @@ module WebTranslateIt
|
|
16
21
|
data.gsub!(%r{<banner>(.*?)</banner>}m, colourise_output? ? "\e[33m\e[44m\e[1m\\1#{DEFAULT_TERMINAL_COLORS}" : MONOCHROME_OUTPUT)
|
17
22
|
data
|
18
23
|
end
|
19
|
-
|
20
|
-
def self.insert_into(file, line)
|
21
|
-
logger.insert "#{line} into #{file}"
|
22
|
-
unless options[:pretend] || file_contains?(file, line)
|
23
|
-
gsub_file file, /^(class|module) .+$/ do |match|
|
24
|
-
"#{match}\n #{line}"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
24
|
end
|
29
25
|
end
|
@@ -5,6 +5,15 @@ describe WebTranslateIt::TranslationFile do
|
|
5
5
|
@translation_file = WebTranslateIt::TranslationFile.new(1174, "/config/locales/[locale].yml", "04b254da22a6eb301b103f848e469ad494eea47d")
|
6
6
|
end
|
7
7
|
|
8
|
+
describe "#initialize" do
|
9
|
+
it "should assign id, file_path and api_key" do
|
10
|
+
tr_file = WebTranslateIt::TranslationFile.new(1174, "/config/locales/[locale].yml", "04b254da22a6eb301b103f848e469ad494eea47d")
|
11
|
+
tr_file.id.should == 1174
|
12
|
+
tr_file.file_path.should == "/config/locales/[locale].yml"
|
13
|
+
tr_file.api_key.should == "04b254da22a6eb301b103f848e469ad494eea47d"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
8
17
|
describe "#fetch" do
|
9
18
|
it "should prepare a HTTP request and get a 200 OK if the language file is stale" do
|
10
19
|
file = mock(File)
|
@@ -13,12 +22,47 @@ describe WebTranslateIt::TranslationFile do
|
|
13
22
|
@translation_file.fetch('fr_FR').should == 200
|
14
23
|
end
|
15
24
|
|
25
|
+
it "should prepare a HTTP request and get a 200 OK if the language file is stale using the force download parameter" do
|
26
|
+
file = mock(File)
|
27
|
+
file.stub(:puts => true, :close => true)
|
28
|
+
File.stub(:exist? => true, :mtime => Time.at(0), :new => file)
|
29
|
+
@translation_file.fetch('fr_FR', true).should == 200
|
30
|
+
end
|
31
|
+
|
16
32
|
it "should prepare a HTTP request and get a 304 OK if the language file is fresh" do
|
17
33
|
file = mock(File)
|
18
34
|
file.stub(:puts => true, :close => true)
|
19
35
|
File.stub(:exist? => true, :mtime => Time.now, :new => file)
|
20
|
-
@translation_file.stub(:path_to_locale_file => file)
|
21
36
|
@translation_file.fetch('fr_FR').should == 304
|
22
37
|
end
|
38
|
+
|
39
|
+
it "should prepare a HTTP request and get a 200 OK if the language file is fresh using the force download parameter" do
|
40
|
+
file = mock(File)
|
41
|
+
file.stub(:puts => true, :close => true)
|
42
|
+
File.stub(:exist? => true, :mtime => Time.now, :new => file)
|
43
|
+
@translation_file.fetch('fr_FR', true).should == 200
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#upload" do
|
48
|
+
it "should prepare a HTTP request and get a 200 OK" do
|
49
|
+
@translation_file.stub(:file_path => File.join(File.dirname(__FILE__), '..', 'examples', 'en.yml'))
|
50
|
+
@translation_file.upload('en')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should fail if the file does not exist" do
|
54
|
+
@translation_file.stub(:file_path => File.join('something', 'that', 'does', 'not', 'exist'))
|
55
|
+
lambda { @translation_file.upload('en') }.should raise_error
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#file_path_for_locale" do
|
60
|
+
it "should replace [locale] by the locale passed as a parameter" do
|
61
|
+
@translation_file.file_path_for_locale('fr').should == "/config/locales/fr.yml"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should fail if no parameter is given" do
|
65
|
+
lambda { @translation_file.file_path_for_locale }.should raise_error
|
66
|
+
end
|
23
67
|
end
|
24
68
|
end
|
data/version.yml
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: web_translate_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "\xC3\x89douard Bri\xC3\xA8re"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-07 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- history.md
|
35
35
|
- MIT-LICENSE
|
36
36
|
- README.md
|
37
|
+
- version.yml
|
37
38
|
- examples/locale.rb
|
38
39
|
- lib/web_translate_it.rb
|
39
40
|
- lib/web_translate_it/auto_fetch.rb
|