gettext_i18n_rails_js 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +1 -1
- data/lib/gettext_i18n_rails_js.rb +1 -0
- data/lib/gettext_i18n_rails_js/handlebars_parser.rb +65 -0
- data/lib/gettext_i18n_rails_js/js_and_coffee_parser.rb +3 -3
- data/lib/gettext_i18n_rails_js/tasks.rb +3 -2
- data/lib/gettext_i18n_rails_js/version.rb +1 -1
- data/lib/tasks/gettext_i18n_rails_js_tasks.rake +1 -0
- metadata +24 -12
data/Readme.md
CHANGED
@@ -6,7 +6,7 @@ It will find translations inside your .js and .coffee files, then it will create
|
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
|
9
|
+
Requires rails 3.2 or later.
|
10
10
|
|
11
11
|
#### Add the following to your gemfile:
|
12
12
|
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'gettext/tools/xgettext'
|
2
|
+
|
3
|
+
module GettextI18nRailsJs
|
4
|
+
class HandlebarsParser
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# The gettext function name can be configured at the module level as handlebars_gettext_function
|
8
|
+
attr_accessor :handlebars_gettext_function
|
9
|
+
end
|
10
|
+
|
11
|
+
self.handlebars_gettext_function = '_'
|
12
|
+
|
13
|
+
def self.target?(file)
|
14
|
+
[/\.handlebars\Z/, /\.handlebars.erb\Z/].any? {|regexp| file.match regexp}
|
15
|
+
end
|
16
|
+
|
17
|
+
# We're lazy and klumsy, so this is a regex based parser that looks for
|
18
|
+
# invocations of the various gettext functions. Once captured, we
|
19
|
+
# scan them once again to fetch all the function arguments.
|
20
|
+
# Invoke regex captures like this:
|
21
|
+
# source: "{{ _ "foo"}}"
|
22
|
+
# matches:
|
23
|
+
# [0]: {{_ "foo"}}
|
24
|
+
# [1]: _
|
25
|
+
# [2]: "foo"
|
26
|
+
#
|
27
|
+
# source: "{{ _ "foo" "foos" 3}}"
|
28
|
+
# matches:
|
29
|
+
# [0]: {{ _ "foo" "foos" 3}}
|
30
|
+
# [1]: _
|
31
|
+
# [2]: "foo" "foos" 3'
|
32
|
+
|
33
|
+
def self.parse(file, msgids = [])
|
34
|
+
cookie = self.handlebars_gettext_function
|
35
|
+
|
36
|
+
# We first parse full invocations
|
37
|
+
invoke_regex = /
|
38
|
+
\B[{]{2}(([snN]?#{cookie}) # Matches the function handlebars helper call grouping "{{"
|
39
|
+
\s+ # and a parenthesis to start the arguments to the function.
|
40
|
+
(".*?" # Then double quote string
|
41
|
+
.*? # remaining arguments
|
42
|
+
)
|
43
|
+
)
|
44
|
+
[}]{2} # function call closing parenthesis
|
45
|
+
/x
|
46
|
+
|
47
|
+
File.read(file).scan(invoke_regex).collect do |whole, function, arguments|
|
48
|
+
separator = function == "n#{cookie}" ? "\000" : "\004"
|
49
|
+
key = arguments.scan(/('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/).
|
50
|
+
collect{|match| match.first[1..-2]}.
|
51
|
+
join(separator)
|
52
|
+
next if key == ''
|
53
|
+
key.gsub!("\n", '\n')
|
54
|
+
key.gsub!("\t", '\t')
|
55
|
+
key.gsub!("\0", '\0')
|
56
|
+
|
57
|
+
[key, "#{file}:1"]
|
58
|
+
end.compact
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
require 'gettext_i18n_rails/gettext_hooks'
|
65
|
+
GettextI18nRails::GettextHooks.add_parser(GettextI18nRailsJs::HandlebarsParser)
|
@@ -49,9 +49,9 @@ module GettextI18nRailsJs
|
|
49
49
|
|
50
50
|
File.read(file).scan(invoke_regex).collect do |function, arguments|
|
51
51
|
separator = function == "n#{_}" ? "\000" : "\004"
|
52
|
-
key = arguments.scan(/('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/)
|
53
|
-
|
54
|
-
|
52
|
+
key = arguments.scan(/('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/).
|
53
|
+
collect{|match| match.first[1..-2]}.
|
54
|
+
join(separator)
|
55
55
|
next if key == ''
|
56
56
|
key.gsub!("\n", '\n')
|
57
57
|
key.gsub!("\t", '\t')
|
@@ -2,8 +2,9 @@ namespace :gettext do
|
|
2
2
|
desc "Convert PO files to js files in app/assets/locales"
|
3
3
|
task :po_to_json => :environment do
|
4
4
|
require 'po_to_json'
|
5
|
+
require 'gettext_i18n_rails_js/js_and_coffee_parser'
|
5
6
|
|
6
|
-
|
7
|
+
GettextI18nRailsJs::JsAndCoffeeParser.js_gettext_function = js_gettext_function
|
7
8
|
|
8
9
|
po_files = Dir["#{locale_path}/**/*.po"]
|
9
10
|
if po_files.empty?
|
@@ -39,7 +40,7 @@ namespace :gettext do
|
|
39
40
|
|
40
41
|
def files_to_translate
|
41
42
|
puts 'translate these files'
|
42
|
-
Dir.glob("{app,lib,config,#{locale_path}}/**/*.{rb,erb,haml,slim,js,coffee}")
|
43
|
+
Dir.glob("{app,lib,config,#{locale_path}}/**/*.{rb,erb,haml,slim,js,coffee,handlebars}")
|
43
44
|
end
|
44
45
|
|
45
46
|
# The parser will use this as the function basename when parsing translations.
|
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: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70327314868040 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.2.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70327314868040
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: gettext
|
27
|
-
requirement: &
|
27
|
+
requirement: &70327314866780 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.3.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70327314866780
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: gettext_i18n_rails
|
38
|
-
requirement: &
|
38
|
+
requirement: &70327314882420 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,21 @@ dependencies:
|
|
43
43
|
version: 0.7.1
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70327314882420
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: po_to_json
|
49
|
+
requirement: &70327314881920 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70327314881920
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: sqlite3
|
49
|
-
requirement: &
|
60
|
+
requirement: &70327314881000 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ! '>='
|
@@ -54,10 +65,10 @@ dependencies:
|
|
54
65
|
version: '0'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *70327314881000
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: rspec
|
60
|
-
requirement: &
|
71
|
+
requirement: &70327314880040 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ~>
|
@@ -65,7 +76,7 @@ dependencies:
|
|
65
76
|
version: '2'
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *70327314880040
|
69
80
|
description: gettext_i18n_rails will find translations inside your .js and .coffee
|
70
81
|
files, then it will create JSON versions of your .PO files and will let you serve
|
71
82
|
them with the rest of your assets, thus letting you access all your translations
|
@@ -78,6 +89,7 @@ files:
|
|
78
89
|
- lib/assets/javascripts/gettext/all.js
|
79
90
|
- lib/assets/javascripts/gettext/jed.js
|
80
91
|
- lib/gettext_i18n_rails_js/engine.rb
|
92
|
+
- lib/gettext_i18n_rails_js/handlebars_parser.rb
|
81
93
|
- lib/gettext_i18n_rails_js/js_and_coffee_parser.rb
|
82
94
|
- lib/gettext_i18n_rails_js/tasks.rb
|
83
95
|
- lib/gettext_i18n_rails_js/version.rb
|