po_to_json 0.0.1 → 0.0.2
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/README.md +11 -3
- data/lib/po_to_json.rb +46 -5
- metadata +4 -4
data/README.md
CHANGED
@@ -9,7 +9,7 @@ with Jed ( http://slexaxton.github.com/Jed/ )
|
|
9
9
|
|
10
10
|
Via rubygems:
|
11
11
|
```ruby
|
12
|
-
gem install
|
12
|
+
gem install po_to_json
|
13
13
|
```
|
14
14
|
|
15
15
|
In your gemfile:
|
@@ -19,11 +19,19 @@ gem 'po_to_json'
|
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
|
-
|
22
|
+
Most common use would be to generate a Jed ready javascript file. For example, in a Rails 3 project:
|
23
23
|
|
24
24
|
```ruby
|
25
25
|
require 'po_to_json'
|
26
|
-
json_string = PoToJson.new.
|
26
|
+
json_string = PoToJson.new("#{Rails.root}/locale/es/app.po").generate_for_jed('es')
|
27
|
+
File.open("#{Rails.root}/app/assets/javascripts/locale/es/app.js",'w').write(json_string)
|
28
|
+
```
|
29
|
+
|
30
|
+
The javascript file generated has a global 'locales' object with an attribute corresponding to the generated language:
|
31
|
+
|
32
|
+
```javascript
|
33
|
+
i18n = new Jed(locales['es'])
|
34
|
+
i18n.gettext('Hello World') // Should evaluate to 'Hola Mundo'
|
27
35
|
```
|
28
36
|
|
29
37
|
## Maintainers
|
data/lib/po_to_json.rb
CHANGED
@@ -2,14 +2,55 @@ require 'json'
|
|
2
2
|
|
3
3
|
class PoToJson
|
4
4
|
|
5
|
-
CONTEXT_GLUE = '|'
|
6
5
|
|
7
|
-
|
6
|
+
# Gettext translations may be contextualized like so User|name
|
7
|
+
# The default 'GLUE' in rails gettext is '|' so we use that here too.
|
8
|
+
def initialize(path_to_po, context_glue = '|')
|
9
|
+
# Gettext translations may be contextualized like so User|name
|
10
|
+
# The default 'GLUE' in rails gettext is '|' so we use that here too.
|
11
|
+
@context_glue = context_glue
|
12
|
+
@path_to_po = path_to_po
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
# Generates a jed-compatible js file from the current PO.
|
17
|
+
# This include adding some wrapping structure to the translations and
|
18
|
+
# making sure the minimum headers required by Jed are provided.
|
19
|
+
# Jed is a js gettext library ( http://slexaxton.github.com/Jed/ )
|
20
|
+
# The generated file leaves the generated json inside a global locales
|
21
|
+
# object which you can use to instatiate Jed:
|
22
|
+
# >>> i18n = new Jed(locales['es'])
|
23
|
+
# >>> i18n.gettext('Hello World')
|
24
|
+
# => 'Hola Mundo'
|
25
|
+
def generate_for_jed(language_code)
|
26
|
+
@parsed ||= self.parse
|
27
|
+
|
28
|
+
@parsed['']['lang'] = language_code
|
29
|
+
@parsed['']['domain'] = 'messages'
|
30
|
+
@parsed['']['plural_forms'] ||= @parsed['']['Plural-Forms']
|
31
|
+
|
32
|
+
jed_json = {
|
33
|
+
domain: 'messages',
|
34
|
+
locale_data: @parsed
|
35
|
+
}
|
36
|
+
|
37
|
+
"var locales = locales || {}; locales['#{language_code}'] = #{jed_json.to_json};"
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
# Messages in a PO file are defined as a series of 'key value' pairs,
|
42
|
+
# values may span over more than one line. Each key defines an attribute
|
43
|
+
# of the message, like message id, context, pluralization options, etc.
|
44
|
+
# Each message is separated by a blank line.
|
45
|
+
# The parser reads attributes until it finds an empty line, at that point
|
46
|
+
# it saves the attributes read so far into a message and stores it in a hash
|
47
|
+
# to be later turned into a json object.
|
48
|
+
def parse
|
8
49
|
@parsed_values = {}
|
9
50
|
@buffer = {}
|
10
51
|
@last_key_type = ""
|
11
52
|
@errors = []
|
12
|
-
File.foreach(path_to_po) do |line|
|
53
|
+
File.foreach(@path_to_po) do |line|
|
13
54
|
line = line.chomp
|
14
55
|
case line
|
15
56
|
# Empty lines means we have parsed one full message
|
@@ -47,14 +88,14 @@ class PoToJson
|
|
47
88
|
# This will turn the header values into a friendlier json structure too.
|
48
89
|
parse_header_lines
|
49
90
|
|
50
|
-
return @parsed_values
|
91
|
+
return @parsed_values
|
51
92
|
end
|
52
93
|
|
53
94
|
def flush_buffer
|
54
95
|
return unless @buffer[:msgid]
|
55
96
|
|
56
97
|
msg_ctxt_id = if @buffer[:msgctxt] && @buffer[:msgctxt].size > 0
|
57
|
-
@buffer[:msgctxt] +
|
98
|
+
@buffer[:msgctxt] + @context_glue + @buffer[:msgid]
|
58
99
|
else
|
59
100
|
@buffer[:msgid]
|
60
101
|
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.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-07-
|
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: &
|
17
|
+
requirement: &70327703735720 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - =
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
version: 1.7.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70327703735720
|
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
|