po_to_json 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +7 -1
  2. data/lib/po_to_json.rb +21 -17
  3. metadata +5 -5
data/README.md CHANGED
@@ -5,7 +5,7 @@ Convert gettext PO files to json to use in your javascript app, based po2json.pl
5
5
  Ideally you'll use this on a rake task that creates json versions of your po files, which can later be used from javascript
6
6
  with Jed ( http://slexaxton.github.com/Jed/ )
7
7
 
8
- ## Installing
8
+ ## Installing
9
9
 
10
10
  Via rubygems:
11
11
  ```ruby
@@ -27,6 +27,12 @@ json_string = PoToJson.new("#{Rails.root}/locale/es/app.po").generate_for_jed('e
27
27
  File.open("#{Rails.root}/app/assets/javascripts/locale/es/app.js",'w').write(json_string)
28
28
  ```
29
29
 
30
+ If you need a pretty json, add `:pretty => true` to `generate_for_jed`, like
31
+
32
+ ```ruby
33
+ json_string = PoToJson.new("#{Rails.root}/locale/es/app.po").generate_for_jed('es', :pretty => true)
34
+ ```
35
+
30
36
  The javascript file generated has a global 'locales' object with an attribute corresponding to the generated language:
31
37
 
32
38
  ```javascript
data/lib/po_to_json.rb CHANGED
@@ -12,7 +12,7 @@ class PoToJson
12
12
  @path_to_po = path_to_po
13
13
  end
14
14
 
15
-
15
+
16
16
  # Generates a jed-compatible js file from the current PO.
17
17
  # This include adding some wrapping structure to the translations and
18
18
  # making sure the minimum headers required by Jed are provided.
@@ -22,7 +22,7 @@ class PoToJson
22
22
  # >>> i18n = new Jed(locales['es'])
23
23
  # >>> i18n.gettext('Hello World')
24
24
  # => 'Hola Mundo'
25
- def generate_for_jed(language_code)
25
+ def generate_for_jed(language_code, opts={})
26
26
  @parsed ||= self.parse
27
27
 
28
28
  @parsed['']['lang'] = language_code
@@ -33,10 +33,14 @@ class PoToJson
33
33
  domain: 'app',
34
34
  locale_data: { app: @parsed }
35
35
  }
36
-
37
- "var locales = locales || {}; locales['#{language_code}'] = #{jed_json.to_json};"
36
+
37
+ if opts[:pretty]
38
+ "var locales = locales || {}; locales['#{language_code}'] = #{JSON.pretty_generate(jed_json)};"
39
+ else
40
+ "var locales = locales || {}; locales['#{language_code}'] = #{JSON.generate(jed_json)};"
41
+ end
38
42
  end
39
-
43
+
40
44
 
41
45
  # Messages in a PO file are defined as a series of 'key value' pairs,
42
46
  # values may span over more than one line. Each key defines an attribute
@@ -56,12 +60,12 @@ class PoToJson
56
60
  # Empty lines means we have parsed one full message
57
61
  # so far and need to flush the buffer
58
62
  when /^$/ then flush_buffer
59
-
63
+
60
64
  # These are the po file comments
61
65
  # The second '#' in the following regex is in square brackets
62
66
  # b/c it messed up my syntax highlighter, no other reason.
63
67
  when /^(#[^~]|[#]$)/ then next
64
-
68
+
65
69
  when /^(?:#~ )?msgctxt\s+(.*)/ then add_to_buffer($1, :msgctxt)
66
70
 
67
71
  when /^(?:#~ )?msgid\s+(.*)/ then add_to_buffer($1, :msgid)
@@ -75,7 +79,7 @@ class PoToJson
75
79
  when /^(?:#~ )?msgstr\[(\d+)\]\s+(.*)/ then add_to_buffer($2, "msgstr_#{$1}".to_sym)
76
80
 
77
81
  when /^(?:#~ )?"/ then add_to_buffer(line)
78
-
82
+
79
83
  else
80
84
  @errors << ["Strange line #{line}"]
81
85
  end
@@ -84,13 +88,13 @@ class PoToJson
84
88
  # In case the file did not end with a newline, we want to flush the buffer
85
89
  # one more time to write the last message too.
86
90
  flush_buffer
87
-
91
+
88
92
  # This will turn the header values into a friendlier json structure too.
89
93
  parse_header_lines
90
-
94
+
91
95
  return @parsed_values
92
96
  end
93
-
97
+
94
98
  def flush_buffer
95
99
  return unless @buffer[:msgid]
96
100
 
@@ -99,7 +103,7 @@ class PoToJson
99
103
  else
100
104
  @buffer[:msgid]
101
105
  end
102
-
106
+
103
107
  msgid_plural = if @buffer[:msgid_plural] && @buffer[:msgid_plural].size > 0
104
108
  @buffer[:msgid_plural]
105
109
  end
@@ -111,12 +115,12 @@ class PoToJson
111
115
  end
112
116
  trans.unshift(msgid_plural)
113
117
 
114
- @parsed_values[msg_ctxt_id] = trans if trans.size > 1
118
+ @parsed_values[msg_ctxt_id] = trans if trans.size > 1
115
119
 
116
120
  @buffer = {}
117
121
  @last_key_type = ""
118
122
  end
119
-
123
+
120
124
  # The buffer keeps key/value pairs for all the config options
121
125
  # defined on an entry, including the message id and value.
122
126
  # For continued lines, the key_type can be empty, so the last
@@ -143,11 +147,11 @@ class PoToJson
143
147
  @parsed_values[""] = {}
144
148
  return
145
149
  end
146
-
150
+
147
151
  headers = {}
148
152
  # Heading lines may have escaped newlines in them
149
153
  @parsed_values[""][1].split(/\\n/).each do |line|
150
- next if line.size == 0
154
+ next if line.size == 0
151
155
 
152
156
  if line =~ /(.*?):(.*)/
153
157
  key, value = $1, $2
@@ -162,7 +166,7 @@ class PoToJson
162
166
  @errors << ["Malformed header #{line}"]
163
167
  end
164
168
  end
165
-
169
+
166
170
  @parsed_values[""] = headers
167
171
  end
168
172
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: po_to_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,19 +10,19 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-18 00:00:00.000000000 Z
13
+ date: 2012-07-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
17
- requirement: &70133537073220 !ruby/object:Gem::Requirement
17
+ requirement: &70258116707580 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
21
21
  - !ruby/object:Gem::Version
22
- version: 1.7.0
22
+ version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70133537073220
25
+ version_requirements: *70258116707580
26
26
  description: Convert gettext PO files to json to use in your javascript app, based
27
27
  po2json.pl (by DuckDuckGo, Inc. http://duckduckgo.com/, Torsten Raudssus <torsten@raudss.us>.)
28
28
  email: nubis@woobiz.com.ar