wti_gettext_i18n_rails 1.0.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/MIT-LICENSE +22 -0
- data/README.md +106 -0
- data/bin/wti +5 -0
- data/examples/locale.rb +12 -0
- data/examples/translation.yml +18 -0
- data/generators/webtranslateit/lib/insert_commands.rb +34 -0
- data/generators/webtranslateit/templates/translation.yml +17 -0
- data/generators/webtranslateit/webtranslateit_generator.rb +26 -0
- data/history.md +99 -0
- data/lib/web_translate_it/auto_fetch.rb +22 -0
- data/lib/web_translate_it/command_line.rb +167 -0
- data/lib/web_translate_it/configuration.rb +54 -0
- data/lib/web_translate_it/project.rb +38 -0
- data/lib/web_translate_it/tasks.rb +59 -0
- data/lib/web_translate_it/translation_file.rb +97 -0
- data/lib/web_translate_it/util.rb +35 -0
- data/lib/web_translate_it.rb +20 -0
- data/man/wti.1 +90 -0
- data/man/wti.1.html +157 -0
- data/man/wti.1.ron +81 -0
- data/spec/examples/config/translation.yml +15 -0
- data/spec/examples/en.yml +38 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/web_translate_it/configuration_spec.rb +31 -0
- data/spec/web_translate_it/translation_file_spec.rb +68 -0
- data/spec/web_translate_it/util_spec.rb +10 -0
- data/spec/web_translate_it_spec.rb +27 -0
- data/version.yml +4 -0
- metadata +132 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009-2010 Atelier Convivialité
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# Web Translate It
|
2
|
+
|
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) |
|
7
|
+
[Example app](http://github.com/AtelierConvivialite/rails_example_app)
|
8
|
+
|
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.
|
10
|
+
|
11
|
+

|
12
|
+
|
13
|
+
This gem provides your app with:
|
14
|
+
|
15
|
+
* an executable, `wti`, to upload and download language files from the command line (or in whatever else you want to execute it)
|
16
|
+
* a handful of rake task to fetch and upload your translations.
|
17
|
+
* a rack middleware to automatically fetch new translations from Web Translate It.
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
gem install web_translate_it
|
22
|
+
|
23
|
+
That’s it! At this point you have the Web Translate It executable.
|
24
|
+
If your project if already set up on Web Translate It, run `wti autoconf` to generate the configuration file.
|
25
|
+
|
26
|
+
Run `wti --help` to see the usage:
|
27
|
+
|
28
|
+
pull Pull target language file(s) from Web Translate It.
|
29
|
+
push Push master language file(s) to Web Translate It.
|
30
|
+
autoconf Configure your project to sync with Web Translate It.
|
31
|
+
stats Fetch and display your project statistics.
|
32
|
+
|
33
|
+
OPTIONAL PARAMETERS:
|
34
|
+
--------------------
|
35
|
+
-l --locale The ISO code of a specific locale to pull or push.
|
36
|
+
-c --config Path to a translation.yml file. If this option
|
37
|
+
is absent, looks for config/translation.yml.
|
38
|
+
--all Respectively download or upload all files.
|
39
|
+
--force Force wti pull to re-download the language file,
|
40
|
+
regardless if local version is current.
|
41
|
+
OTHER:
|
42
|
+
------
|
43
|
+
-v --version Show version.
|
44
|
+
-h --help This page.
|
45
|
+
|
46
|
+
## Specific tools for Ruby on Rails
|
47
|
+
|
48
|
+
This gem includes some rake tasks and a rack middleware to integrate Web Translate It with Ruby on Rails.
|
49
|
+
|
50
|
+
* Add to your config/environments.rb:
|
51
|
+
|
52
|
+
`config.gem 'web_translate_it'`
|
53
|
+
|
54
|
+
* Then, run:
|
55
|
+
|
56
|
+
`rake gems:install`
|
57
|
+
|
58
|
+
* Copy/paste your api key from Web Translate It and run:
|
59
|
+
|
60
|
+
`script/generate webtranslateit --api-key your_key_here`
|
61
|
+
|
62
|
+
The generator does two things:
|
63
|
+
|
64
|
+
- It adds a auto-configured `config/translation.yml` file using Web Translate It’s API.
|
65
|
+
- It adds `require 'web_translate_it/tasks' rescue LoadError` to your `Rakefile`
|
66
|
+
|
67
|
+
### Rake tasks provided
|
68
|
+
|
69
|
+
There are 3 rake tasks.
|
70
|
+
|
71
|
+
rake trans:fetch:all
|
72
|
+
|
73
|
+
Fetch the latest translations for all your files for all languages defined in Web Translate It’s interface, except for the languages set in `ignore_locales`.
|
74
|
+
|
75
|
+
rake trans:fetch[fr_FR]
|
76
|
+
|
77
|
+
Fetch the latest translations for all the languages defined in Web Translate It’s interface. It takes the locale name as a parameter
|
78
|
+
|
79
|
+
rake trans:upload[fr_FR]
|
80
|
+
|
81
|
+
Upload to Web Translate It your files in a specific locale defined in Web Translate It’s interface.
|
82
|
+
|
83
|
+
Read more about [Rails integration in the wiki](http://wiki.github.com/AtelierConvivialite/webtranslateit/).
|
84
|
+
|
85
|
+
|
86
|
+
## Supported Rails Versions
|
87
|
+
|
88
|
+
The gem currently has been tested against the following versions of Rails:
|
89
|
+
|
90
|
+
* 2.3.4
|
91
|
+
* 2.3.5
|
92
|
+
|
93
|
+
Please open a discussion on [our support site](http://help.webtranslateit.com) if you're using a version of Rails that is not listed above and the gem is not working properly.
|
94
|
+
|
95
|
+
# Acknowledgement
|
96
|
+
|
97
|
+
* The executable is very much inspired from the awesome [Gemcutter](http://gemcutter.org/) commands,
|
98
|
+
* The Rails generator has been pinched from [Hoptoad Notifier](http://github.com/thoughtbot/hoptoad_notifier/).
|
99
|
+
|
100
|
+
# What is Web Translate It anyway?
|
101
|
+
|
102
|
+
Web Translate It is a web-based translation hub to collaboratively translate software.
|
103
|
+
|
104
|
+
To learn more about it, please visit our [tour page](https://webtranslateit.com/tour).
|
105
|
+
|
106
|
+
This gem is released under the MIT License.
|
data/bin/wti
ADDED
data/examples/locale.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# The Project API Token from Web Translate It
|
2
|
+
api_key: SECRET
|
3
|
+
|
4
|
+
# The locales not to sync with Web Translate It.
|
5
|
+
# Pass an array of string, or an array of symbols, a string or a symbol.
|
6
|
+
# 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
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Mostly pinched from http://github.com/ryanb/nifty-generators/tree/master
|
2
|
+
|
3
|
+
Rails::Generator::Commands::Base.class_eval do
|
4
|
+
def file_contains?(relative_destination, line)
|
5
|
+
File.read(destination_path(relative_destination)).include?(line)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
Rails::Generator::Commands::Create.class_eval do
|
10
|
+
def append_to(file, line)
|
11
|
+
logger.insert "#{line} appended to #{file}"
|
12
|
+
unless options[:pretend] || file_contains?(file, line)
|
13
|
+
File.open(file, "a") do |file|
|
14
|
+
file.puts
|
15
|
+
file.puts line
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Rails::Generator::Commands::Destroy.class_eval do
|
22
|
+
def append_to(file, line)
|
23
|
+
logger.remove "#{line} removed from #{file}"
|
24
|
+
unless options[:pretend]
|
25
|
+
gsub_file file, "\n#{line}", ''
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
Rails::Generator::Commands::List.class_eval do
|
31
|
+
def append_to(file, line)
|
32
|
+
logger.insert "#{line} appended to #{file}"
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# The Project API Token from Web Translate It
|
2
|
+
api_key: '<%= api_key %>'
|
3
|
+
|
4
|
+
# The locales not to sync with Web Translate It.
|
5
|
+
# Pass an array of string, or an array of symbols, a string or a symbol.
|
6
|
+
# 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 -%>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
|
2
|
+
|
3
|
+
class WebtranslateitGenerator < Rails::Generator::Base
|
4
|
+
def add_options!(opt)
|
5
|
+
opt.on('-k', '--api-key=key', String, "Your Web Translate It API key") {|v| options[:api_key] = v}
|
6
|
+
end
|
7
|
+
|
8
|
+
def manifest
|
9
|
+
if !api_key_configured? && !options[:api_key]
|
10
|
+
puts "You must pass --api-key or create config/translations.yml"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
record do |m|
|
14
|
+
if options[:api_key]
|
15
|
+
project_details = YAML.load WebTranslateIt::Project.fetch_info(options[:api_key])
|
16
|
+
m.template 'translation.yml', 'config/translation.yml',
|
17
|
+
:assigns => { :api_key => options[:api_key], :project => project_details["project"] }
|
18
|
+
m.append_to 'Rakefile', "require 'web_translate_it/tasks' rescue LoadError"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def api_key_configured?
|
24
|
+
File.exists?('config/translations.yml')
|
25
|
+
end
|
26
|
+
end
|
data/history.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
## Version 1.5.2 /2010-03-13
|
2
|
+
|
3
|
+
* Only added a man page.
|
4
|
+
|
5
|
+
## Version 1.5.1 /2010-03-09
|
6
|
+
|
7
|
+
* Add `wti stats`, to fetch the project stats from the stats API endpoint.
|
8
|
+
|
9
|
+
## Version 1.5.0 /2010-02-19
|
10
|
+
|
11
|
+
Warning, some deprecations in this version.
|
12
|
+
|
13
|
+
* Remove `wti fetch` and `wti upload`. It was deprecated in 1.4.7 in favour of `wti pull` and `wti push` respectively.
|
14
|
+
* `wti push` now only pushes the master language file. Use `wti push --all` to push all or `wti push -l [locale]` to push a specific locale.
|
15
|
+
* `wti pull` now only pulls the target language files. Use `wti pull --all` to pull all or `wti pull -l [locale]` to pull a specific locale.
|
16
|
+
* Increase read timeout to 40s — Required for very large projects.
|
17
|
+
* Bug fix: `wti autoconf` now create directory if it doesn't exist.
|
18
|
+
* Bug fix: `wti autoconf` now ask the user where are the locale files.
|
19
|
+
|
20
|
+
## Version 1.4.7 /2010-02-05
|
21
|
+
|
22
|
+
* Add deprecation warning for `wti --fetch`, `wti -f`. These commands will be deprecated in favour of `wti pull`.
|
23
|
+
* Add deprecation warning for `wti --upload`, `wti -u`. These commands will be deprecated in favour of `wti push`.
|
24
|
+
* Add -c parameter to specify a configuration file at a custom location.
|
25
|
+
* Add -l parameter to specify a specific language file to pull or push (only works with `wti pull` and `wti push`).
|
26
|
+
* Add --force parameter to force Web Translate It to send the language files again, regardless if the current
|
27
|
+
language file version is current (this makes `wti pull` very much slower).
|
28
|
+
* Add `wti autoconf` command to automatically configure your project for Web Translate It.
|
29
|
+
|
30
|
+
## Version 1.4.6 /2010-02-04
|
31
|
+
|
32
|
+
* Add feedback when using the `wti command`.
|
33
|
+
* Fix bug where fetch requests were not using the conditional get requests feature.
|
34
|
+
|
35
|
+
## Version 1.4.5 /2010-02-02
|
36
|
+
|
37
|
+
* Improved documentation
|
38
|
+
* Web Translate is now an executable: `wti`
|
39
|
+
|
40
|
+
## Version 1.4.4 / 2010-02-01
|
41
|
+
|
42
|
+
* Add generator to automatically configure your project, given a Web Translate It API key.
|
43
|
+
* Remove rake trans:config, as configuration is now handled by the generator.
|
44
|
+
|
45
|
+
## Version 1.4.3 / 2010-01-09
|
46
|
+
|
47
|
+
* Remove colour outputs as it increases code complexity and doesn't add any value.
|
48
|
+
* Rack middleware now write to the application’s log file instead of just puts-ing
|
49
|
+
* Better error messages for misconfigured projects
|
50
|
+
|
51
|
+
## Version 1.4.2 / 2010-01-07
|
52
|
+
|
53
|
+
* Bug fix for `rake trans:config` which was not installing the translation.yml file properly.
|
54
|
+
|
55
|
+
## Version 1.4.1 / 2010-01-07
|
56
|
+
|
57
|
+
* Rename `rake trans:send[fr_Fr]` to `rake trans:upload[fr_FR]`
|
58
|
+
* Remove `rake trans:version` task. Instead, version number is displayed in the welcome banner.
|
59
|
+
* Code refactoring
|
60
|
+
* More tests
|
61
|
+
|
62
|
+
## Version 1.4.0 / 2010-01-06
|
63
|
+
|
64
|
+
* The plugin is now a gem
|
65
|
+
|
66
|
+
## Version 1.3.0 / 2010-01-05
|
67
|
+
|
68
|
+
* Add rack middleware to automatically fetch your translations
|
69
|
+
* Remove `autofetch` parameter in configuration file as it is better off to leave it to the user to
|
70
|
+
include the rack middleware in each environment file.
|
71
|
+
* Add example of rack middleware for setting up I18n.locale
|
72
|
+
|
73
|
+
## Version 1.2.1 / 2010-01-04
|
74
|
+
|
75
|
+
* Add some documentation
|
76
|
+
* More feedback on file upload
|
77
|
+
|
78
|
+
## Version 1.2 / 2010-01-04
|
79
|
+
|
80
|
+
* New: Ability to **update** language files via PUT requests. Upload brand new language files is not possible at the moment.
|
81
|
+
|
82
|
+
## Version 1.1.1 / 2010-01-04
|
83
|
+
|
84
|
+
* Fix: locales in exclude list are no longer autofetched when browsing the app in these locales.
|
85
|
+
|
86
|
+
## Version 1.1 / 2010-01-04
|
87
|
+
|
88
|
+
* Support for multi-files projects.
|
89
|
+
* Deprecate `rake translation:*` in favour of the shorter form `rake trans`.
|
90
|
+
* Add task `rake trans:fetch[en_US]` to fetch files locale by locale.
|
91
|
+
* Add task `rake trans:fetch:all` to fetch all the files for all locales defined in Web Translate It’s web interface.
|
92
|
+
|
93
|
+
## Version 1.0.0 / 2009-11-02
|
94
|
+
|
95
|
+
* Better support for exceptions.
|
96
|
+
|
97
|
+
## Version 0.9.0 / 2009-10-26
|
98
|
+
|
99
|
+
* First version, plugin only support download of strings. Strings upload will be available in a later version.
|
@@ -0,0 +1,22 @@
|
|
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
|
+
#
|
11
|
+
class AutoFetch
|
12
|
+
def initialize(app)
|
13
|
+
@app = app
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
WebTranslateIt::fetch_translations
|
18
|
+
status, headers, response = @app.call(env)
|
19
|
+
[status, headers, response.body]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module WebTranslateIt
|
3
|
+
class CommandLine
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
OPTIONS = <<-OPTION
|
7
|
+
pull Pull target language file(s) from Web Translate It.
|
8
|
+
push Push master language file(s) to Web Translate It.
|
9
|
+
autoconf Configure your project to sync with Web Translate It.
|
10
|
+
stats Fetch and display your project statistics.
|
11
|
+
|
12
|
+
OPTIONAL PARAMETERS:
|
13
|
+
--------------------
|
14
|
+
-l --locale The ISO code of a specific locale to pull or push.
|
15
|
+
-c --config Path to a translation.yml file. If this option
|
16
|
+
is absent, looks for config/translation.yml.
|
17
|
+
--all Respectively download or upload all files.
|
18
|
+
--force Force wti pull to re-download the language file,
|
19
|
+
regardless if local version is current.
|
20
|
+
OTHER:
|
21
|
+
------
|
22
|
+
-v --version Show version.
|
23
|
+
-h --help This page.
|
24
|
+
OPTION
|
25
|
+
|
26
|
+
def self.run
|
27
|
+
case ARGV[0]
|
28
|
+
when 'pull'
|
29
|
+
pull
|
30
|
+
when 'push'
|
31
|
+
push
|
32
|
+
when 'autoconf'
|
33
|
+
autoconf
|
34
|
+
when 'stats'
|
35
|
+
stats
|
36
|
+
when '-v', '--version'
|
37
|
+
show_version
|
38
|
+
when '-h', '--help'
|
39
|
+
show_options
|
40
|
+
else
|
41
|
+
puts "Command not found"
|
42
|
+
show_options
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.pull
|
47
|
+
configuration = fetch_configuration
|
48
|
+
locales = fetch_locales_to_pull(configuration)
|
49
|
+
configuration.files.each do |file|
|
50
|
+
locales.each do |locale|
|
51
|
+
puts "Pulling #{file.file_path_for_locale(locale)}…"
|
52
|
+
file.fetch(locale, ARGV.index('--force'))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.push
|
58
|
+
configuration = fetch_configuration
|
59
|
+
locales = fetch_locales_to_push(configuration)
|
60
|
+
configuration.files.each do |file|
|
61
|
+
locales.each do |locale|
|
62
|
+
puts "Pushing #{file.file_path_for_locale(locale)}…"
|
63
|
+
file.upload(locale)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.autoconf
|
69
|
+
puts "We will attempt to configure your project automagically"
|
70
|
+
puts "Please enter your project API Key:"
|
71
|
+
api_key = STDIN.gets.strip
|
72
|
+
if api_key == ""
|
73
|
+
puts "You must enter your project API key provided by Web Translate It"
|
74
|
+
exit
|
75
|
+
end
|
76
|
+
puts "Where should we create the configuration file? (Default: `config/translation.yml`)"
|
77
|
+
path = STDIN.gets.strip
|
78
|
+
path = "config/translation.yml" if path == ""
|
79
|
+
FileUtils.mkpath(path.split('/')[0..path.split('/').size-1])
|
80
|
+
puts "Where are you language files located? (Default: `config/locales/`)"
|
81
|
+
path_to_locale_files = STDIN.gets.strip
|
82
|
+
path_to_locale_files = "config/locales/" if path_to_locale_files == ""
|
83
|
+
FileUtils.mkpath(path.split('/')[0..path.split('/').size-1])
|
84
|
+
File.open(path, 'w'){ |file| file << generate_configuration(api_key, path_to_locale_files) }
|
85
|
+
puts "Done! You can now use `wti` to push and pull your language files."
|
86
|
+
puts "Check `wti --help` for more information."
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.stats
|
90
|
+
configuration = fetch_configuration
|
91
|
+
stats = YAML.load(Project.fetch_stats(configuration.api_key))
|
92
|
+
stats.each do |locale, values|
|
93
|
+
percent_translated = Util.calculate_percentage(values['count_strings_to_proofread'] + values['count_strings_done'] + values['count_strings_to_verify'], values['count_strings'])
|
94
|
+
percent_completed = Util.calculate_percentage(values['count_strings_done'], values['count_strings'])
|
95
|
+
puts "#{locale}: #{percent_translated}% translated, #{percent_completed}% completed #{values['stale'] ? "Stale" : ""}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.show_options
|
100
|
+
puts ""
|
101
|
+
puts "Web Translate It Help:"
|
102
|
+
puts "**********************"
|
103
|
+
$stdout.puts OPTIONS
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.show_version
|
107
|
+
puts ""
|
108
|
+
puts "Web Translate It #{WebTranslateIt::Util.version}"
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.fetch_configuration
|
112
|
+
if (index = ARGV.index('-c') || ARGV.index('--config')).nil?
|
113
|
+
configuration = WebTranslateIt::Configuration.new('.')
|
114
|
+
else
|
115
|
+
configuration = WebTranslateIt::Configuration.new('.', ARGV[index+1])
|
116
|
+
end
|
117
|
+
return configuration
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.fetch_locales_to_pull(configuration)
|
121
|
+
if (index = ARGV.index('-l') || ARGV.index('--locale')).nil?
|
122
|
+
locales = configuration.target_locales
|
123
|
+
else
|
124
|
+
locales = [ARGV[index+1]]
|
125
|
+
end
|
126
|
+
locales.push(configuration.source_locale) if ARGV.index('--all')
|
127
|
+
return locales.uniq
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.fetch_locales_to_push(configuration)
|
131
|
+
if (index = ARGV.index('-l') || ARGV.index('--locale')).nil?
|
132
|
+
locales = [configuration.source_locale]
|
133
|
+
else
|
134
|
+
locales = [ARGV[index+1]]
|
135
|
+
end
|
136
|
+
locales.push(configuration.target_locales) if ARGV.index('--all')
|
137
|
+
return locales.uniq
|
138
|
+
end
|
139
|
+
|
140
|
+
def self.generate_configuration(api_key, path_to_locale_files)
|
141
|
+
project_info = YAML.load WebTranslateIt::Project.fetch_info(api_key)
|
142
|
+
project = project_info['project']
|
143
|
+
file = <<-FILE
|
144
|
+
api_key: #{api_key}
|
145
|
+
|
146
|
+
# The locales not to sync with Web Translate It.
|
147
|
+
# Pass an array of string, or an array of symbols, a string or a symbol.
|
148
|
+
# eg. [:en, :fr] or just 'en'
|
149
|
+
ignore_locales: '#{project["source_locale"]["code"]}'
|
150
|
+
|
151
|
+
# A list of files to translate
|
152
|
+
# You can name your language files as you want, as long as the locale name match the
|
153
|
+
# locale name you set in Web Translate It, and that the different language files names are
|
154
|
+
# differenciated by their locale name.
|
155
|
+
# For example, if you set to translate a project in en_US in WTI, you should use the locale en_US in your app
|
156
|
+
#
|
157
|
+
files:
|
158
|
+
FILE
|
159
|
+
project["project_files"].each do |project_file|
|
160
|
+
if project_file["master"]
|
161
|
+
file << " #{project_file["id"]}: #{path_to_locale_files}" + project_file["name"].gsub(project["source_locale"]["code"], "[locale]") + "\n"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
return file
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -0,0 +1,54 @@
|
|
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
|
+
#
|
9
|
+
class Configuration
|
10
|
+
require 'yaml'
|
11
|
+
require 'fileutils'
|
12
|
+
attr_accessor :path, :api_key, :source_locale, :target_locales, :files, :ignore_locales, :logger
|
13
|
+
|
14
|
+
# Load 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))
|
18
|
+
self.logger = logger
|
19
|
+
self.api_key = configuration['api_key']
|
20
|
+
self.files = []
|
21
|
+
self.ignore_locales = configuration['ignore_locales'].split.map{ |locale| locale.to_s }
|
22
|
+
configuration['files'].each do |file_id, file_path|
|
23
|
+
self.files.push(TranslationFile.new(file_id, root_path + '/' + file_path, api_key))
|
24
|
+
end
|
25
|
+
set_locales
|
26
|
+
end
|
27
|
+
|
28
|
+
# Makes an API request to fetch the list of the different locales for a project.
|
29
|
+
# Implementation example:
|
30
|
+
#
|
31
|
+
# configuration = WebTranslateIt::Configuration.new
|
32
|
+
# 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']
|
36
|
+
self.source_locale = project['source_locale']['code']
|
37
|
+
self.target_locales = project['target_locales'].map{|locale| locale['code']}
|
38
|
+
end
|
39
|
+
|
40
|
+
# Convenience method which returns the endpoint for fetching a list of locales for a project.
|
41
|
+
def api_url
|
42
|
+
"/api/projects/#{api_key}.yaml"
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns a logger. If RAILS_DEFAULT_LOGGER is defined, use it, else, define a new logger.
|
46
|
+
def logger
|
47
|
+
if defined?(Rails.logger)
|
48
|
+
Rails.logger
|
49
|
+
elsif defined?(RAILS_DEFAULT_LOGGER)
|
50
|
+
RAILS_DEFAULT_LOGGER
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module WebTranslateIt
|
2
|
+
class Project
|
3
|
+
|
4
|
+
def self.fetch_info(api_key)
|
5
|
+
WebTranslateIt::Util.http_connection do |http|
|
6
|
+
request = Net::HTTP::Get.new("/api/projects/#{api_key}.yaml")
|
7
|
+
response = http.request(request)
|
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
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.fetch_stats(api_key)
|
22
|
+
WebTranslateIt::Util.http_connection do |http|
|
23
|
+
request = Net::HTTP::Get.new("/api/projects/#{api_key}/stats.yaml")
|
24
|
+
response = http.request(request)
|
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
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|