po_to_json 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.md +36 -0
- data/lib/po_to_json.rb +127 -0
- metadata +60 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 Dropmysite.com. https://dropmyemail.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
## po_to_json
|
2
|
+
|
3
|
+
Convert gettext PO files to json to use in your javascript app, based po2json.pl (by DuckDuckGo, Inc. http://duckduckgo.com/, Torsten Raudssus <torsten@raudss.us>.
|
4
|
+
|
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
|
+
with Jed ( http://slexaxton.github.com/Jed/ )
|
7
|
+
|
8
|
+
## Installing
|
9
|
+
|
10
|
+
Via rubygems:
|
11
|
+
```ruby
|
12
|
+
gem install 'po_to_json'
|
13
|
+
```
|
14
|
+
|
15
|
+
In your gemfile:
|
16
|
+
```ruby
|
17
|
+
gem 'po_to_json'
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Simply parses a po file generating the corresponding JSON encoded string.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'po_to_json'
|
26
|
+
json_string = PoToJson.new.parse_po('/path/to/your/translations.po')
|
27
|
+
```
|
28
|
+
|
29
|
+
## Maintainers
|
30
|
+
|
31
|
+
* eromirou (https://github.com/eromirou)
|
32
|
+
* Nubis (https://github.com/nubis)
|
33
|
+
|
34
|
+
## License
|
35
|
+
|
36
|
+
MIT License. Copyright 2012 Dropmysite.com. https://dropmyemail.com
|
data/lib/po_to_json.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class PoToJson
|
4
|
+
|
5
|
+
CONTEXT_GLUE = '|'
|
6
|
+
|
7
|
+
def parse_po(path_to_po)
|
8
|
+
@parsed_values = {}
|
9
|
+
@buffer = {}
|
10
|
+
@last_key_type = ""
|
11
|
+
@errors = []
|
12
|
+
File.foreach(path_to_po) do |line|
|
13
|
+
line = line.chomp
|
14
|
+
case line
|
15
|
+
# Empty lines means we have parsed one full message
|
16
|
+
# so far and need to flush the buffer
|
17
|
+
when /^$/ then flush_buffer
|
18
|
+
|
19
|
+
# These are the po file comments
|
20
|
+
# The second '#' in the following regex is in square brackets
|
21
|
+
# b/c it messed up my syntax highlighter, no other reason.
|
22
|
+
when /^(#[^~]|[#]$)/ then next
|
23
|
+
|
24
|
+
when /^(?:#~ )?msgctxt\s+(.*)/ then add_to_buffer($1, :msgctxt)
|
25
|
+
|
26
|
+
when /^(?:#~ )?msgid\s+(.*)/ then add_to_buffer($1, :msgid)
|
27
|
+
|
28
|
+
when /^(?:#~ )?msgid_plural\s+(.*)/ then add_to_buffer($1, :msgid_plural)
|
29
|
+
|
30
|
+
when /^(?:#~ )?msgstr\s+(.*)/ then add_to_buffer($1, :msgstr_0)
|
31
|
+
|
32
|
+
when /^(?:#~ )?msgstr\[0\]\s+(.*)/ then add_to_buffer($1, :msgstr_0)
|
33
|
+
|
34
|
+
when /^(?:#~ )?msgstr\[(\d+)\]\s+(.*)/ then add_to_buffer($2, "msgstr_#{$1}".to_sym)
|
35
|
+
|
36
|
+
when /^(?:#~ )?"/ then add_to_buffer(line)
|
37
|
+
|
38
|
+
else
|
39
|
+
@errors << ["Strange line #{line}"]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# In case the file did not end with a newline, we want to flush the buffer
|
44
|
+
# one more time to write the last message too.
|
45
|
+
flush_buffer
|
46
|
+
|
47
|
+
# This will turn the header values into a friendlier json structure too.
|
48
|
+
parse_header_lines
|
49
|
+
|
50
|
+
return @parsed_values.to_json
|
51
|
+
end
|
52
|
+
|
53
|
+
def flush_buffer
|
54
|
+
return unless @buffer[:msgid]
|
55
|
+
|
56
|
+
msg_ctxt_id = if @buffer[:msgctxt] && @buffer[:msgctxt].size > 0
|
57
|
+
@buffer[:msgctxt] + CONTEXT_GLUE + @buffer[:msgid]
|
58
|
+
else
|
59
|
+
@buffer[:msgid]
|
60
|
+
end
|
61
|
+
|
62
|
+
msgid_plural = if @buffer[:msgid_plural] && @buffer[:msgid_plural].size > 0
|
63
|
+
@buffer[:msgid_plural]
|
64
|
+
end
|
65
|
+
|
66
|
+
# find msgstr_* translations and push them on
|
67
|
+
trans = []
|
68
|
+
@buffer.each do |key, string|
|
69
|
+
trans[$1.to_i] = string if key.to_s =~ /^msgstr_(\d+)/
|
70
|
+
end
|
71
|
+
trans.unshift(msgid_plural)
|
72
|
+
|
73
|
+
@parsed_values[msg_ctxt_id] = trans if trans.size > 1
|
74
|
+
|
75
|
+
@buffer = {}
|
76
|
+
@last_key_type = ""
|
77
|
+
end
|
78
|
+
|
79
|
+
# The buffer keeps key/value pairs for all the config options
|
80
|
+
# defined on an entry, including the message id and value.
|
81
|
+
# For continued lines, the key_type can be empty, so the last
|
82
|
+
# used key type will be used. Also, the content will be appended
|
83
|
+
# to the last key rather than assigned.
|
84
|
+
def add_to_buffer(value, key_type = nil)
|
85
|
+
value = $1 if value =~ /^"(.*)"/
|
86
|
+
value.gsub(/\\"/, '"')
|
87
|
+
|
88
|
+
if key_type.nil?
|
89
|
+
@buffer[@last_key_type] += value
|
90
|
+
else
|
91
|
+
@buffer[key_type] = value
|
92
|
+
@last_key_type = key_type
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# The parsed values are expected to have an empty string key ("")
|
97
|
+
# which corresponds to the po file metadata defined in it's header.
|
98
|
+
# the header has information like the translator, the pluralization, etc.
|
99
|
+
# Each header line is subseqently parsed into a more usable hash.
|
100
|
+
def parse_header_lines
|
101
|
+
if @parsed_values[""].nil? || @parsed_values[""][1].nil?
|
102
|
+
@parsed_values[""] = {}
|
103
|
+
return
|
104
|
+
end
|
105
|
+
|
106
|
+
headers = {}
|
107
|
+
# Heading lines may have escaped newlines in them
|
108
|
+
@parsed_values[""][1].split(/\\n/).each do |line|
|
109
|
+
next if line.size == 0
|
110
|
+
|
111
|
+
if line =~ /(.*?):(.*)/
|
112
|
+
key, value = $1, $2
|
113
|
+
if headers[key] && headers[key].size > 0
|
114
|
+
@errors << ["Duplicate header line: #{line}"]
|
115
|
+
elsif key =~ /#-#-#-#-#/
|
116
|
+
@errors << ["Marker in header line: #{line}"]
|
117
|
+
else
|
118
|
+
headers[key] = value
|
119
|
+
end
|
120
|
+
else
|
121
|
+
@errors << ["Malformed header #{line}"]
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
@parsed_values[""] = headers
|
126
|
+
end
|
127
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: po_to_json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nubis
|
9
|
+
- eromirou
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-07-16 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: json
|
17
|
+
requirement: &70359307260220 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - =
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.7.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70359307260220
|
26
|
+
description: Convert gettext PO files to json to use in your javascript app, based
|
27
|
+
po2json.pl (by DuckDuckGo, Inc. http://duckduckgo.com/, Torsten Raudssus <torsten@raudss.us>.)
|
28
|
+
email: nubis@woobiz.com.ar
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/po_to_json.rb
|
34
|
+
- README.md
|
35
|
+
- MIT-LICENSE
|
36
|
+
homepage: https://github.com/nubis/po_to_json
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.15
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Convert gettext PO files to json
|
60
|
+
test_files: []
|