gettext_i18n_rails_js 1.0.1 → 1.0.2

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: edf990e13090b53daedd585fa7f63adbe35650c9
4
- data.tar.gz: 69581dc8e6a90b3d976067a03ae6912ed2c1eb6d
3
+ metadata.gz: 96583f4fe33c2c6d46ec4b789274af912cb6ced8
4
+ data.tar.gz: c062e86460008245a06e61ca8444feaaaec7fe35
5
5
  SHA512:
6
- metadata.gz: 95521966ffbf0dcf118c1afb84f1e22d357a43f6a2e9a29816a19cb61c5e4e5318e4fcaf6ca1f388085a29f09720331d210569fd1c3b93afbc3d93db00444a0b
7
- data.tar.gz: 910fc40c326924717f8ac9d9cb4dd12a0848d8ebbb04d7ec0b00aa9150ecd4f81ee675a1a0693afc2a4d014ad27636078b82f44e2546646cad9de007a7d7d213
6
+ metadata.gz: c0b77ad2c1df0f37351426780d230ee9a30c7c8a67b14cc340f1d9f13a46daebc431284158faf5dc6f0f30b53a3eae1c350e7c62ce730b6c29412b7ad3132e3b
7
+ data.tar.gz: 23e4817c2b8cff7d6cb96d108393303640eef63c7231036a4166677ee7bd61c6c3efa141114543c7ede0f3865b75cc3705760445add52452884da099ba4e0f88
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.0.2](https://github.com/webhippie/gettext_i18n_rails_js/releases/tag/v1.0.2) - 2015-03-30
4
+
5
+ * Fixes exception when parsing empty js/coffee files
6
+ * Avoid methods defined in rake task exposing globally
7
+ * Added better configuration options
8
+
3
9
  ## [1.0.1](https://github.com/webhippie/gettext_i18n_rails_js/releases/tag/v1.0.1) - 2015-02-24
4
10
 
5
11
  * Added missing javascripts to the gemspec
data/README.md CHANGED
@@ -82,6 +82,22 @@ jed_options:
82
82
  pretty: false
83
83
  ```
84
84
 
85
+ If you prefer an initializer file within your rails application you can use
86
+ that in favor of the YML configuration as well:
87
+
88
+ ```ruby
89
+ GettextI18nRailsJs.config do |config|
90
+ config.output_path = "app/assets/javascripts/locale"
91
+
92
+ config.handlebars_function = "__"
93
+ config.javascript_function = "__"
94
+
95
+ config.jed_options = {
96
+ pretty: false
97
+ }
98
+ end
99
+ ```
100
+
85
101
 
86
102
  ## Todo
87
103
 
@@ -26,7 +26,7 @@
26
26
  //= require_self
27
27
 
28
28
  (function() {
29
- var locales = locales || {};
29
+ var locales = this.locales || {};
30
30
  var locale = document.getElementsByTagName('html')[0].lang;
31
31
 
32
32
  if(!locale) {
@@ -42,7 +42,13 @@ require "po_to_json"
42
42
 
43
43
  require_relative "gettext_i18n_rails_js/version"
44
44
  require_relative "gettext_i18n_rails_js/parser"
45
+ require_relative "gettext_i18n_rails_js/config"
45
46
  require_relative "gettext_i18n_rails_js/engine"
46
47
 
47
48
  module GettextI18nRailsJs
49
+ class << self
50
+ def config(&block)
51
+ @config ||= GettextI18nRailsJs::Config.new(&block)
52
+ end
53
+ end
48
54
  end
@@ -0,0 +1,75 @@
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
4
+ # Copyright (c) 2015 Webhippie <http://www.webhippie.de>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #
25
+
26
+ module GettextI18nRailsJs
27
+ class Config
28
+ attr_accessor :output_path
29
+ attr_accessor :handlebars_function
30
+ attr_accessor :javascript_function
31
+ attr_accessor :jed_options
32
+
33
+ def initialize(&block)
34
+ @output_path = defaults[:output_path]
35
+ @handlebars_function = defaults[:handlebars_function]
36
+ @javascript_function = defaults[:javascript_function]
37
+ @jed_options = defaults[:jed_options].symbolize_keys
38
+
39
+ instance_eval(&block) if block_given?
40
+ end
41
+
42
+ protected
43
+
44
+ def defaults
45
+ file = ::Rails.root.join(
46
+ "config",
47
+ "gettext_i18n_rails_js.yml"
48
+ )
49
+
50
+ values = {
51
+ output_path: File.join(
52
+ "app",
53
+ "assets",
54
+ "javascripts",
55
+ "locale"
56
+ ),
57
+ handlebars_function: "__",
58
+ javascript_function: "__",
59
+ jed_options: {
60
+ pretty: false
61
+ }
62
+ }
63
+
64
+ if file.exist?
65
+ yaml = YAML.load_file(file) || {}
66
+
67
+ values.deep_merge(
68
+ yaml
69
+ ).with_indifferent_access
70
+ else
71
+ values.with_indifferent_access
72
+ end
73
+ end
74
+ end
75
+ end
@@ -43,13 +43,13 @@ module GettextI18nRailsJs
43
43
  protected
44
44
 
45
45
  def collect_for(value)
46
- ::File.new(
47
- value
48
- ).each_line.each_with_index.collect do |line, idx|
49
- line.scan(invoke_regex).collect do |function, arguments|
50
- yield(function, arguments, idx + 1)
51
- end
52
- end.inject(:+).compact
46
+ ::File.open(value) do |f|
47
+ f.each_line.each_with_index.collect do |line, idx|
48
+ line.scan(invoke_regex).collect do |function, arguments|
49
+ yield(function, arguments, idx + 1)
50
+ end
51
+ end.inject([], :+).compact
52
+ end
53
53
  end
54
54
 
55
55
  def invoke_regex
@@ -0,0 +1,98 @@
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
4
+ # Copyright (c) 2015 Webhippie <http://www.webhippie.de>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #
25
+
26
+ module GettextI18nRailsJs
27
+ module Task
28
+ extend self
29
+
30
+ def po_to_json
31
+ GettextI18nRailsJs::Parser::Javascript
32
+ .gettext_function = GettextI18nRailsJs.config.javascript_function
33
+
34
+ GettextI18nRailsJs::Parser::Handlebars
35
+ .gettext_function = GettextI18nRailsJs.config.handlebars_function
36
+
37
+ if files_list.empty?
38
+ puts "Couldn't find PO files in #{locale_path}, run 'rake gettext:find'"
39
+ else
40
+ files_list.each do |input|
41
+ # Language is used for filenames, while language code is used as the
42
+ # in-app language code. So for instance, simplified chinese will live
43
+ # in app/assets/locale/zh_CN/app.js but inside the file the language
44
+ # will be referred to as locales['zh-CN']. This is to adapt to the
45
+ # existing gettext_rails convention.
46
+
47
+ language = input.dirname.basename.to_s
48
+ language_code = language.gsub("_", "-")
49
+
50
+ destination = output_path.join(language)
51
+ destination.mkpath
52
+
53
+ json = PoToJson.new(
54
+ input.to_s
55
+ ).generate_for_jed(
56
+ language_code,
57
+ GettextI18nRailsJs.config.jed_options
58
+ )
59
+
60
+ destination.join("app.js").open("w") do |f|
61
+ f.rewind
62
+ f.write(json)
63
+ end
64
+
65
+ puts "Created app.js in #{destination}"
66
+ end
67
+
68
+ puts
69
+ puts "All files created, make sure they are being added to your assets."
70
+ puts "If they are not, you can add them with this line (configurable):"
71
+ puts
72
+ puts "//= require_tree ./locale"
73
+ puts "//= require gettext/all"
74
+ puts
75
+ end
76
+ end
77
+
78
+ protected
79
+
80
+ def files_list
81
+ require "gettext_i18n_rails/tasks"
82
+
83
+ ::Pathname.glob(
84
+ ::File.join(
85
+ locale_path,
86
+ "**",
87
+ "*.po"
88
+ )
89
+ )
90
+ end
91
+
92
+ def output_path
93
+ ::Rails.root.join(
94
+ GettextI18nRailsJs.config.output_path
95
+ )
96
+ end
97
+ end
98
+ end
@@ -27,7 +27,7 @@ module GettextI18nRailsJs
27
27
  class Version
28
28
  MAJOR = 1
29
29
  MINOR = 0
30
- PATCH = 1
30
+ PATCH = 2
31
31
 
32
32
  PRE = nil
33
33
 
@@ -24,104 +24,12 @@
24
24
  #
25
25
 
26
26
  require "gettext_i18n_rails/tasks"
27
+ require "gettext_i18n_rails_js/task"
27
28
 
28
29
  namespace :gettext do
29
30
  desc "Convert PO files to JS files"
30
31
  task po_to_json: :environment do
31
- GettextI18nRailsJs::Parser::Javascript
32
- .gettext_function = config[:javascript_function]
33
-
34
- GettextI18nRailsJs::Parser::Handlebars
35
- .gettext_function = config[:handlebars_function]
36
-
37
- if files_list.empty?
38
- puts "Couldn't find PO files in #{locale_path}, run 'rake gettext:find'"
39
- else
40
- files_list.each do |input|
41
- # Language is used for filenames, while language code is used as the
42
- # in-app language code. So for instance, simplified chinese will live
43
- # in app/assets/locale/zh_CN/app.js but inside the file the language
44
- # will be referred to as locales['zh-CN']. This is to adapt to the
45
- # existing gettext_rails convention.
46
-
47
- language = input.dirname.basename.to_s
48
- language_code = language.gsub("_", "-")
49
-
50
- destination = output_path.join(language)
51
- destination.mkpath
52
-
53
- json = PoToJson.new(
54
- input.to_s
55
- ).generate_for_jed(
56
- language_code,
57
- config[:jed_options].symbolize_keys
58
- )
59
-
60
- destination.join("app.js").open("w") do |f|
61
- f.rewind
62
- f.write(json)
63
- end
64
-
65
- puts "Created app.js in #{destination}"
66
- end
67
-
68
- puts
69
- puts "All files created, make sure they are being added to your assets."
70
- puts "If they are not, you can add them with this line (configurable):"
71
- puts
72
- puts "//= require_tree ./locale"
73
- puts "//= require gettext/all"
74
- puts
75
- end
76
- end
77
-
78
- def files_list
79
- Pathname.glob(
80
- ::File.join(
81
- locale_path,
82
- "**",
83
- "*.po"
84
- )
85
- )
86
- end
87
-
88
- def output_path
89
- Rails.root.join(
90
- config[:output_path]
91
- )
92
- end
93
-
94
- def config
95
- @config ||= begin
96
- file = Rails.root.join(
97
- "config",
98
- "gettext_i18n_rails_js.yml"
99
- )
100
-
101
- defaults = {
102
- output_path: File.join(
103
- "app",
104
- "assets",
105
- "javascripts",
106
- "locale"
107
- ),
108
- handlebars_function: "__",
109
- javascript_function: "__",
110
- jed_options: {
111
- pretty: false
112
- }
113
- }
114
-
115
- if file.exist?
116
- yaml = YAML.load_file(file) || {}
117
-
118
- defaults.deep_merge(
119
- yaml
120
- ).with_indifferent_access
121
- else
122
- defaults.with_indifferent_access
123
- end
124
- end
32
+ GettextI18nRailsJs::Task.po_to_json
125
33
  end
126
34
 
127
35
  # Required for gettext to filter the files
@@ -236,6 +236,16 @@ describe GettextI18nRailsJs::Parser::Javascript do
236
236
  )
237
237
  end
238
238
  end
239
+
240
+ it "does not parse empty files" do
241
+ content = ""
242
+
243
+ with_file content do |path|
244
+ expect(parser.parse(path, [])).to(
245
+ eq([])
246
+ )
247
+ end
248
+ end
239
249
  end
240
250
 
241
251
  describe "parses javascript files" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gettext_i18n_rails_js
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Boerger
@@ -9,120 +9,120 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-24 00:00:00.000000000 Z
12
+ date: 2015-03-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - '>='
19
19
  - !ruby/object:Gem::Version
20
20
  version: '0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - '>='
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - '>='
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - '>='
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: yard
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - '>='
47
47
  - !ruby/object:Gem::Version
48
48
  version: '0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - '>='
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rspec
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ">="
60
+ - - '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ">="
67
+ - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rails
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ">="
74
+ - - '>='
75
75
  - !ruby/object:Gem::Version
76
76
  version: 3.2.0
77
77
  type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ">="
81
+ - - '>='
82
82
  - !ruby/object:Gem::Version
83
83
  version: 3.2.0
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: gettext
86
86
  requirement: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - ">="
88
+ - - '>='
89
89
  - !ruby/object:Gem::Version
90
90
  version: 3.0.2
91
91
  type: :runtime
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - ">="
95
+ - - '>='
96
96
  - !ruby/object:Gem::Version
97
97
  version: 3.0.2
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: gettext_i18n_rails
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - ">="
102
+ - - '>='
103
103
  - !ruby/object:Gem::Version
104
104
  version: 0.7.1
105
105
  type: :runtime
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - ">="
109
+ - - '>='
110
110
  - !ruby/object:Gem::Version
111
111
  version: 0.7.1
112
112
  - !ruby/object:Gem::Dependency
113
113
  name: po_to_json
114
114
  requirement: !ruby/object:Gem::Requirement
115
115
  requirements:
116
- - - ">="
116
+ - - '>='
117
117
  - !ruby/object:Gem::Version
118
- version: 0.1.0
118
+ version: 1.0.0
119
119
  type: :runtime
120
120
  prerelease: false
121
121
  version_requirements: !ruby/object:Gem::Requirement
122
122
  requirements:
123
- - - ">="
123
+ - - '>='
124
124
  - !ruby/object:Gem::Version
125
- version: 0.1.0
125
+ version: 1.0.0
126
126
  description: |2
127
127
  It will find translations inside your .js and .coffee files, then it will
128
128
  create JSON versions of your .PO files and will let you serve them with the
@@ -136,26 +136,28 @@ extensions: []
136
136
  extra_rdoc_files: []
137
137
  files:
138
138
  - CHANGELOG.md
139
- - LICENSE
140
139
  - README.md
141
- - lib/assets/javascripts/gettext/all.js
140
+ - LICENSE
141
+ - lib/tasks/gettext_i18n_rails_js_tasks.rake
142
142
  - lib/gettext_i18n_rails_js.rb
143
+ - lib/assets/javascripts/gettext/all.js
144
+ - lib/gettext_i18n_rails_js/version.rb
145
+ - lib/gettext_i18n_rails_js/config.rb
143
146
  - lib/gettext_i18n_rails_js/engine.rb
144
- - lib/gettext_i18n_rails_js/parser.rb
147
+ - lib/gettext_i18n_rails_js/task.rb
145
148
  - lib/gettext_i18n_rails_js/parser/base.rb
146
149
  - lib/gettext_i18n_rails_js/parser/handlebars.rb
147
150
  - lib/gettext_i18n_rails_js/parser/javascript.rb
148
- - lib/gettext_i18n_rails_js/version.rb
149
- - lib/tasks/gettext_i18n_rails_js_tasks.rake
151
+ - lib/gettext_i18n_rails_js/parser.rb
152
+ - vendor/assets/javascripts/gettext/jed.js
153
+ - spec/spec_helper.rb
150
154
  - spec/fixtures/example.coffee
151
- - spec/fixtures/example.handlebars
152
155
  - spec/fixtures/example.js
156
+ - spec/fixtures/example.handlebars
157
+ - spec/support/with_file.rb
158
+ - spec/gettext_i18n_rails_js_spec.rb
153
159
  - spec/gettext_i18n_rails_js/parser/handlebars_spec.rb
154
160
  - spec/gettext_i18n_rails_js/parser/javascript_spec.rb
155
- - spec/gettext_i18n_rails_js_spec.rb
156
- - spec/spec_helper.rb
157
- - spec/support/with_file.rb
158
- - vendor/assets/javascripts/gettext/jed.js
159
161
  homepage: https://github.com/webhippie/gettext_i18n_rails_js
160
162
  licenses:
161
163
  - MIT
@@ -166,28 +168,28 @@ require_paths:
166
168
  - lib
167
169
  required_ruby_version: !ruby/object:Gem::Requirement
168
170
  requirements:
169
- - - ">="
171
+ - - '>='
170
172
  - !ruby/object:Gem::Version
171
173
  version: 1.9.3
172
174
  required_rubygems_version: !ruby/object:Gem::Requirement
173
175
  requirements:
174
- - - ">="
176
+ - - '>='
175
177
  - !ruby/object:Gem::Version
176
178
  version: '0'
177
179
  requirements: []
178
180
  rubyforge_project:
179
- rubygems_version: 2.4.5
181
+ rubygems_version: 2.0.3
180
182
  signing_key:
181
183
  specification_version: 4
182
184
  summary: Extends gettext_i18n_rails making your .po files available to client side
183
185
  javascript as JSON
184
186
  test_files:
187
+ - spec/spec_helper.rb
185
188
  - spec/fixtures/example.coffee
186
- - spec/fixtures/example.handlebars
187
189
  - spec/fixtures/example.js
190
+ - spec/fixtures/example.handlebars
191
+ - spec/support/with_file.rb
192
+ - spec/gettext_i18n_rails_js_spec.rb
188
193
  - spec/gettext_i18n_rails_js/parser/handlebars_spec.rb
189
194
  - spec/gettext_i18n_rails_js/parser/javascript_spec.rb
190
- - spec/gettext_i18n_rails_js_spec.rb
191
- - spec/spec_helper.rb
192
- - spec/support/with_file.rb
193
195
  has_rdoc: