translation 1.9 → 1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c393ed07bd2bf4db910f2b89540920d86da3244
4
- data.tar.gz: f82f7acf41fb97eccdc42eca985b6ae2b2724d21
3
+ metadata.gz: 74fd821b4fe211e17511bac0255df4d19e69ea53
4
+ data.tar.gz: 7412cec20b8b917d1117ede24162392a075fb648
5
5
  SHA512:
6
- metadata.gz: 32fd1caaa72d9de52a640323cd3337609fa09d92044f2cd4747203d272b50010afde2ee97547b606f86f94c0c8d6a2b192834efc8f18822cbdf19cb3d41b7479
7
- data.tar.gz: d1bd9c545975c64e8095ed7b484c2776891f61a7fd48de357b6bcde257c5444e865372ce22ff6b616d999ef9b9f5a4d660616f38dac3cadb8fd210f96958f7bc
6
+ metadata.gz: f06f00c70f5eb7f3daf889c2da2df86ba331da18b7f55fbfb165b76708099b6755ad9e4116eb7898d4bd001af1dc8ba1fad126929a31adf8a3cf3fc605229e40
7
+ data.tar.gz: 75c6637df0ed63be1e9a00afc7d395c2fc22ad9e8f79c20859eb104360ef78c9d418f611b9f613abaefcab05c3673ee086f56367beb489d45e263c7469e80416
data/README.md CHANGED
@@ -1,38 +1,91 @@
1
1
  # Gem for [Translation.io](http://translation.io). [ ![Codeship Status](https://app.codeship.com/projects/f7cd4ac0-b73c-0131-51ea-522dcd2196ed/status?branch=master)](https://app.codeship.com/projects/20528) [![Code Climate](https://codeclimate.com/github/aurels/translation-gem/badges/gpa.svg)](https://codeclimate.com/github/aurels/translation-gem) [![Test Coverage](https://codeclimate.com/github/aurels/translation-gem/badges/coverage.svg)](https://codeclimate.com/github/aurels/translation-gem/coverage)
2
2
 
3
- ## Description
3
+ Add this gem to your [Rails](http://rubyonrails.org) application to localize
4
+ it using [I18n (YAML)](http://guides.rubyonrails.org/i18n.html) or
5
+ [GetText](https://github.com/ruby-gettext/gettext) syntaxes.
4
6
 
5
- Add this gem to your [Rails](http://rubyonrails.org) app to translate it with [Translation.io](http://translation.io).
7
+ Keep it synchronized with your translators on [Translation.io](https://translation.io).
8
+
9
+ Need help? [contact@translation.io](mailto:contact@translation.io)
6
10
 
7
11
  ## Installation
8
12
 
9
- Add the gem to your project's Gemfile:
13
+ 1. Add the gem to your project's Gemfile:
10
14
 
11
15
  ```ruby
12
16
  gem 'translation'
13
17
  ```
14
18
 
15
- Then:
16
-
17
- * Create a translation project [from the UI](https://translation.io).
18
- * Copy the initializer into your Rails app (`config/initializers/translation.rb`)
19
+ 2. Create a new translation project [from the UI](https://translation.io).
20
+ 3. Copy the initializer into your Rails app (`config/initializers/translation.rb`)
19
21
 
20
22
  The initializer looks like this:
21
23
 
22
24
  ```ruby
23
25
  TranslationIO.configure do |config|
24
- config.api_key = 'some api key which is very long'
26
+ config.api_key = 'abcdefghijklmnopqrstuvwxyz012345'
25
27
  config.source_locale = 'en'
26
28
  config.target_locales = ['fr', 'nl', 'de', 'es']
27
29
  end
28
30
  ```
29
31
 
30
- And finish by inititalizing your translation project with:
31
-
32
- bundle exec rake translation:init
32
+ 4. Initialize your translation project with `bundle exec rake translation:init`
33
33
 
34
34
  If you later need to add/remove target languages, please read our
35
- [dedicated article](https://translation.io/blog/adding-target-languages) about that.
35
+ [documentation](https://translation.io/blog/adding-target-languages) about that.
36
+
37
+ ## Usage
38
+
39
+ #### I18n (YAML)
40
+
41
+ ```ruby
42
+ # Regular
43
+ t('inbox.title')
44
+
45
+ # Plural management
46
+ t('inbox.message', count: n)
47
+
48
+ # Interpolation
49
+ t('inbox.hello', name: @user.name)
50
+ ```
51
+
52
+ ...with the source YAML file:
53
+
54
+ ```yaml
55
+ en:
56
+ inbox:
57
+ title: 'text to be translated'
58
+ message:
59
+ zero: 'no messages'
60
+ one: 'one message'
61
+ other: '%{count} messages'
62
+ hello: 'Hello %{name}'
63
+ ```
64
+
65
+ You can keep your source YAML file automatically updated using [i18n-tasks](https://github.com/glebm/i18n-tasks).
66
+
67
+ More information about I18n usage [here](http://guides.rubyonrails.org/i18n.html).
68
+
69
+ #### GetText
70
+
71
+ ```ruby
72
+ # Regular
73
+ _("text to be translated")
74
+
75
+ # Plural management
76
+ n_("singular text", "plural text", number)
77
+
78
+ # Regular with context
79
+ p_("context", "text to be translated")
80
+
81
+ # Plural management with contect
82
+ np_("context", "singular text", "plural text", number)
83
+
84
+ # Interpolations
85
+ _('%{city1} is bigger than %{city2}') % { city1: "NYC", city2: "BXL" }
86
+ ```
87
+
88
+ More information about GetText usage [here](https://github.com/ruby-gettext/gettext#usage).
36
89
 
37
90
  ## Sync
38
91
 
@@ -40,21 +93,29 @@ To send new translatable keys/strings and get new translations from Translation.
40
93
 
41
94
  bundle exec rake translation:sync
42
95
 
96
+ ## Sync and Show Purgeable
97
+
98
+ If you need to see what are the unused keys/strings from Translation.io, using the current branch as reference:
99
+
100
+ bundle exec rake translation:sync_and_show_purgeable
101
+
102
+ As the name says, this operation will also perform a sync at the same time.
103
+
43
104
  ## Sync and Purge
44
105
 
45
- If you also need to remove unused keys/strings from Translation.io using the current branch as reference:
106
+ If you need to remove unused keys/strings from Translation.io, using the current branch as reference:
46
107
 
47
108
  bundle exec rake translation:sync_and_purge
48
109
 
49
110
  As the name says, this operation will also perform a sync at the same time.
50
111
 
51
- Warning: all keys that are not present in the current branch will be **permanently deleted both on Translation.io and in your app**.
112
+ Warning: all keys that are not present in the current branch will be **permanently deleted from Translation.io**.
52
113
 
53
114
  ## Advanced Configuration Options
54
115
 
55
116
  The `TranslationIO.configure` block in `config/initializers/translation.rb` can take several optional configuration options.
56
117
 
57
- ### Disable GetText
118
+ #### Disable GetText
58
119
 
59
120
  Sometimes, you only want to use YAML and don't want to be bothered by GetText at all.
60
121
  For these cases, you just have to add `disable_gettext` in the config file.
@@ -69,7 +130,7 @@ TranslationIO.configure do |config|
69
130
  end
70
131
  ```
71
132
 
72
- ### Ignored YAML keys
133
+ #### Ignored YAML keys
73
134
 
74
135
  Sometimes you would like to ignore some YAML keys coming from gems or so.
75
136
  You can use the `ignored_key_prefixes` for that.
@@ -93,7 +154,27 @@ TranslationIO.configure do |config|
93
154
  end
94
155
  ```
95
156
 
96
- ### Custom localization key prefixes
157
+ #### Source file formats (for GetText)
158
+
159
+ If you are using GetText and you want to manage other file formats than:
160
+
161
+ * `rb`, `erb`, `ruby` and `rabl` for Ruby.
162
+ * `haml` and `mjmlhaml` for [HAML](http://haml.info/).
163
+ * `slim` and `mjmlslim` for [SLIM](http://slim-lang.com/).
164
+
165
+ Just add them in your configuration file like this:
166
+
167
+ ```ruby
168
+ TranslationIO.configure do |config|
169
+ ...
170
+ config.source_formats << 'rb2'
171
+ config.haml_source_formats << 'haml2'
172
+ config.slim_source_formats << 'slim2'
173
+ ...
174
+ end
175
+ ```
176
+
177
+ #### Custom localization key prefixes
97
178
 
98
179
  Rails YAML files contain not only translation strings but also localization values (integers, arrays, booleans)
99
180
  in the same place and that's bad. For example: date formats, number separators, default
@@ -119,7 +200,7 @@ TranslationIO.configure do |config|
119
200
  end
120
201
  ```
121
202
 
122
- ### Paths where locales are stored (not recommended)
203
+ #### Paths where locales are stored (not recommended)
123
204
 
124
205
  You can specify where your GetText and YAML files are on disk:
125
206
 
@@ -138,9 +219,85 @@ To run the specs:
138
219
 
139
220
  bundle exec rspec
140
221
 
222
+ ## Pure Ruby (without Rails)
223
+
224
+ This gem was created specifically for Rails, but you can also use it in a pure Ruby project by making some arrangements:
225
+
226
+ ```ruby
227
+ require 'rubygems'
228
+ require 'active_support/all'
229
+ require 'yaml'
230
+
231
+ class FakeConfig
232
+ def after_initialize
233
+ end
234
+ def development?
235
+ false
236
+ end
237
+ end
238
+
239
+ module Rails
240
+ class Railtie
241
+ def self.rake_tasks
242
+ yield
243
+ end
244
+
245
+ def self.initializer(*args)
246
+ end
247
+
248
+ def self.config
249
+ ::FakeConfig.new
250
+ end
251
+ end
252
+
253
+ def self.env
254
+ ::FakeConfig.new
255
+ end
256
+ end
257
+
258
+ task :environment do
259
+ end
260
+
261
+ require 'translation'
262
+
263
+ I18n.load_path += Dir[File.join('i18n', '**', '*.{yml,yaml}')]
264
+
265
+ # Put your configuration here:
266
+ TranslationIO.configure do |config|
267
+ config.yaml_locales_path = 'i18n'
268
+ config.api_key = ''
269
+ config.source_locale = 'en'
270
+ config.target_locales = ['nl', 'de']
271
+ config.metadata_path = 'i18n/.translation_io'
272
+ end
273
+ ```
274
+
275
+ (Thanks [@kubaw](https://github.com/kubaw) for this snippet!)
276
+
277
+ ## Other implementations
278
+
279
+ These implementations are made by contributors for their own projects and are not
280
+ *currently* supported by [Translation.io](https://translation.io). However, they are quite well documented.
281
+
282
+ Thanks a lot to these contributors for their hard work!
283
+
284
+ #### React and React-Intl (JavaScript)
285
+
286
+ * GitHub: https://github.com/deecewan/translation-io
287
+ * NPM: https://www.npmjs.com/package/translation-io
288
+
289
+ Credit: [@deecewan](https://github.com/deecewan)
290
+
291
+ #### Laravel (PHP)
292
+
293
+ * GitHub: https://github.com/armandsar/laravel-translationio
294
+ * Packagist: https://packagist.org/packages/armandsar/laravel-translationio
295
+
296
+ Credit: [@armandsar](https://github.com/armandsar)
297
+
141
298
  ## Credits
142
299
 
143
- The [translation gem](https://rubygems.org/gems/translation) in released under MIT license by [Aurélien Malisart](http://aurelien.malisart.be) and [Michaël Hoste](http://80limit.com) (see MIT-LICENSE
144
- file).
300
+ The [translation gem](https://rubygems.org/gems/translation) in released under MIT license by
301
+ [Aurélien Malisart](http://aurelien.malisart.be) and [Michaël Hoste](http://80limit.com) (see LICENSE file).
145
302
 
146
- (c) http://translation.io
303
+ (c) [https://translation.io](https://translation.io) / [contact@translation.io](mailto:contact@translation.io)
@@ -19,8 +19,12 @@ module TranslationIO
19
19
  TranslationIO::Client::SyncOperation.new(self).run
20
20
  end
21
21
 
22
+ def sync_and_show_purgeable
23
+ TranslationIO::Client::SyncOperation.new(self).run(:show_purgeable => true)
24
+ end
25
+
22
26
  def sync_and_purge
23
- TranslationIO::Client::SyncOperation.new(self).run(true)
27
+ TranslationIO::Client::SyncOperation.new(self).run(:purge => true)
24
28
  end
25
29
  end
26
30
  end
@@ -34,7 +34,7 @@ module TranslationIO
34
34
  warn_source_locale_unfound(source_locale, all_used_yaml_locales)
35
35
  warn_target_locale_unfound(target_locales, all_used_yaml_locales)
36
36
 
37
- TranslationIO.info "Sending data to server"
37
+ TranslationIO.info "Sending data to server (Init may take some time, please be patient. Sync will be faster)"
38
38
  uri = URI("#{client.endpoint}/projects/#{client.api_key}/init")
39
39
  parsed_response = BaseOperation.perform_request(uri, params)
40
40
 
@@ -4,7 +4,10 @@ require 'translation_io/client/sync_operation/apply_yaml_source_edits_step'
4
4
  module TranslationIO
5
5
  class Client
6
6
  class SyncOperation < BaseOperation
7
- def run(purge = false)
7
+ def run(options = {})
8
+ purge = options.fetch(:purge, false)
9
+ show_purgeable = options.fetch(:show_purgeable, false)
10
+
8
11
  config = TranslationIO.config
9
12
 
10
13
  haml_source_files = config.haml_source_files
@@ -40,12 +43,76 @@ module TranslationIO
40
43
  BaseOperation::SaveNewYamlFilesStep.new(target_locales, yaml_locales_path, parsed_response).run
41
44
  BaseOperation::SaveSpecialYamlFilesStep.new(source_locale, target_locales, yaml_locales_path, yaml_file_paths).run
42
45
 
46
+ display_unused_segments(parsed_response, show_purgeable, purge)
47
+
43
48
  info_project_url(parsed_response)
44
49
  end
45
50
 
46
51
  cleanup
47
52
  end
48
53
 
54
+ def display_unused_segments(parsed_response, show_purgeable, purge)
55
+ yaml_unused_segments = parsed_response['unused_segments'].select { |unused_segment| unused_segment['kind'] == 'yaml' }
56
+ gettext_unused_segments = parsed_response['unused_segments'].select { |unused_segment| unused_segment['kind'] == 'gettext' }
57
+
58
+ yaml_size = yaml_unused_segments.size
59
+ gettext_size = gettext_unused_segments.size
60
+ total_size = yaml_size + gettext_size
61
+
62
+ # Quick unused segments summary for simple "sync"
63
+ if !show_purgeable && !purge
64
+ if total_size > 0
65
+ puts
66
+ puts "----------"
67
+ puts "#{yaml_size + gettext_size} keys/strings are in Translation.io but not in your current branch."
68
+ puts 'Execute "rake translation:sync_and_show_purgeable" to list these keys/strings'
69
+ end
70
+ # Complete summary for sync_and_show_purgeable or sync_and_purge
71
+ else
72
+ if purge
73
+ text = "were removed from Translation.io to match your current branch:"
74
+ elsif show_purgeable
75
+ text = "are in Translation.io but not in your current branch:"
76
+ end
77
+
78
+ if yaml_size > 0
79
+ puts
80
+ puts "----------"
81
+ puts "#{yaml_size} YAML #{yaml_size == 1 ? 'key' : 'keys'} #{text}"
82
+ puts
83
+
84
+ yaml_unused_segments.each do |yaml_unused_segment|
85
+ puts " - [#{yaml_unused_segment['msgctxt']}] \"#{yaml_unused_segment['msgid']}\""
86
+ end
87
+ end
88
+
89
+ if gettext_size > 0
90
+ puts
91
+ puts "----------"
92
+ puts "#{gettext_size} GetText #{gettext_size == 1 ? 'string' : 'strings'} #{text}"
93
+ puts
94
+
95
+ gettext_unused_segments.each do |gettext_unused_segment|
96
+ puts " - \"#{gettext_unused_segment['msgid']}\""
97
+ end
98
+ end
99
+
100
+ # Special message for when nothing need to be purged
101
+ if total_size == 0
102
+ puts
103
+ puts "----------"
104
+ puts "Nothing to purge: all the keys/strings in Translation.io are also in your current branch."
105
+ end
106
+
107
+ # Special message when sync_and_show_purgeable and unused segments
108
+ if show_purgeable && total_size > 0
109
+ puts
110
+ puts "----------"
111
+ puts "If you know what you are doing, you can remove them using \"rake translation:sync_and_purge\""
112
+ end
113
+ end
114
+ end
115
+
49
116
  def info_project_url(parsed_response)
50
117
  puts
51
118
  puts "----------"
@@ -5,8 +5,15 @@ module TranslationIO
5
5
  attr_accessor :endpoint
6
6
  attr_accessor :verbose
7
7
  attr_accessor :test
8
+
8
9
  attr_accessor :ignored_key_prefixes
10
+ attr_accessor :ignored_source_paths
9
11
  attr_accessor :ignored_source_files
12
+
13
+ attr_accessor :source_formats
14
+ attr_accessor :haml_source_formats
15
+ attr_accessor :slim_source_formats
16
+
10
17
  attr_accessor :localization_key_prefixes
11
18
  attr_accessor :disable_gettext
12
19
  attr_accessor :charset
@@ -26,18 +33,25 @@ module TranslationIO
26
33
  self.endpoint = 'https://translation.io/api'
27
34
  self.verbose = 1
28
35
  self.test = false
36
+
29
37
  self.ignored_key_prefixes = []
38
+ self.ignored_source_paths = ['vendor/', 'tmp/']
30
39
  self.ignored_source_files = [] # Files not parsed for GetText entries
40
+
41
+ self.source_formats = ['rb', 'erb', 'ruby', 'rabl']
42
+ self.haml_source_formats = ['haml', 'mjmlhaml']
43
+ self.slim_source_formats = ['slim', 'mjmlslim']
44
+
31
45
  self.localization_key_prefixes = []
32
46
  self.disable_gettext = false
33
47
  self.charset = 'UTF-8'
34
48
  self.metadata_path = File.join('config', 'locales', '.translation_io')
35
49
 
36
- self.pot_msgid_bugs_address = 'contact@translation.io'
37
- self.pot_package_name = File.basename(Dir.pwd)
38
- self.pot_package_version = '1.0'
39
- self.pot_copyright_holder = File.basename(Dir.pwd)
40
- self.pot_copyright_year = Date.today.year
50
+ self.pot_msgid_bugs_address = 'contact@translation.io'
51
+ self.pot_package_name = File.basename(Dir.pwd)
52
+ self.pot_package_version = '1.0'
53
+ self.pot_copyright_holder = File.basename(Dir.pwd)
54
+ self.pot_copyright_year = Date.today.year
41
55
  end
42
56
 
43
57
  def pot_path
@@ -51,27 +65,33 @@ module TranslationIO
51
65
  end
52
66
 
53
67
  def source_files
54
- file_paths = Dir['**/*.{rb,erb,ruby,rabl}'].select do |p|
55
- !p.start_with?('vendor/') && !p.start_with?('tmp/')
56
- end
57
-
58
- file_paths - ignored_source_files
68
+ source_files_for_formats(source_formats)
59
69
  end
60
70
 
61
71
  def haml_source_files
62
- Dir['**/*.{haml}'].select do |p|
63
- !p.start_with?('vendor/') && !p.start_with?('tmp/')
64
- end
72
+ source_files_for_formats(haml_source_formats)
65
73
  end
66
74
 
67
75
  def slim_source_files
68
- Dir['**/*.{slim,mjmlslim}'].select do |p|
69
- !p.start_with?('vendor/') && !p.start_with?('tmp/')
76
+ source_files_for_formats(slim_source_formats)
77
+ end
78
+
79
+ def source_files_for_formats(formats)
80
+ file_paths = Dir["**/*.{#{formats.join(',')}}"]
81
+
82
+ # remove ignored files
83
+ file_paths = file_paths - ignored_source_files
84
+
85
+ # remove ignored paths
86
+ ignored_source_paths.each do |ignored_source_path|
87
+ file_paths = file_paths.select { |file_path| !file_path.start_with?(ignored_source_path) }
70
88
  end
89
+
90
+ file_paths
71
91
  end
72
92
 
73
93
  def to_s
74
- "#{api_key} - #{source_locale} => #{target_locales.join(' + ')}"
94
+ "API Key: #{api_key} | Languages: #{source_locale} => [#{target_locales.join(', ')}]"
75
95
  end
76
96
  end
77
97
  end
@@ -77,7 +77,7 @@ module TranslationIO
77
77
  current_object[current_key] = {}
78
78
  end
79
79
  current_object = current_object[current_key]
80
- current_object[new_key] = value if current_object.respond_to? :each
80
+ current_object[new_key] = value if current_object.is_a?(Hash)
81
81
 
82
82
  key_string = ''
83
83
  end
@@ -5,33 +5,46 @@ require 'gettext/tools'
5
5
 
6
6
  namespace :translation do
7
7
 
8
- DESCRIPTIONS = {
9
- :init => "Initialize your Translation.io project",
10
- :sync => "Send new translatable keys/strings and get new translations from Translation.io",
11
- :sync_and_purge => "Remove unused keys/strings from Translation.io using the current branch as reference"
12
- }
13
-
14
8
  desc "Get configuration infos of Translation gem"
15
9
  task :config => :environment do
16
10
  puts TranslationIO.config
17
11
  end
18
12
 
19
- DESCRIPTIONS.each_pair do |t, description|
20
- desc description
21
- task t => :environment do
22
- if TranslationIO.client
23
- TranslationIO.client.send(t)
24
- else
25
- message = <<EOS
26
- [Error] Can't configure client. Did you set up the initializer?
27
- Read usage instructions here : http://translation.io/usage
28
- EOS
29
- TranslationIO.info(message)
30
- end
13
+ desc "Initialize your Translation.io project"
14
+ task :init => :environment do
15
+ if client_ready?
16
+ TranslationIO.client.init
17
+ end
18
+ end
19
+
20
+ desc "Send new translatable keys/strings and get new translations from Translation.io"
21
+ task :sync => :environment do
22
+ if client_ready?
23
+ TranslationIO.client.sync
31
24
  end
32
25
  end
33
26
 
34
- task :purge => :environment do
35
- TranslationIO.info("Purge is deprecated, please use rake 'translation:sync_and_purge' instead.")
27
+ desc "Sync and show unused keys/strings from Translation.io using the current branch as reference"
28
+ task :sync_and_show_purgeable => :environment do
29
+ if client_ready?
30
+ TranslationIO.client.sync_and_show_purgeable
31
+ end
32
+ end
33
+
34
+ desc "Sync and remove unused keys/strings from Translation.io using the current branch as reference"
35
+ task :sync_and_purge => :environment do
36
+ if client_ready?
37
+ TranslationIO.client.sync_and_purge
38
+ end
39
+ end
40
+
41
+ def client_ready?
42
+ if TranslationIO.client
43
+ true
44
+ else
45
+ TranslationIO.info("[Error] Can't configure client. Did you set up the initializer?\n"\
46
+ "Read usage instructions here : http://translation.io/usage")
47
+ false
48
+ end
36
49
  end
37
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translation
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.9'
4
+ version: '1.10'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aurelien Malisart
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-16 00:00:00.000000000 Z
12
+ date: 2017-10-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gettext
@@ -128,7 +128,7 @@ files:
128
128
  - lib/translation_io/tasks.rb
129
129
  - lib/translation_io/yaml_conversion.rb
130
130
  - lib/translation_io/yaml_entry.rb
131
- homepage: http://translation.io
131
+ homepage: https://translation.io
132
132
  licenses:
133
133
  - MIT
134
134
  metadata: {}