web_translate_it 1.5.2 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +8 -8
- data/examples/locale.rb +2 -2
- data/examples/translation.yml +2 -14
- data/generators/webtranslateit/templates/translation.yml +2 -13
- data/history.md +10 -0
- data/lib/web_translate_it.rb +5 -4
- data/lib/web_translate_it/command_line.rb +26 -34
- data/lib/web_translate_it/configuration.rb +25 -12
- data/lib/web_translate_it/project.rb +2 -22
- data/lib/web_translate_it/tasks.rb +11 -27
- data/lib/web_translate_it/translation_file.rb +22 -27
- data/lib/web_translate_it/util.rb +18 -1
- data/spec/examples/config/translation.yml +4 -13
- data/spec/web_translate_it/configuration_spec.rb +44 -11
- data/spec/web_translate_it/translation_file_spec.rb +11 -21
- data/spec/web_translate_it_spec.rb +1 -1
- data/version.yml +2 -2
- metadata +3 -13
data/README.md
CHANGED
@@ -3,10 +3,10 @@
|
|
3
3
|
[Homepage](https://webtranslateit.com) |
|
4
4
|
[RDocs](http://yardoc.org/docs/AtelierConvivialite-webtranslateit) |
|
5
5
|
[Metrics](http://getcaliper.com/caliper/project?repo=git%3A%2F%2Fgithub.com%2FAtelierConvivialite%2Fwebtranslateit.git) |
|
6
|
-
[
|
6
|
+
[Bugs](http://github.com/AtelierConvivialite/webtranslateit/issues) |
|
7
7
|
[Example app](http://github.com/AtelierConvivialite/rails_example_app)
|
8
8
|
|
9
|
-
This is a gem providing tools to
|
9
|
+
This is a gem providing tools to sync your software’s language files with [Web Translate It](https://webtranslateit.com), a web-based computer-aided translation tool.
|
10
10
|
|
11
11
|

|
12
12
|
|
@@ -20,8 +20,8 @@ This gem provides your app with:
|
|
20
20
|
|
21
21
|
gem install web_translate_it
|
22
22
|
|
23
|
-
|
24
|
-
If your project if already set up on Web Translate It,
|
23
|
+
At this point you should have the `wti` executable working.
|
24
|
+
If your project if already set up on Web Translate It, open a terminal and type `wti autoconf` to generate the configuration file.
|
25
25
|
|
26
26
|
Run `wti --help` to see the usage:
|
27
27
|
|
@@ -55,7 +55,7 @@ This gem includes some rake tasks and a rack middleware to integrate Web Transla
|
|
55
55
|
|
56
56
|
`rake gems:install`
|
57
57
|
|
58
|
-
* Copy/paste your
|
58
|
+
* Copy/paste your API key from Web Translate It and run:
|
59
59
|
|
60
60
|
`script/generate webtranslateit --api-key your_key_here`
|
61
61
|
|
@@ -94,12 +94,12 @@ Please open a discussion on [our support site](http://help.webtranslateit.com) i
|
|
94
94
|
|
95
95
|
# Acknowledgement
|
96
96
|
|
97
|
-
* The executable
|
98
|
-
* The Rails generator
|
97
|
+
* The executable’s commands are very much inspired from [Gemcutter](http://gemcutter.org/),
|
98
|
+
* The Rails generator was pinched from [Hoptoad Notifier](http://github.com/thoughtbot/hoptoad_notifier/).
|
99
99
|
|
100
100
|
# What is Web Translate It anyway?
|
101
101
|
|
102
|
-
Web Translate It is a web-based translation
|
102
|
+
Web Translate It is a web-based translation tool to collaboratively translate software.
|
103
103
|
|
104
104
|
To learn more about it, please visit our [tour page](https://webtranslateit.com/tour).
|
105
105
|
|
data/examples/locale.rb
CHANGED
@@ -5,8 +5,8 @@ class Locale
|
|
5
5
|
|
6
6
|
def call(env)
|
7
7
|
req = Rack::Request.new(env)
|
8
|
-
I18n.locale = req.params['locale']
|
8
|
+
I18n.locale = req.params['locale'] || :en
|
9
9
|
status, headers, response = @app.call(env)
|
10
|
-
[status, headers, response
|
10
|
+
[status, headers, response]
|
11
11
|
end
|
12
12
|
end
|
data/examples/translation.yml
CHANGED
@@ -1,18 +1,6 @@
|
|
1
1
|
# The Project API Token from Web Translate It
|
2
2
|
api_key: SECRET
|
3
3
|
|
4
|
-
#
|
5
|
-
# Pass an array of string, or an array of symbols, a string or a symbol.
|
4
|
+
# Optional: locales not to sync with Web Translate It.
|
6
5
|
# eg. [:en, :fr] or just 'en'
|
7
|
-
ignore_locales: :en
|
8
|
-
|
9
|
-
# A list of files to translate
|
10
|
-
# You can name your language files as you want, as long as the locale name match the
|
11
|
-
# locale name you set in Web Translate It, and that the different language files names are
|
12
|
-
# differenciated by their locale name.
|
13
|
-
# For example, if you set to translate a project in en_US in WTI, you should use the locale en_US in your app
|
14
|
-
#
|
15
|
-
# wti_id is the file id from Web Translate It.
|
16
|
-
files:
|
17
|
-
wti_id: config/locales/file1_[locale].yml
|
18
|
-
wti_id: config/locales/file2_[locale].yml
|
6
|
+
# ignore_locales: :en
|
@@ -1,17 +1,6 @@
|
|
1
1
|
# The Project API Token from Web Translate It
|
2
2
|
api_key: '<%= api_key %>'
|
3
3
|
|
4
|
-
#
|
5
|
-
# Pass an array of string, or an array of symbols, a string or a symbol.
|
4
|
+
# Optional: locales not to sync with Web Translate It.
|
6
5
|
# eg. [:en, :fr] or just 'en'
|
7
|
-
ignore_locales: '<%= project["source_locale"]["code"] %>'
|
8
|
-
|
9
|
-
# A list of files to translate
|
10
|
-
# You can name your language files as you want, as long as the locale name match the
|
11
|
-
# locale name you set in Web Translate It, and that the different language files names are
|
12
|
-
# differenciated by their locale name.
|
13
|
-
# For example, if you set to translate a project in en_US in WTI, you should use the locale en_US in your app
|
14
|
-
#
|
15
|
-
# wti_id is the file id from Web Translate It.
|
16
|
-
files:
|
17
|
-
<% project["project_files"].each do |project_file| -%><% if project_file["master"] -%> <%= project_file["id"] %>: config/locales/<%= project_file["name"].gsub(project["source_locale"]["code"], "[locale]") %><% end %><% end -%>
|
6
|
+
# ignore_locales: '<%= project["source_locale"]["code"] %>'
|
data/history.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
## Version 1.6.0 /2010-03-22
|
2
|
+
|
3
|
+
* Bug fix: Encoding problem with Ruby 1.9 (Romain Sempé)
|
4
|
+
* Bug fix: Make rake task create locale directory if it doesn’t exist yet. (Romain Sempé)
|
5
|
+
* Bug fix: Make the list of ignore locales work for wti pull (it won’t pull the locales you ignore)
|
6
|
+
|
7
|
+
* **Breaking update**: The project configuration changed and doesn’t include a list of master language files any longer.
|
8
|
+
This makes configuration much simpler. However you must configure the exact file path of your files in
|
9
|
+
the File Manager. This allows much more flexibility for the choice of your language file names. Ref #22.
|
10
|
+
|
1
11
|
## Version 1.5.2 /2010-03-13
|
2
12
|
|
3
13
|
* Only added a man page.
|
data/lib/web_translate_it.rb
CHANGED
@@ -6,14 +6,15 @@ require File.join(File.dirname(__FILE__), 'web_translate_it', 'command_line')
|
|
6
6
|
require File.join(File.dirname(__FILE__), 'web_translate_it', 'project')
|
7
7
|
|
8
8
|
module WebTranslateIt
|
9
|
+
|
9
10
|
def self.fetch_translations
|
10
11
|
config = Configuration.new
|
11
12
|
locale = I18n.locale.to_s
|
12
13
|
return if config.ignore_locales.include?(locale)
|
13
|
-
config.logger.debug { "Fetching #{locale}
|
14
|
-
config.files.each do |file|
|
15
|
-
response = file.fetch
|
16
|
-
config.logger { "Web Translate It response: #{response}" } if config.logger
|
14
|
+
config.logger.debug { "➔ Fetching #{locale.upcase} language file(s) from Web Translate It…" } if config.logger
|
15
|
+
config.files.find_all{ |file| file.locale == locale }.each do |file|
|
16
|
+
response = file.fetch
|
17
|
+
config.logger.debug { "➔ Web Translate It response: #{response}" } if config.logger
|
17
18
|
end
|
18
19
|
end
|
19
20
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
module WebTranslateIt
|
2
3
|
class CommandLine
|
3
4
|
require 'fileutils'
|
@@ -44,22 +45,20 @@ OPTION
|
|
44
45
|
|
45
46
|
def self.pull
|
46
47
|
configuration = fetch_configuration
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
puts
|
51
|
-
file.fetch(locale, ARGV.index('--force'))
|
48
|
+
fetch_locales_to_pull(configuration).each do |locale|
|
49
|
+
configuration.files.find_all{ |file| file.locale == locale }.each do |file|
|
50
|
+
puts "Pulling #{file.file_path}…"
|
51
|
+
puts file.fetch(ARGV.index('--force'))
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
def self.push
|
57
57
|
configuration = fetch_configuration
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
puts
|
62
|
-
file.upload(locale)
|
58
|
+
fetch_locales_to_push(configuration).each do |locale|
|
59
|
+
configuration.files.find_all{ |file| file.locale == locale }.each do |file|
|
60
|
+
puts "Pushing #{file.file_path}…"
|
61
|
+
puts file.upload
|
63
62
|
end
|
64
63
|
end
|
65
64
|
end
|
@@ -76,11 +75,19 @@ OPTION
|
|
76
75
|
path = STDIN.gets.strip
|
77
76
|
path = "config/translation.yml" if path == ""
|
78
77
|
FileUtils.mkpath(path.split('/')[0..path.split('/').size-1])
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
78
|
+
project = YAML.load WebTranslateIt::Project.fetch_info(api_key)
|
79
|
+
project_info = project['project']
|
80
|
+
File.open(path, 'w'){ |file| file << generate_configuration(api_key, project_info) }
|
81
|
+
project_info['project_files'].each do |file|
|
82
|
+
if !File.exists?(file['name'])
|
83
|
+
puts "Could not find file `#{file['name']}`."
|
84
|
+
puts "Please check the correct full path is specified in the File Manager"
|
85
|
+
puts "https://webtranslateit.com/projects/#{project_info['id']}/files"
|
86
|
+
else
|
87
|
+
puts "Found #{file['name']}."
|
88
|
+
end
|
89
|
+
end
|
90
|
+
puts ""
|
84
91
|
puts "Done! You can now use `wti` to push and pull your language files."
|
85
92
|
puts "Check `wti --help` for more information."
|
86
93
|
end
|
@@ -119,6 +126,7 @@ OPTION
|
|
119
126
|
def self.fetch_locales_to_pull(configuration)
|
120
127
|
if (index = ARGV.index('-l') || ARGV.index('--locale')).nil?
|
121
128
|
locales = configuration.target_locales
|
129
|
+
configuration.ignore_locales.each{ |locale_to_delete| locales.delete(locale_to_delete) }
|
122
130
|
else
|
123
131
|
locales = [ARGV[index+1]]
|
124
132
|
end
|
@@ -136,30 +144,14 @@ OPTION
|
|
136
144
|
return locales.uniq
|
137
145
|
end
|
138
146
|
|
139
|
-
def self.generate_configuration(api_key,
|
140
|
-
project_info = YAML.load WebTranslateIt::Project.fetch_info(api_key)
|
141
|
-
project = project_info['project']
|
147
|
+
def self.generate_configuration(api_key, project_info)
|
142
148
|
file = <<-FILE
|
143
149
|
api_key: #{api_key}
|
144
150
|
|
145
|
-
#
|
146
|
-
# Pass an array of string, or an array of symbols, a string or a symbol.
|
151
|
+
# Optional: locales not to sync with Web Translate It.
|
147
152
|
# eg. [:en, :fr] or just 'en'
|
148
|
-
ignore_locales: '#{
|
149
|
-
|
150
|
-
# A list of files to translate
|
151
|
-
# You can name your language files as you want, as long as the locale name match the
|
152
|
-
# locale name you set in Web Translate It, and that the different language files names are
|
153
|
-
# differenciated by their locale name.
|
154
|
-
# For example, if you set to translate a project in en_US in WTI, you should use the locale en_US in your app
|
155
|
-
#
|
156
|
-
files:
|
153
|
+
# ignore_locales: '#{project_info["source_locale"]["code"]}'
|
157
154
|
FILE
|
158
|
-
project["project_files"].each do |project_file|
|
159
|
-
if project_file["master"]
|
160
|
-
file << " #{project_file["id"]}: #{path_to_locale_files}" + project_file["name"].gsub(project["source_locale"]["code"], "[locale]") + "\n"
|
161
|
-
end
|
162
|
-
end
|
163
155
|
return file
|
164
156
|
end
|
165
157
|
end
|
@@ -12,31 +12,44 @@ module WebTranslateIt
|
|
12
12
|
attr_accessor :path, :api_key, :source_locale, :target_locales, :files, :ignore_locales, :logger
|
13
13
|
|
14
14
|
# Load configuration file from the path.
|
15
|
-
def initialize(root_path=
|
15
|
+
def initialize(root_path = Rails.root, path_to_config_file = "config/translation.yml")
|
16
16
|
self.path = root_path
|
17
|
-
configuration = YAML.load_file(File.join(root_path, path_to_config))
|
18
17
|
self.logger = logger
|
18
|
+
configuration = YAML.load_file(File.join(self.path, path_to_config_file))
|
19
19
|
self.api_key = configuration['api_key']
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
set_locales
|
20
|
+
project_info = YAML.load WebTranslateIt::Project.fetch_info(api_key)
|
21
|
+
set_locales_to_ignore(configuration)
|
22
|
+
set_files(project_info['project'])
|
23
|
+
set_locales(project_info['project'])
|
26
24
|
end
|
27
25
|
|
28
|
-
#
|
26
|
+
# Set the project locales from the Project API.
|
29
27
|
# Implementation example:
|
30
28
|
#
|
31
29
|
# configuration = WebTranslateIt::Configuration.new
|
32
30
|
# locales = configuration.locales # returns an array of locales: ['en', 'fr', 'es', ...]
|
33
|
-
def set_locales
|
34
|
-
project_info = YAML.load WebTranslateIt::Project.fetch_info(api_key)
|
35
|
-
project = project_info['project']
|
31
|
+
def set_locales(project)
|
36
32
|
self.source_locale = project['source_locale']['code']
|
37
33
|
self.target_locales = project['target_locales'].map{|locale| locale['code']}
|
38
34
|
end
|
39
35
|
|
36
|
+
# Set the project files from the Project API.
|
37
|
+
# Implementation example:
|
38
|
+
#
|
39
|
+
# configuration = WebTranslateIt::Configuration.new
|
40
|
+
# files = configuration.files # returns an array of TranslationFile
|
41
|
+
def set_files(project)
|
42
|
+
self.files = []
|
43
|
+
project['project_files'].each do |project_file|
|
44
|
+
self.files.push TranslationFile.new(project_file['id'], project_file['name'], project_file['locale_code'], self.api_key)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Set locales to ignore from the configuration file, if set.
|
49
|
+
def set_locales_to_ignore(configuration)
|
50
|
+
self.ignore_locales = Array(configuration['ignore_locales']).map{ |locale| locale.to_s }
|
51
|
+
end
|
52
|
+
|
40
53
|
# Convenience method which returns the endpoint for fetching a list of locales for a project.
|
41
54
|
def api_url
|
42
55
|
"/api/projects/#{api_key}.yaml"
|
@@ -4,34 +4,14 @@ module WebTranslateIt
|
|
4
4
|
def self.fetch_info(api_key)
|
5
5
|
WebTranslateIt::Util.http_connection do |http|
|
6
6
|
request = Net::HTTP::Get.new("/api/projects/#{api_key}.yaml")
|
7
|
-
|
8
|
-
if response.code.to_i >= 400 and response.code.to_i < 500
|
9
|
-
puts "We had a problem connecting to Web Translate It with this API key."
|
10
|
-
puts "Make sure it is correct."
|
11
|
-
exit
|
12
|
-
elsif response.code.to_i >= 500
|
13
|
-
puts "Web Translate It is temporarily unavailable. Please try again shortly."
|
14
|
-
exit
|
15
|
-
else
|
16
|
-
return response.body
|
17
|
-
end
|
7
|
+
Util.handle_response(http.request(request), true)
|
18
8
|
end
|
19
9
|
end
|
20
10
|
|
21
11
|
def self.fetch_stats(api_key)
|
22
12
|
WebTranslateIt::Util.http_connection do |http|
|
23
13
|
request = Net::HTTP::Get.new("/api/projects/#{api_key}/stats.yaml")
|
24
|
-
|
25
|
-
if response.code.to_i >= 400 and response.code.to_i < 500
|
26
|
-
puts "We had a problem connecting to Web Translate It with this API key."
|
27
|
-
puts "Make sure it is correct."
|
28
|
-
exit
|
29
|
-
elsif response.code.to_i >= 500
|
30
|
-
puts "Web Translate It is temporarily unavailable. Please try again shortly."
|
31
|
-
exit
|
32
|
-
else
|
33
|
-
return response.body
|
34
|
-
end
|
14
|
+
Util.handle_response(http.request(request), true)
|
35
15
|
end
|
36
16
|
end
|
37
17
|
end
|
@@ -1,31 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require File.join(File.dirname(__FILE__), '..', 'web_translate_it')
|
2
3
|
|
3
4
|
namespace :trans do
|
4
5
|
desc "Fetch translation files from Web Translate It"
|
5
6
|
task :fetch, :locale do |task, args|
|
6
|
-
welcome_message
|
7
|
+
WebTranslateIt::Util.welcome_message
|
7
8
|
puts "Fetching file for locale #{args.locale}…"
|
8
9
|
configuration = WebTranslateIt::Configuration.new
|
9
|
-
configuration.files.each do |file|
|
10
|
-
|
11
|
-
handle_response(file.file_path_for_locale(args.locale), response_code)
|
10
|
+
configuration.files.find_all{ |file| file.locale == args.locale }.each do |file|
|
11
|
+
puts file.file_path + ": " + file.fetch
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
namespace :fetch do
|
16
16
|
desc "Fetch all the translation files from Web Translate It"
|
17
17
|
task :all do
|
18
|
-
welcome_message
|
18
|
+
WebTranslateIt::Util.welcome_message
|
19
19
|
configuration = WebTranslateIt::Configuration.new
|
20
20
|
locales = configuration.target_locales
|
21
|
-
configuration.ignore_locales.each
|
22
|
-
locales.delete(ignore)
|
23
|
-
end
|
21
|
+
configuration.ignore_locales.each{ |locale_to_ignore| locales.delete(locale_to_ignore) }
|
24
22
|
puts "Fetching all files for all locales…"
|
25
23
|
locales.each do |locale|
|
26
|
-
configuration.files.each do |file|
|
27
|
-
|
28
|
-
handle_response(file.file_path_for_locale(locale), response_code)
|
24
|
+
configuration.files.find_all{ |file| file.locale == locale }.each do |file|
|
25
|
+
puts file.file_path + " " + file.fetch
|
29
26
|
end
|
30
27
|
end
|
31
28
|
end
|
@@ -33,24 +30,11 @@ namespace :trans do
|
|
33
30
|
|
34
31
|
desc "Upload the translation files for a locale to Web Translate It"
|
35
32
|
task :upload, :locale do |task, args|
|
36
|
-
welcome_message
|
33
|
+
WebTranslateIt::Util.welcome_message
|
37
34
|
puts "Uploading file for locale #{args.locale}…"
|
38
35
|
configuration = WebTranslateIt::Configuration.new
|
39
|
-
configuration.files.each do |file|
|
40
|
-
|
41
|
-
handle_response(file.file_path_for_locale(args.locale), response_code)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def handle_response(file_path, response_code)
|
46
|
-
if response_code < 400
|
47
|
-
puts "#{file_path}: #{response_code}, OK"
|
48
|
-
else
|
49
|
-
puts "#{file_path}: #{response_code}, Problem!"
|
36
|
+
configuration.files.find_all{ |file| file.locale == args.locale }.each do |file|
|
37
|
+
puts file.file_path + " " + file.upload
|
50
38
|
end
|
51
39
|
end
|
52
|
-
|
53
|
-
def welcome_message
|
54
|
-
puts "Web Translate It v#{WebTranslateIt::Util.version}"
|
55
|
-
end
|
56
40
|
end
|
@@ -11,11 +11,12 @@ module WebTranslateIt
|
|
11
11
|
require 'net/http/post/multipart'
|
12
12
|
require 'time'
|
13
13
|
|
14
|
-
attr_accessor :id, :file_path, :api_key
|
14
|
+
attr_accessor :id, :file_path, :locale, :api_key
|
15
15
|
|
16
|
-
def initialize(id, file_path, api_key)
|
16
|
+
def initialize(id, file_path, locale, api_key)
|
17
17
|
self.id = id
|
18
18
|
self.file_path = file_path
|
19
|
+
self.locale = locale
|
19
20
|
self.api_key = api_key
|
20
21
|
end
|
21
22
|
|
@@ -26,19 +27,19 @@ module WebTranslateIt
|
|
26
27
|
# Example of implementation:
|
27
28
|
#
|
28
29
|
# configuration = WebTranslateIt::Configuration.new
|
29
|
-
# locale = configuration.locales.first
|
30
30
|
# file = configuration.files.first
|
31
|
-
# file.fetch
|
32
|
-
# file.fetch
|
33
|
-
# file.fetch(
|
31
|
+
# file.fetch # the first time, will return the content of the language file with a status 200 OK
|
32
|
+
# file.fetch # returns nothing, with a status 304 Not Modified
|
33
|
+
# file.fetch(true) # force to re-download the file, will return the content of the file with a 200 OK
|
34
34
|
#
|
35
|
-
def fetch(
|
35
|
+
def fetch(force = false)
|
36
36
|
WebTranslateIt::Util.http_connection do |http|
|
37
|
-
request = Net::HTTP::Get.new(api_url
|
38
|
-
request.add_field('If-Modified-Since', last_modification
|
37
|
+
request = Net::HTTP::Get.new(api_url)
|
38
|
+
request.add_field('If-Modified-Since', last_modification) if File.exist?(self.file_path) and !force
|
39
39
|
response = http.request(request)
|
40
|
-
|
41
|
-
response.code.to_i
|
40
|
+
FileUtils.mkpath(self.file_path.split('/')[0..-2].join('/')) unless File.exist?(self.file_path)
|
41
|
+
File.open(self.file_path, 'w'){ |file| file << response.body } if response.code.to_i == 200 and response.body != ''
|
42
|
+
Util.handle_response(response)
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
@@ -50,37 +51,31 @@ module WebTranslateIt
|
|
50
51
|
# configuration = WebTranslateIt::Configuration.new
|
51
52
|
# locale = configuration.locales.first
|
52
53
|
# file = configuration.files.first
|
53
|
-
# file.upload
|
54
|
+
# file.upload # should respond the HTTP code 202 Accepted
|
54
55
|
#
|
55
56
|
# The meaning of the HTTP 202 code is: the request has been accepted for processing, but the processing has not
|
56
57
|
# been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing
|
57
58
|
# actually takes place.
|
58
59
|
# This is due to the fact that language file imports are handled by background processing.
|
59
|
-
def upload
|
60
|
-
File.open(
|
60
|
+
def upload
|
61
|
+
File.open(self.file_path) do |file|
|
61
62
|
WebTranslateIt::Util.http_connection do |http|
|
62
|
-
request = Net::HTTP::Put::Multipart.new(api_url
|
63
|
-
|
64
|
-
response.code.to_i
|
63
|
+
request = Net::HTTP::Put::Multipart.new(api_url, "file" => UploadIO.new(file, "text/plain", file.path))
|
64
|
+
Util.handle_response(http.request(request))
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
68
|
-
|
69
|
-
# Convenience method which returns the file path of a TranslationFile for a given locale.
|
70
|
-
def file_path_for_locale(locale)
|
71
|
-
self.file_path.gsub("[locale]", locale)
|
72
|
-
end
|
73
|
-
|
68
|
+
|
74
69
|
protected
|
75
70
|
|
76
71
|
# Convenience method which returns the date of last modification of a language file.
|
77
|
-
def last_modification
|
78
|
-
File.mtime(File.new(file_path, 'r')).rfc2822
|
72
|
+
def last_modification
|
73
|
+
File.mtime(File.new(self.file_path, 'r')).rfc2822
|
79
74
|
end
|
80
75
|
|
81
76
|
# Convenience method which returns the URL of the API endpoint for a locale.
|
82
|
-
def api_url
|
83
|
-
"/api/projects/#{api_key}/files/#{self.id}/locales/#{locale}"
|
77
|
+
def api_url
|
78
|
+
"/api/projects/#{self.api_key}/files/#{self.id}/locales/#{self.locale}"
|
84
79
|
end
|
85
80
|
end
|
86
81
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
module WebTranslateIt
|
2
|
-
|
3
2
|
# A few useful functions
|
4
3
|
class Util
|
5
4
|
|
@@ -31,5 +30,23 @@ module WebTranslateIt
|
|
31
30
|
return 0 if total == 0
|
32
31
|
((processed*10)/total).to_f.ceil*10
|
33
32
|
end
|
33
|
+
|
34
|
+
def self.welcome_message
|
35
|
+
puts "Web Translate It v#{WebTranslateIt::Util.version}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.handle_response(response, return_response = false)
|
39
|
+
if response.code.to_i >= 400 and response.code.to_i < 500
|
40
|
+
"We had a problem connecting to Web Translate It with this API key. Make sure it is correct."
|
41
|
+
elsif response.code.to_i >= 500
|
42
|
+
"Web Translate It is temporarily unavailable. Please try again shortly."
|
43
|
+
else
|
44
|
+
if return_response
|
45
|
+
response.body
|
46
|
+
else
|
47
|
+
"#{response.code.to_i} OK"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
34
51
|
end
|
35
52
|
end
|
@@ -1,15 +1,6 @@
|
|
1
1
|
# The Project API Token from Web Translate It
|
2
|
-
api_key:
|
2
|
+
api_key: 4af21ce1fb3a4f7127a60b31ebc41c1446b38bb2
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
ignore_locales:
|
7
|
-
|
8
|
-
# A list of files to translate
|
9
|
-
# You can name your language files as you want, as long as the locale name match the
|
10
|
-
# locale name you set in Web Translate It, and that the different language files names are
|
11
|
-
# differenciated by their locale name.
|
12
|
-
# For example, if you set to translate a project in en_US in WTI, you should use the locale en_US in your app
|
13
|
-
files:
|
14
|
-
- config/locales/file1_[locale].yml
|
15
|
-
- config/locales/file2_[locale].yml
|
4
|
+
# Optional: locales not to sync with Web Translate It.
|
5
|
+
# eg. [:en, :fr] or just 'en'
|
6
|
+
# ignore_locales: :en
|
@@ -1,31 +1,64 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
2
2
|
|
3
3
|
describe WebTranslateIt::Configuration do
|
4
|
-
before(:each) do
|
5
|
-
WebTranslateIt::Configuration.const_set("RAILS_ROOT", File.dirname(__FILE__) + '/../examples')
|
6
|
-
end
|
7
|
-
|
8
4
|
describe "#initialize" do
|
9
5
|
it "should fetch and not blow up" do
|
6
|
+
Rails = OpenStruct.new(:root => Pathname.new(File.dirname(__FILE__) + "/../examples"))
|
10
7
|
lambda{ WebTranslateIt::Configuration.new }.should_not raise_error
|
11
8
|
end
|
12
9
|
|
13
10
|
it "should load the content of the YAML file" do
|
14
11
|
config_hash = {
|
15
|
-
"api_key" => "
|
16
|
-
"ignore_locales" => "en_GB"
|
17
|
-
"files" => ["config/locales/file1_[locale].yml", "config/locales/file2_[locale].yml"]
|
12
|
+
"api_key" => "4af21ce1fb3a4f7127a60b31ebc41c1446b38bb2",
|
13
|
+
"ignore_locales" => "en_GB"
|
18
14
|
}
|
19
15
|
YAML.should_receive(:load_file).and_return(config_hash)
|
20
|
-
WebTranslateIt::Configuration.new
|
16
|
+
WebTranslateIt::Configuration.new(File.dirname(__FILE__) + '/../..', 'examples/translation.yml')
|
21
17
|
end
|
22
18
|
|
23
|
-
it "should assign the API key,
|
19
|
+
it "should assign the API key, files" do
|
20
|
+
Rails = OpenStruct.new(:root => Pathname.new(File.dirname(__FILE__) + "/../examples"))
|
24
21
|
configuration = WebTranslateIt::Configuration.new
|
25
|
-
configuration.api_key.should == '
|
22
|
+
configuration.api_key.should == '4af21ce1fb3a4f7127a60b31ebc41c1446b38bb2'
|
26
23
|
configuration.files.first.should be_a(WebTranslateIt::TranslationFile)
|
27
|
-
configuration.ignore_locales.should == ['en_GB']
|
28
24
|
end
|
29
25
|
end
|
30
26
|
|
27
|
+
describe "#set_locales_to_ignore" do
|
28
|
+
before(:each) do
|
29
|
+
Rails = OpenStruct.new(:root => Pathname.new(File.dirname(__FILE__) + "/../examples"))
|
30
|
+
@configuration = WebTranslateIt::Configuration.new
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return an array" do
|
34
|
+
config_hash = { 'ignore_locales' => 'en' }
|
35
|
+
@configuration.set_locales_to_ignore(config_hash).should be_a(Array)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not blow up if no locales are given" do
|
39
|
+
config_hash = { 'ignore_locales' => nil }
|
40
|
+
@configuration.set_locales_to_ignore(config_hash).should be_a(Array)
|
41
|
+
@configuration.set_locales_to_ignore(config_hash).should == []
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return an array of 2 elements if given array of strings" do
|
45
|
+
config_hash = { 'ignore_locales' => ['en', 'fr'] }
|
46
|
+
@configuration.set_locales_to_ignore(config_hash).should be_a(Array)
|
47
|
+
@configuration.set_locales_to_ignore(config_hash).should == ['en', 'fr']
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return an array of 1 element if given a symbol" do
|
51
|
+
config_hash = { 'ignore_locales' => :en }
|
52
|
+
@configuration.set_locales_to_ignore(config_hash).should be_a(Array)
|
53
|
+
@configuration.set_locales_to_ignore(config_hash).should == ['en']
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should return an array of 2 element if given an array of symbol and string" do
|
57
|
+
config_hash = { 'ignore_locales' => [:en, 'fr'] }
|
58
|
+
@configuration.set_locales_to_ignore(config_hash).should be_a(Array)
|
59
|
+
@configuration.set_locales_to_ignore(config_hash).should == ['en', 'fr']
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
31
64
|
end
|
@@ -2,15 +2,15 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
|
|
2
2
|
|
3
3
|
describe WebTranslateIt::TranslationFile do
|
4
4
|
before(:each) do
|
5
|
-
@translation_file = WebTranslateIt::TranslationFile.new(
|
5
|
+
@translation_file = WebTranslateIt::TranslationFile.new(2267, "examples/en.yml", 'fr', "4af21ce1fb3a4f7127a60b31ebc41c1446b38bb2")
|
6
6
|
end
|
7
7
|
|
8
8
|
describe "#initialize" do
|
9
9
|
it "should assign id, file_path and api_key" do
|
10
|
-
tr_file = WebTranslateIt::TranslationFile.new(
|
11
|
-
tr_file.id.should ==
|
12
|
-
tr_file.file_path.should == "/
|
13
|
-
tr_file.api_key.should == "
|
10
|
+
tr_file = WebTranslateIt::TranslationFile.new(2267, "examples/en.yml", 'fr', "4af21ce1fb3a4f7127a60b31ebc41c1446b38bb2")
|
11
|
+
tr_file.id.should == 2267
|
12
|
+
tr_file.file_path.should == "examples/en.yml"
|
13
|
+
tr_file.api_key.should == "4af21ce1fb3a4f7127a60b31ebc41c1446b38bb2"
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -19,50 +19,40 @@ describe WebTranslateIt::TranslationFile do
|
|
19
19
|
file = mock(File)
|
20
20
|
file.stub(:puts => true, :close => true)
|
21
21
|
File.stub(:exist? => true, :mtime => Time.at(0), :new => file)
|
22
|
-
@translation_file.fetch
|
22
|
+
@translation_file.fetch.should == 200
|
23
23
|
end
|
24
24
|
|
25
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
26
|
file = mock(File)
|
27
27
|
file.stub(:puts => true, :close => true)
|
28
28
|
File.stub(:exist? => true, :mtime => Time.at(0), :new => file)
|
29
|
-
@translation_file.fetch(
|
29
|
+
@translation_file.fetch(true).should == 200
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should prepare a HTTP request and get a 304 OK if the language file is fresh" do
|
33
33
|
file = mock(File)
|
34
34
|
file.stub(:puts => true, :close => true)
|
35
35
|
File.stub(:exist? => true, :mtime => Time.now, :new => file)
|
36
|
-
@translation_file.fetch
|
36
|
+
@translation_file.fetch.should == 304
|
37
37
|
end
|
38
38
|
|
39
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
40
|
file = mock(File)
|
41
41
|
file.stub(:puts => true, :close => true)
|
42
42
|
File.stub(:exist? => true, :mtime => Time.now, :new => file)
|
43
|
-
@translation_file.fetch(
|
43
|
+
@translation_file.fetch(true).should == 200
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
describe "#upload" do
|
48
48
|
it "should prepare a HTTP request and get a 200 OK" do
|
49
49
|
@translation_file.stub(:file_path => File.join(File.dirname(__FILE__), '..', 'examples', 'en.yml'))
|
50
|
-
@translation_file.upload
|
50
|
+
@translation_file.upload
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should fail if the file does not exist" do
|
54
54
|
@translation_file.stub(:file_path => File.join('something', 'that', 'does', 'not', 'exist'))
|
55
|
-
lambda { @translation_file.upload
|
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
|
55
|
+
lambda { @translation_file.upload }.should raise_error
|
66
56
|
end
|
67
57
|
end
|
68
58
|
end
|
@@ -9,7 +9,7 @@ describe WebTranslateIt do
|
|
9
9
|
|
10
10
|
before(:each) do
|
11
11
|
WebTranslateIt::I18n.stub(:locale => 'en')
|
12
|
-
|
12
|
+
Rails = OpenStruct.new(:root => Pathname.new(File.dirname(__FILE__) + "/examples"))
|
13
13
|
@configuration = WebTranslateIt::Configuration.new
|
14
14
|
@file = mock(WebTranslateIt::TranslationFile)
|
15
15
|
@file.stub(:fetch => true)
|
data/version.yml
CHANGED
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
|
+
version: 1.6.0
|
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-03-
|
12
|
+
date: 2010-03-22 00:00:00 +01:00
|
13
13
|
default_executable: wti
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -32,16 +32,6 @@ dependencies:
|
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 1.2.0
|
34
34
|
version:
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: mg
|
37
|
-
type: :development
|
38
|
-
version_requirement:
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 0.0.7
|
44
|
-
version:
|
45
35
|
description: A rack middleware and a handful of rake tasks to sync your translations between webtranslateit.com and your rails applications.
|
46
36
|
email: edouard@atelierconvivialite.com
|
47
37
|
executables:
|
@@ -77,7 +67,7 @@ has_rdoc: true
|
|
77
67
|
homepage: https://webtranslateit.com
|
78
68
|
licenses: []
|
79
69
|
|
80
|
-
post_install_message:
|
70
|
+
post_install_message: " ************************************************************\n\n Thank you for installing web_translate_it\n\n The version 1.6.0 you are upgrading to has some\n important breaking changes. Please be sure to read\n this blog post http://bit.ly/aEox3b\n for useful information about this release.\n\n ************************************************************\n"
|
81
71
|
rdoc_options:
|
82
72
|
- --main
|
83
73
|
- README.md
|