web_translate_it 1.4.2 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,6 +7,11 @@ This gem provides your app with:
7
7
  * a set of 4 handy rake task to fetch your translations.
8
8
  * a rack middleware to automatically fetch new translations from Web Translate It.
9
9
 
10
+ Examples
11
+
12
+ * See a [Rails 2.3 example app](http://github.com/AtelierConvivialite/rails_example_app) using it.
13
+ * All the necessary changes have been made in [this commit](http://github.com/AtelierConvivialite/rails_example_app/commit/d88e4aa62b061f215d1784d76c94cd8613efc9bb)
14
+
10
15
  ## First steps
11
16
 
12
17
  * For each environment you want to use the gem, add to your config/environment/development.rb:
data/history.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## Version 1.4.3 / 2010-01-09
2
+
3
+ * Remove colour outputs as it increases code complexity and doesn't add any value.
4
+ * Rack middleware now write to the application’s log file instead of just puts-ing
5
+ * Better error messages for misconfigured projects
6
+
1
7
  ## Version 1.4.2 / 2010-01-07
2
8
 
3
9
  * Bug fix for `rake trans:config` which was not installing the translation.yml file properly.
@@ -8,7 +8,10 @@ module WebTranslateIt
8
8
  config = Configuration.new
9
9
  locale = I18n.locale.to_s
10
10
  return if config.ignore_locales.include?(locale)
11
- puts "Looking for #{locale} translations"
12
- config.files.each {|file| puts "Done. Response: #{file.fetch(locale)}" }
11
+ config.logger.debug { "Fetching #{locale} translations to Web Translate It" } if config.logger
12
+ config.files.each do |file|
13
+ response = file.fetch(locale)
14
+ config.logger { "Web Translate It response: #{response}" } if config.logger
15
+ end
13
16
  end
14
17
  end
@@ -6,7 +6,7 @@ module WebTranslateIt
6
6
 
7
7
  def call(env)
8
8
  # Update language files
9
- fetch_translations
9
+ WebTranslateIt::fetch_translations
10
10
  status, headers, response = @app.call(env)
11
11
  [status, headers, response.body]
12
12
  end
@@ -2,27 +2,32 @@ module WebTranslateIt
2
2
  class Configuration
3
3
  require 'yaml'
4
4
  require 'fileutils'
5
- attr_accessor :api_key, :files, :ignore_locales
5
+ attr_accessor :api_key, :files, :ignore_locales, :logger
6
6
 
7
7
  def initialize
8
8
  file = File.join(RAILS_ROOT, 'config', 'translation.yml')
9
9
  configuration = YAML.load_file(file)
10
+ self.logger = logger
10
11
  self.api_key = configuration['api_key']
11
12
  self.files = []
12
- self.ignore_locales = configuration['ignore_locales'].to_a.map{ |l| l.to_s }
13
+ self.ignore_locales = configuration['ignore_locales'].to_a.map{ |locale| locale.to_s }
13
14
  configuration['files'].each do |file_id, file_path|
14
15
  self.files.push(TranslationFile.new(file_id, file_path, api_key))
15
16
  end
16
17
  end
17
18
 
18
19
  def locales
19
- http = Net::HTTP.new('webtranslateit.com', 443)
20
- http.use_ssl = true
21
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
22
- http.read_timeout = 10
23
- request = Net::HTTP::Get.new("/api/projects/#{api_key}/locales")
24
- response = http.request(request)
25
- response.body.split
20
+ WebTranslateIt::Util.http_connection do |http|
21
+ request = Net::HTTP::Get.new(api_url)
22
+ response = http.request(request)
23
+ if response.code.to_i >= 400 and response.code.to_i < 500
24
+ puts "----------------------------------------------------------------------"
25
+ puts "You API key seems to be misconfigured. It is currently “self.api_key”."
26
+ puts "Change it in RAILS_ROOT/configuration/translation.yml."
27
+ else
28
+ response.body.split
29
+ end
30
+ end
26
31
  end
27
32
 
28
33
  def self.create_config_file
@@ -32,5 +37,17 @@ module WebTranslateIt
32
37
  FileUtils.copy File.join(File.dirname(__FILE__), '..', '..', 'examples', 'translation.yml'), config_file
33
38
  end
34
39
  end
40
+
41
+ def api_url
42
+ "/api/projects/#{api_key}/locales"
43
+ end
44
+
45
+ def logger
46
+ if defined?(Rails.logger)
47
+ Rails.logger
48
+ elsif defined?(RAILS_DEFAULT_LOGGER)
49
+ RAILS_DEFAULT_LOGGER
50
+ end
51
+ end
35
52
  end
36
53
  end
@@ -2,9 +2,9 @@ require File.join(File.dirname(__FILE__), '..', 'web_translate_it')
2
2
 
3
3
  namespace :trans do
4
4
  desc "Fetch translation files from Web Translate It"
5
- task :fetch, :locale do |t, args|
5
+ task :fetch, :locale do |task, args|
6
6
  welcome_message
7
- colour_puts "<b>Fetching file for locale #{args.locale}…</b>"
7
+ puts "Fetching file for locale #{args.locale}"
8
8
  configuration = WebTranslateIt::Configuration.new
9
9
  configuration.files.each do |file|
10
10
  response_code = file.fetch(args.locale)
@@ -21,7 +21,7 @@ namespace :trans do
21
21
  configuration.ignore_locales.each do |ignore|
22
22
  locales.delete(ignore)
23
23
  end
24
- colour_puts "<b>Fetching all files for all locales…</b>"
24
+ puts "Fetching all files for all locales"
25
25
  locales.each do |locale|
26
26
  configuration.files.each do |file|
27
27
  response_code = file.fetch(locale)
@@ -32,9 +32,9 @@ namespace :trans do
32
32
  end
33
33
 
34
34
  desc "Upload a translation file to Web Translate It"
35
- task :upload, :locale do |t, args|
35
+ task :upload, :locale do |task, args|
36
36
  welcome_message
37
- colour_puts "<b>Uploading file for locale #{args.locale}…</b>"
37
+ puts "Uploading file for locale #{args.locale}"
38
38
  configuration = WebTranslateIt::Configuration.new
39
39
  configuration.files.each do |file|
40
40
  response_code = file.upload(args.locale)
@@ -50,24 +50,13 @@ namespace :trans do
50
50
 
51
51
  def handle_response(file_path, response_code)
52
52
  if response_code < 400
53
- colour_puts "<green>#{file_path}: #{response_code}, OK</green>"
53
+ puts "#{file_path}: #{response_code}, OK"
54
54
  else
55
- colour_puts "<red>#{file_path}: #{response_code}, Problem!</red>"
55
+ puts "#{file_path}: #{response_code}, Problem!"
56
56
  end
57
57
  end
58
58
 
59
59
  def welcome_message
60
- colour_puts WELCOME_SCREEN
60
+ puts "Web Translate It v#{WebTranslateIt::Util.version}"
61
61
  end
62
-
63
- def colour_puts(text)
64
- puts WebTranslateIt::Util.subs_colour(text)
65
- end
66
-
67
- private
68
-
69
- WELCOME_SCREEN = <<-EO_WELCOME
70
- <banner>Web Translate It v#{WebTranslateIt::Util.version}</banner>
71
-
72
- EO_WELCOME
73
62
  end
@@ -13,19 +13,18 @@ module WebTranslateIt
13
13
  end
14
14
 
15
15
  def fetch(locale, force = false)
16
- http_connection do |http|
16
+ WebTranslateIt::Util.http_connection do |http|
17
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
18
+ request.add_field('If-Modified-Since', last_modification(file_path)) if File.exist?(file_path) and !force
19
+ response = http.request(request)
20
+ File.open(file_path_for_locale(locale), 'w'){ |file| file << response.body } if response.code.to_i == 200 and !response.body == ''
21
+ response.code.to_i
23
22
  end
24
23
  end
25
24
 
26
25
  def upload(locale)
27
26
  File.open(file_path_for_locale(locale)) do |file|
28
- http_connection do |http|
27
+ WebTranslateIt::Util.http_connection do |http|
29
28
  request = Net::HTTP::Put::Multipart.new(api_url(locale), "file" => UploadIO.new(file, "text/plain", file.path))
30
29
  response = http.request(request)
31
30
  response.code.to_i
@@ -38,15 +37,11 @@ module WebTranslateIt
38
37
  end
39
38
 
40
39
  protected
41
-
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
40
 
41
+ def last_modification(file_path)
42
+ File.mtime(File.new(file_path, 'r')).rfc2822
43
+ end
44
+
50
45
  def api_url(locale)
51
46
  "/api/projects/#{api_key}/files/#{self.id}/locales/#{locale}"
52
47
  end
@@ -1,25 +1,17 @@
1
1
  module WebTranslateIt
2
2
  class Util
3
- DEFAULT_TERMINAL_COLORS = "\e[0m\e[37m\e[40m"
4
- MONOCHROME_OUTPUT = "\\1"
5
-
6
3
  def self.version
7
4
  hash = YAML.load_file File.join(File.dirname(__FILE__), '..', '..' '/version.yml')
8
5
  [hash[:major], hash[:minor], hash[:patch]].join('.')
9
6
  end
10
7
 
11
- def self.colourise_output?
12
- @colourise_output = !!(RUBY_PLATFORM !~ /mswin/ || defined?(Win32::Console::ANSI)) if @colourise_output.nil?
13
- @colourise_output
14
- end
15
-
16
- def self.subs_colour(data)
17
- data = data.gsub(%r{<b>(.*?)</b>}m, colourise_output? ? "\e[1m\\1#{DEFAULT_TERMINAL_COLORS}" : MONOCHROME_OUTPUT)
18
- data.gsub!(%r{<red>(.*?)</red>}m, colourise_output? ? "\e[1m\e[31m\\1#{DEFAULT_TERMINAL_COLORS}" : MONOCHROME_OUTPUT)
19
- data.gsub!(%r{<green>(.*?)</green>}m, colourise_output? ? "\e[1m\e[32m\\1#{DEFAULT_TERMINAL_COLORS}" : MONOCHROME_OUTPUT)
20
- data.gsub!(%r{<yellow>(.*?)</yellow>}m, colourise_output? ? "\e[1m\e[33m\\1#{DEFAULT_TERMINAL_COLORS}" : MONOCHROME_OUTPUT)
21
- data.gsub!(%r{<banner>(.*?)</banner>}m, colourise_output? ? "\e[33m\e[44m\e[1m\\1#{DEFAULT_TERMINAL_COLORS}" : MONOCHROME_OUTPUT)
22
- data
8
+ def self.http_connection
9
+ http = Net::HTTP.new('webtranslateit.com', 443)
10
+ http.use_ssl = true
11
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
12
+ http.read_timeout = 10
13
+ yield http
23
14
  end
15
+
24
16
  end
25
17
  end
data/version.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
3
  :minor: 4
4
- :patch: 2
4
+ :patch: 3
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.2
4
+ version: 1.4.3
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-07 00:00:00 +01:00
12
+ date: 2010-01-09 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency