web_translate_it 1.4.4 → 1.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,17 +1,42 @@
1
1
  # Web Translate It
2
2
 
3
- [RDocs](http://yardoc.org/docs/AtelierConvivialite-webtranslateit) | [Metrics](http://getcaliper.com/caliper/project?repo=git%3A%2F%2Fgithub.com%2FAtelierConvivialite%2Fwebtranslateit.git) | [Tests](http://runcoderun.com/AtelierConvivialite/webtranslateit/builds/74a78c2b382cb1856fa0964ed4ad372b50872844/1/ruby_186) |
3
+ [Homepage](https://webtranslateit.com) |
4
+ [RDocs](http://yardoc.org/docs/AtelierConvivialite-webtranslateit) |
5
+ [Metrics](http://getcaliper.com/caliper/project?repo=git%3A%2F%2Fgithub.com%2FAtelierConvivialite%2Fwebtranslateit.git) |
6
+ [Tests](http://runcoderun.com/AtelierConvivialite/webtranslateit/builds/74a78c2b382cb1856fa0964ed4ad372b50872844/1/ruby_186) |
4
7
  [Example app](http://github.com/AtelierConvivialite/rails_example_app)
5
8
 
6
- This is a gem providing tools to integrate your app with [Web Translate It](https://webtranslateit.com).
9
+ This is a gem providing tools to integrate your software to translate with [Web Translate It](https://webtranslateit.com), a web-based translation hub.
7
10
 
8
11
  This gem provides your app with:
9
12
 
10
- * a handful of rake task to fetch your translations.
13
+ * an executable, `wti`, to upload and download language files from the command line (or in whatever else you want to execute it)
14
+ * a handful of rake task to fetch and upload your translations.
11
15
  * a rack middleware to automatically fetch new translations from Web Translate It.
12
16
 
13
17
  ## Installation
14
18
 
19
+ gem install web_translate_it
20
+
21
+ That’s it! At this point you have the Web Translate It executable. Run `wti --help` to see the usage:
22
+
23
+ Web Translate It Help:
24
+ **********************
25
+ -f --fetch [locale] Download all the files for your project.
26
+ If a locale is specified, only download
27
+ the file for that locale.
28
+ -u --upload [locale] Upload your files for a locale.
29
+ -v --version Show version.
30
+ -h --help This page.
31
+
32
+ ### Assumptions
33
+
34
+ We assume you have a `config/translation.yml` file in your project containing the configuration to sync with Web Translate It. [Read about the configuration file in the wiki](http://wiki.github.com/AtelierConvivialite/webtranslateit/).
35
+
36
+ ## Specific tools for Ruby on Rails
37
+
38
+ This gem includes some rake tasks and a rack middleware to integrate Web Translate It with Ruby on Rails.
39
+
15
40
  * Add to your config/environments.rb:
16
41
 
17
42
  `config.gem 'web_translate_it'`
@@ -19,19 +44,19 @@ This gem provides your app with:
19
44
  * Then, run:
20
45
 
21
46
  `rake gems:install`
22
-
47
+
23
48
  * Copy/paste your api key from Web Translate It and run:
24
49
 
25
50
  `script/generate webtranslateit --api-key your_key_here`
26
51
 
27
52
  The generator does two things:
28
53
 
29
- * It adds a auto-configured `config/translation.yml` file using Web Translate It’s API.
30
- * It adds `require 'web_translate_it/tasks' rescue LoadError` to your `Rakefile`
54
+ - It adds a auto-configured `config/translation.yml` file using Web Translate It’s API.
55
+ - It adds `require 'web_translate_it/tasks' rescue LoadError` to your `Rakefile`
31
56
 
32
- ## Rake tasks provided
57
+ ### Rake tasks provided
33
58
 
34
- The gem provides 3 rake tasks.
59
+ There are 3 rake tasks.
35
60
 
36
61
  rake trans:fetch:all
37
62
 
@@ -45,49 +70,8 @@ Fetch the latest translations for all the languages defined in Web Translate It
45
70
 
46
71
  Upload to Web Translate It your files in a specific locale defined in Web Translate It’s interface.
47
72
 
48
- ## Optional: automatically fetch new language files for each page request
49
-
50
- This should not be used on production. This allows to translate text on Web Translate It, reload a page on your site and see the translations appear.
51
-
52
- ### Rails 2.3 and newer
53
-
54
- Use the rack middleware!
55
-
56
- * Before starting up anything, you need to have a rack middleware setup to assign the value of the current locale to `I18n.locale`.
57
- This is very much specific to your app, this is left as an exercise to the reader. You can inspire yourself from Ryan Tomakyo’s [locale.rb](http://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/locale.rb).
58
- You can also find an example of a very simple middleware using the `locale` parameter in [examples/locale.rb](http://github.com/AtelierConvivialite/webtranslateit/blob/master/examples/locale.rb).
59
-
60
- * The next step is to setup the `autofetch` middleware. Add in `config/environments/development.rb` and any other environments you want to autofetch this line:
61
-
62
- config.middleware.use "WebTranslateIt::AutoFetch"
63
-
64
- * Restart your application, load a page. You should see this in the logs:
65
-
66
- Looking for fr_FR translations...
67
- Done. Response code: 200
68
-
69
- * That’s it!
70
-
71
- ### Rails older than 2.3 (works also for 2.3 and newer)
72
-
73
- * Add the following lines in your `ApplicationController`:
74
-
75
- <pre>before_filter :update_locale
76
-
77
- def update_locale
78
- begin
79
- WebTranslateIt.fetch_translations
80
- rescue Exception => e
81
- puts "** Web Translate It raised an exception: " + e.message
82
- end
83
- end</pre>
84
-
85
- * Restart your application for the changes to take effect. You should see something like this in the logs:
86
-
87
- Looking for fr translations...
88
- Done. Response code: 304
73
+ Read more about "Rails integration in the wiki":http://wiki.github.com/AtelierConvivialite/webtranslateit/.
89
74
 
90
- * That’s it!
91
75
 
92
76
  ## Supported Rails Versions
93
77
 
@@ -100,7 +84,7 @@ Please open a discussion on [our support site](http://help.webtranslateit.com) i
100
84
 
101
85
  # What is Web Translate It anyway?
102
86
 
103
- Web Translate It is an easy to use web-based translation hub to collaboratively translate software.
87
+ Web Translate It is a web-based translation hub to collaboratively translate software.
104
88
 
105
89
  To learn more about it, please visit our [tour page](https://webtranslateit.com/tour).
106
90
 
data/bin/wti ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'web_translate_it'
4
+ # require 'lib/web_translate_it' # for testing
5
+ WebTranslateIt::CommandLine.run
data/history.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## Edge
2
+
3
+ * Improved documentation
4
+ * Web Translate is now an executable: `wti`
5
+
1
6
  ## Version 1.4.4 / 2010-02-02
2
7
 
3
8
  * Add generator to automatically configure your project, given a Web Translate It API key.
@@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), 'web_translate_it', 'util')
2
2
  require File.join(File.dirname(__FILE__), 'web_translate_it', 'configuration')
3
3
  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
+ require File.join(File.dirname(__FILE__), 'web_translate_it', 'command_line')
5
6
 
6
7
  module WebTranslateIt
7
8
  def self.fetch_translations
@@ -1,11 +1,19 @@
1
1
  module WebTranslateIt
2
+
3
+ # Class to automatically fetch the last translations from Web Translate It
4
+ # for every page requested.
5
+ # This can be used as a rack middleware.
6
+ # Implementation example:
7
+ #
8
+ # # in config/environment.rb:
9
+ # config.middleware.use "WebTranslateIt::AutoFetch"
10
+ #
2
11
  class AutoFetch
3
12
  def initialize(app)
4
13
  @app = app
5
14
  end
6
15
 
7
16
  def call(env)
8
- # Update language files
9
17
  WebTranslateIt::fetch_translations
10
18
  status, headers, response = @app.call(env)
11
19
  [status, headers, response.body]
@@ -0,0 +1,71 @@
1
+ module WebTranslateIt
2
+ class CommandLine
3
+
4
+ OPTIONS = <<-OPTION
5
+ -f --fetch [locale] Download all the files for your project.
6
+ If a locale is specified, only download
7
+ the file for that locale.
8
+ -u --upload [locale] Upload your files for a locale.
9
+ -v --version Show version.
10
+ -h --help This page.
11
+ OPTION
12
+
13
+ def self.run
14
+ case ARGV[0]
15
+ when '-f', '--fetch'
16
+ fetch
17
+ when '-u', '--upload'
18
+ upload
19
+ when '-v', '--version'
20
+ show_version
21
+ when '-h', '--help'
22
+ show_options
23
+ else
24
+ puts "Command not found"
25
+ show_options
26
+ end
27
+ end
28
+
29
+ def self.fetch
30
+ #TODO: Ability to pass a path as an argument
31
+ configuration = WebTranslateIt::Configuration.new('.')
32
+ if ARGV.size == 2
33
+ locales = [ARGV[1]]
34
+ elsif ARGV.size == 1
35
+ locales = configuration.locales
36
+ end
37
+ configuration.files.each do |file|
38
+ locales.each do |locale|
39
+ file.fetch(locale)
40
+ end
41
+ end
42
+ end
43
+
44
+ def self.upload
45
+ #TODO: Ability to pass a path as an argument
46
+ configuration = WebTranslateIt::Configuration.new('.')
47
+ if ARGV.size == 2
48
+ locales = [ARGV[1]]
49
+ elsif ARGV.size == 1
50
+ locales = configuration.locales
51
+ end
52
+ configuration.files.each do |file|
53
+ locales.each do |locale|
54
+ file.upload(locale)
55
+ end
56
+ end
57
+ end
58
+
59
+ def self.show_options
60
+ puts ""
61
+ puts "Web Translate It Help:"
62
+ puts "**********************"
63
+ $stdout.puts OPTIONS
64
+ end
65
+
66
+ def self.show_version
67
+ puts ""
68
+ puts "Web Translate It #{WebTranslateIt::Util.version}"
69
+ end
70
+ end
71
+ end
@@ -1,28 +1,43 @@
1
1
  module WebTranslateIt
2
+
3
+ # Handles the configuration of your project, both via the the configuration file
4
+ # and via the API.
5
+ # Implementation example, assuming you have a valid config/translation.yml file:
6
+ #
7
+ # configuration = WebTranslateIt::Configuration.new
8
+ #
2
9
  class Configuration
3
10
  require 'yaml'
4
11
  require 'fileutils'
5
- attr_accessor :api_key, :files, :ignore_locales, :logger
12
+ attr_accessor :path, :api_key, :files, :ignore_locales, :logger
6
13
 
7
- def initialize
8
- file = File.join(RAILS_ROOT, 'config', 'translation.yml')
9
- configuration = YAML.load_file(file)
14
+ # Load the configuration file from the path.
15
+ def initialize(root_path=RAILS_ROOT, path_to_config="config/translation.yml")
16
+ self.path = root_path
17
+ configuration = YAML.load_file(File.join(root_path, path_to_config))
10
18
  self.logger = logger
11
19
  self.api_key = configuration['api_key']
12
20
  self.files = []
13
21
  self.ignore_locales = configuration['ignore_locales'].to_a.map{ |locale| locale.to_s }
14
22
  configuration['files'].each do |file_id, file_path|
15
- self.files.push(TranslationFile.new(file_id, file_path, api_key))
23
+ self.files.push(TranslationFile.new(file_id, root_path + '/' + file_path, api_key))
16
24
  end
17
25
  end
18
26
 
27
+ # Makes an API request to fetch the list of the different locales for a project.
28
+ # Implementation example:
29
+ #
30
+ # configuration = WebTranslateIt::Configuration.new
31
+ # locales = configuration.locales # returns an array of locales: ['en', 'fr', 'es', ...]
32
+ #
33
+ # TODO: Make this use the new endpoint serving YAML
19
34
  def locales
20
35
  WebTranslateIt::Util.http_connection do |http|
21
36
  request = Net::HTTP::Get.new(api_url)
22
37
  response = http.request(request)
23
38
  if response.code.to_i >= 400 and response.code.to_i < 500
24
39
  puts "----------------------------------------------------------------------"
25
- puts "You API key seems to be misconfigured. It is currently self.api_key”."
40
+ puts "You API key seems to be misconfigured. It is currently #{self.api_key}."
26
41
  puts "Change it in RAILS_ROOT/configuration/translation.yml."
27
42
  else
28
43
  response.body.split
@@ -30,10 +45,12 @@ module WebTranslateIt
30
45
  end
31
46
  end
32
47
 
48
+ # Convenience method which returns the endpoint for fetching a list of locales for a project.
33
49
  def api_url
34
50
  "/api/projects/#{api_key}/locales"
35
51
  end
36
52
 
53
+ # Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.
37
54
  def logger
38
55
  if defined?(Rails.logger)
39
56
  Rails.logger
@@ -31,7 +31,7 @@ namespace :trans do
31
31
  end
32
32
  end
33
33
 
34
- desc "Upload a translation file to Web Translate It"
34
+ desc "Upload the translation files for a locale to Web Translate It"
35
35
  task :upload, :locale do |task, args|
36
36
  welcome_message
37
37
  puts "Uploading file for locale #{args.locale}…"
@@ -1,4 +1,11 @@
1
1
  module WebTranslateIt
2
+
3
+ # A TranslationFile is the representation of a master language file
4
+ # on Web Translate It.
5
+ #
6
+ # This class allows to manipulate TranslationFiles, more specifically upload and download them.
7
+ # If you pass a Locale to the master language file you will be able to
8
+ # manipulate a _target_ language file.
2
9
  class TranslationFile
3
10
  require 'net/https'
4
11
  require 'net/http/post/multipart'
@@ -12,16 +19,43 @@ module WebTranslateIt
12
19
  self.api_key = api_key
13
20
  end
14
21
 
22
+ # Fetch a language file.
23
+ # By default it will make a conditional GET Request, using the `If-Modified-Since` tag.
24
+ # You can force the method to re-download your file by passing `true` as a second argument
25
+ #
26
+ # Example of implementation:
27
+ #
28
+ # configuration = WebTranslateIt::Configuration.new
29
+ # locale = configuration.locales.first
30
+ # file = configuration.files.first
31
+ # file.fetch(locale) # the first time, will return the content of the language file with a status 200 OK
32
+ # file.fetch(locale) # returns nothing, with a status 304 Not Modified
33
+ # file.fetch(locale, true) # force to re-download the file, will return the content of the file with a 200 OK
34
+ #
15
35
  def fetch(locale, force = false)
16
36
  WebTranslateIt::Util.http_connection do |http|
17
37
  request = Net::HTTP::Get.new(api_url(locale))
18
38
  request.add_field('If-Modified-Since', last_modification(file_path)) if File.exist?(file_path) and !force
19
39
  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 == ''
40
+ File.open(file_path_for_locale(locale), 'w'){ |file| file << response.body } if response.code.to_i == 200 and response.body != ''
21
41
  response.code.to_i
22
42
  end
23
43
  end
24
44
 
45
+ # Update a language file to Web Translate It by performing a PUT Request.
46
+ # Note that it is currently not possible to POST a new language file at the moment.
47
+ #
48
+ # Example of implementation:
49
+ #
50
+ # configuration = WebTranslateIt::Configuration.new
51
+ # locale = configuration.locales.first
52
+ # file = configuration.files.first
53
+ # file.upload(locale) # should respond the HTTP code 202 Accepted
54
+ #
55
+ # The meaning of the HTTP 202 code is: the request has been accepted for processing, but the processing has not
56
+ # been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing
57
+ # actually takes place.
58
+ # This is due to the fact that language file imports are handled by background processing.
25
59
  def upload(locale)
26
60
  File.open(file_path_for_locale(locale)) do |file|
27
61
  WebTranslateIt::Util.http_connection do |http|
@@ -32,16 +66,19 @@ module WebTranslateIt
32
66
  end
33
67
  end
34
68
 
69
+ # Convenience method which returns the file path of a TranslationFile for a given locale.
35
70
  def file_path_for_locale(locale)
36
71
  self.file_path.gsub("[locale]", locale)
37
72
  end
38
73
 
39
74
  protected
40
75
 
76
+ # Convenience method which returns the date of last modification of a language file.
41
77
  def last_modification(file_path)
42
78
  File.mtime(File.new(file_path, 'r')).rfc2822
43
79
  end
44
-
80
+
81
+ # Convenience method which returns the URL of the API endpoint for a locale.
45
82
  def api_url(locale)
46
83
  "/api/projects/#{api_key}/files/#{self.id}/locales/#{locale}"
47
84
  end
@@ -1,10 +1,24 @@
1
1
  module WebTranslateIt
2
- class Util
2
+
3
+ # A few useful functions
4
+ class Util
5
+
6
+ # Return a string representing the gem version
7
+ # For example "1.4.4"
3
8
  def self.version
4
9
  hash = YAML.load_file File.join(File.dirname(__FILE__), '..', '..' '/version.yml')
5
10
  [hash[:major], hash[:minor], hash[:patch]].join('.')
6
11
  end
7
12
 
13
+ # Yields a HTTP connection over SSL to Web Translate It.
14
+ # This is used for the connections to the API throughout the library.
15
+ # Use it like so:
16
+ #
17
+ # WebTranslateIt::Util.http_connection do |http|
18
+ # request = Net::HTTP::Get.new(api_url)
19
+ # response = http.request(request)
20
+ # end
21
+ #
8
22
  def self.http_connection
9
23
  http = Net::HTTP.new('webtranslateit.com', 443)
10
24
  http.use_ssl = true
@@ -12,6 +26,5 @@ module WebTranslateIt
12
26
  http.read_timeout = 10
13
27
  yield http
14
28
  end
15
-
16
29
  end
17
30
  end
data/version.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
3
  :minor: 4
4
- :patch: 4
4
+ :patch: 5
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
4
+ version: 1.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - "\xC3\x89douard Bri\xC3\xA8re"
@@ -9,8 +9,8 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-02 00:00:00 +01:00
13
- default_executable:
12
+ date: 2010-02-03 00:00:00 +01:00
13
+ default_executable: wti
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multipart-post
@@ -44,12 +44,13 @@ dependencies:
44
44
  version:
45
45
  description: A rack middleware and a handful of rake tasks to sync your translations between webtranslateit.com and your rails applications.
46
46
  email: edouard@atelierconvivialite.com
47
- executables: []
48
-
47
+ executables:
48
+ - wti
49
49
  extensions: []
50
50
 
51
- extra_rdoc_files: []
52
-
51
+ extra_rdoc_files:
52
+ - history.md
53
+ - README.md
53
54
  files:
54
55
  - history.md
55
56
  - MIT-LICENSE
@@ -58,6 +59,7 @@ files:
58
59
  - examples/locale.rb
59
60
  - examples/translation.yml
60
61
  - lib/web_translate_it/auto_fetch.rb
62
+ - lib/web_translate_it/command_line.rb
61
63
  - lib/web_translate_it/configuration.rb
62
64
  - lib/web_translate_it/tasks.rb
63
65
  - lib/web_translate_it/translation_file.rb
@@ -66,13 +68,15 @@ files:
66
68
  - generators/webtranslateit/lib/insert_commands.rb
67
69
  - generators/webtranslateit/templates/translation.yml
68
70
  - generators/webtranslateit/webtranslateit_generator.rb
71
+ - bin/wti
69
72
  has_rdoc: true
70
73
  homepage: https://webtranslateit.com
71
74
  licenses: []
72
75
 
73
76
  post_install_message:
74
- rdoc_options: []
75
-
77
+ rdoc_options:
78
+ - --main
79
+ - README.md
76
80
  require_paths:
77
81
  - lib
78
82
  required_ruby_version: !ruby/object:Gem::Requirement