gettext_i18n_rails_js 0.0.1
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/MIT-LICENSE +20 -0
- data/Rakefile +27 -0
- data/Readme.md +48 -0
- data/lib/assets/javascripts/gettext/all.js +15 -0
- data/lib/assets/javascripts/gettext/jed.js +1011 -0
- data/lib/gettext_i18n_rails_js.rb +5 -0
- data/lib/gettext_i18n_rails_js/engine.rb +9 -0
- data/lib/gettext_i18n_rails_js/js_and_coffee_parser.rb +72 -0
- data/lib/gettext_i18n_rails_js/tasks.rb +50 -0
- data/lib/gettext_i18n_rails_js/version.rb +3 -0
- data/lib/tasks/gettext_i18n_rails_js_tasks.rake +4 -0
- metadata +114 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'gettext/utils'
|
2
|
+
begin
|
3
|
+
require 'gettext/tools/rgettext'
|
4
|
+
rescue LoadError #version prior to 2.0
|
5
|
+
require 'gettext/rgettext'
|
6
|
+
end
|
7
|
+
|
8
|
+
module GettextI18nRailsJs
|
9
|
+
class JsAndCoffeeParser
|
10
|
+
|
11
|
+
class << self
|
12
|
+
# The gettext function name can be configured at the module level as js_gettext_function
|
13
|
+
# This is to provide a way to avoid conflicts with other javascript libraries.
|
14
|
+
# You only need to define the base function name to replace '_' and all the
|
15
|
+
# other variants (s_, n_, N_) will be deduced automatically.
|
16
|
+
attr_accessor :js_gettext_function
|
17
|
+
end
|
18
|
+
self.js_gettext_function = '__'
|
19
|
+
|
20
|
+
def self.target?(file)
|
21
|
+
['.js', '.coffee'].include?(File.extname(file))
|
22
|
+
end
|
23
|
+
|
24
|
+
# We're lazy and klumsy, so this is a regex based parser that looks for
|
25
|
+
# invocations of the various gettext functions. Once captured, we
|
26
|
+
# scan them once again to fetch all the function arguments.
|
27
|
+
# Invoke regex captures like this:
|
28
|
+
# source: "#{ __('hello') } #{ __("wor)ld") }"
|
29
|
+
# matches:
|
30
|
+
# [0]: __('hello')
|
31
|
+
# [1]: __
|
32
|
+
# [2]: 'hello'
|
33
|
+
#
|
34
|
+
# source: __('item', 'items', 33)
|
35
|
+
# matches:
|
36
|
+
# [0]: __('item', 'items', 33)
|
37
|
+
# [1]: __
|
38
|
+
# [2]: 'item', 'items', 33
|
39
|
+
def self.parse(file, msgids = [])
|
40
|
+
_ = self.js_gettext_function
|
41
|
+
|
42
|
+
# We first parse full invocations
|
43
|
+
invoke_regex = /
|
44
|
+
(\b[snN]?#{_}) # Matches the function call grouping the method used (__, n__, N__, etc)
|
45
|
+
\( # and a parenthesis to start the arguments to the function.
|
46
|
+
(('.*?'| # Then a token inside the argument list, like a single quoted string
|
47
|
+
".*?"| # ...Double quote string
|
48
|
+
[a-zA-Z0-9_\.()]*| # ...a number, variable name, or called function lik: 33, foo, Foo.bar()
|
49
|
+
[ ]| # ...a white space
|
50
|
+
,) # ...or a comma, which separates all of the above.
|
51
|
+
*) # There may be many arguments to the same function call.
|
52
|
+
\) # function call closing parenthesis
|
53
|
+
/x
|
54
|
+
|
55
|
+
File.read(file).scan(invoke_regex).collect do |function, arguments|
|
56
|
+
separator = function == "n#{_}" ? "\000" : "\004"
|
57
|
+
key = arguments.scan(/('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/)
|
58
|
+
.collect{|match| match.first[1..-2]}
|
59
|
+
.join(separator)
|
60
|
+
next if key == ''
|
61
|
+
key.gsub!("\n", '\n')
|
62
|
+
key.gsub!("\t", '\t')
|
63
|
+
key.gsub!("\0", '\0')
|
64
|
+
|
65
|
+
[key, "#{file}:1"]
|
66
|
+
end.compact
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
GetText::RGetText.add_parser(GettextI18nRailsJs::JsAndCoffeeParser)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
namespace :gettext do
|
2
|
+
desc "Convert PO files to js files in app/assets/locales"
|
3
|
+
task :po_to_json => :environment do
|
4
|
+
require 'po_to_json'
|
5
|
+
|
6
|
+
GettextI18nRails::JsAndCoffeeParser.js_gettext_function = js_gettext_function
|
7
|
+
|
8
|
+
po_files = Dir["#{locale_path}/**/*.po"]
|
9
|
+
if po_files.empty?
|
10
|
+
puts "Could not find any PO files in #{locale_path}. Run 'rake gettext:find' first."
|
11
|
+
end
|
12
|
+
|
13
|
+
js_locales = File.join(Rails.root, 'app', 'assets', 'javascripts', 'locale')
|
14
|
+
FileUtils.makedirs(js_locales)
|
15
|
+
|
16
|
+
po_files.each do |po_file|
|
17
|
+
# Language is used for filenames, while language code is used
|
18
|
+
# as the in-app language code. So for instance, simplified chinese will
|
19
|
+
# live in app/assets/locale/zh_CN/app.js but inside the file the language
|
20
|
+
# will be referred to as locales['zh-CN']
|
21
|
+
# This is to adapt to the existing gettext_rails convention.
|
22
|
+
language = File.basename( File.dirname(po_file) )
|
23
|
+
language_code = language.gsub('_','-')
|
24
|
+
|
25
|
+
destination = File.join(js_locales, language)
|
26
|
+
json_string = PoToJson.new(po_file).generate_for_jed(language_code)
|
27
|
+
|
28
|
+
FileUtils.makedirs(destination)
|
29
|
+
File.open(File.join(destination, 'app.js'), 'w'){ |file| file.write(json_string) }
|
30
|
+
|
31
|
+
puts "Created app.js in #{destination}"
|
32
|
+
end
|
33
|
+
puts
|
34
|
+
puts "All files created, make sure they are being added to your assets file."
|
35
|
+
puts "If they are not, you can add them with this line:"
|
36
|
+
puts "//= require_tree ./locale"
|
37
|
+
puts
|
38
|
+
end
|
39
|
+
|
40
|
+
def files_to_translate
|
41
|
+
puts 'translate these files'
|
42
|
+
Dir.glob("{app,lib,config,#{locale_path}}/**/*.{rb,erb,haml,slim,js,coffee}")
|
43
|
+
end
|
44
|
+
|
45
|
+
# The parser will use this as the function basename when parsing translations.
|
46
|
+
def js_gettext_function
|
47
|
+
'__'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gettext_i18n_rails_js
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nubis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70361670650100 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70361670650100
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: gettext
|
27
|
+
requirement: &70361670649680 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70361670649680
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: gettext_i18n_rails
|
38
|
+
requirement: &70361670649220 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70361670649220
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: &70361670648800 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70361670648800
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rspec
|
60
|
+
requirement: &70361670648300 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '2'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70361670648300
|
69
|
+
description: gettext_i18n_rails will find translations inside your .js and .coffee
|
70
|
+
files, then it will create JSON versions of your .PO files and will let you serve
|
71
|
+
them with the rest of your assets, thus letting you access all your translations
|
72
|
+
offline from client side javascript.
|
73
|
+
email: nubis@woobiz.com.ar
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- lib/assets/javascripts/gettext/all.js
|
79
|
+
- lib/assets/javascripts/gettext/jed.js
|
80
|
+
- lib/gettext_i18n_rails_js/engine.rb
|
81
|
+
- lib/gettext_i18n_rails_js/js_and_coffee_parser.rb
|
82
|
+
- lib/gettext_i18n_rails_js/tasks.rb
|
83
|
+
- lib/gettext_i18n_rails_js/version.rb
|
84
|
+
- lib/gettext_i18n_rails_js.rb
|
85
|
+
- lib/tasks/gettext_i18n_rails_js_tasks.rake
|
86
|
+
- MIT-LICENSE
|
87
|
+
- Rakefile
|
88
|
+
- Readme.md
|
89
|
+
homepage: http://github.com/nubis/gettext_i18n_rails_js
|
90
|
+
licenses: []
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.8.15
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: Extends gettext_i18n_rails making your .po files available to client side
|
113
|
+
javascript as JSON
|
114
|
+
test_files: []
|