gettext_i18n_rails_js 0.0.9 → 1.0.0

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.
@@ -1,65 +0,0 @@
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/, /\.mustache\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)
@@ -1,74 +0,0 @@
1
- require 'gettext/tools/xgettext'
2
-
3
- module GettextI18nRailsJs
4
- class JsAndCoffeeParser
5
-
6
- class << self
7
- # The gettext function name can be configured at the module level as js_gettext_function
8
- # This is to provide a way to avoid conflicts with other javascript libraries.
9
- # You only need to define the base function name to replace '_' and all the
10
- # other variants (s_, n_, N_) will be deduced automatically.
11
- attr_accessor :js_gettext_function
12
- end
13
- self.js_gettext_function = '__'
14
-
15
- def self.target?(file)
16
- ['.js', '.coffee'].include?(File.extname(file))
17
- end
18
-
19
- # We're lazy and klumsy, so this is a regex based parser that looks for
20
- # invocations of the various gettext functions. Once captured, we
21
- # scan them once again to fetch all the function arguments.
22
- # Invoke regex captures like this:
23
- # source: "#{ __('hello') } #{ __("wor)ld") }"
24
- # matches:
25
- # [0]: __('hello')
26
- # [1]: __
27
- # [2]: 'hello'
28
- #
29
- # source: __('item', 'items', 33)
30
- # matches:
31
- # [0]: __('item', 'items', 33)
32
- # [1]: __
33
- # [2]: 'item', 'items', 33
34
- def self.parse(file, msgids = [])
35
- _ = self.js_gettext_function
36
-
37
- arg_regex = /
38
- \s* # Some whitespace
39
- ('(?:[^'\\]|\\.)*?'| # A token inside the argument list, like a single quoted string
40
- "(?:[^"\\]|\\.)*?"| # ...Double quote string, both support escapes
41
- [a-zA-Z0-9_\.()]*?) # ...a number, variable name, or called function lik: 33, foo, Foo.bar()
42
- \s* # More whitespace
43
- /x
44
-
45
- # We first parse full invocations
46
- invoke_regex = /
47
- (\b[snN]?#{_}) # Matches the function call grouping the method used (__, n__, N__, etc)
48
- \( # and a parenthesis to start the arguments to the function.
49
- ((#{arg_regex},)* # There may be many arguments to the same function call
50
- #{arg_regex})? # then the last, or only argument to the function call.
51
- \) # function call closing parenthesis
52
- /x
53
-
54
- File.new(file).each_line.each_with_index.collect do |line, idx|
55
- line.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}:#{idx+1}"]
66
- end
67
- end.inject(:+).compact
68
- end
69
-
70
- end
71
- end
72
-
73
- require 'gettext_i18n_rails/gettext_hooks'
74
- GettextI18nRails::GettextHooks.add_parser(GettextI18nRailsJs::JsAndCoffeeParser)
@@ -1,54 +0,0 @@
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
- require 'gettext_i18n_rails_js/js_and_coffee_parser'
6
-
7
- GettextI18nRailsJs::JsAndCoffeeParser.js_gettext_function = js_gettext_function
8
-
9
- po_files = Dir["#{locale_path}/**/*.po"]
10
- if po_files.empty?
11
- puts "Could not find any PO files in #{locale_path}. Run 'rake gettext:find' first."
12
- end
13
-
14
- js_locales = File.join(Rails.root, 'app', 'assets', 'javascripts', 'locale')
15
- FileUtils.makedirs(js_locales)
16
-
17
- config_file = File.join(Rails.root, 'config', 'gettext_i18n_rails_js.yml')
18
- opts = File.file?(config_file) ? YAML.load_file(config_file).symbolize_keys : {}
19
-
20
- po_files.each do |po_file|
21
- # Language is used for filenames, while language code is used
22
- # as the in-app language code. So for instance, simplified chinese will
23
- # live in app/assets/locale/zh_CN/app.js but inside the file the language
24
- # will be referred to as locales['zh-CN']
25
- # This is to adapt to the existing gettext_rails convention.
26
- language = File.basename( File.dirname(po_file) )
27
- language_code = language.gsub('_','-')
28
-
29
- destination = File.join(js_locales, language)
30
- json_string = PoToJson.new(po_file).generate_for_jed(language_code, opts)
31
-
32
- FileUtils.makedirs(destination)
33
- File.open(File.join(destination, 'app.js'), 'w'){ |file| file.write(json_string) }
34
-
35
- puts "Created app.js in #{destination}"
36
- end
37
- puts
38
- puts "All files created, make sure they are being added to your assets file."
39
- puts "If they are not, you can add them with this line:"
40
- puts "//= require_tree ./locale"
41
- puts
42
- end
43
-
44
- def files_to_translate
45
- puts 'translate these files'
46
- Dir.glob("{app,lib,config,#{locale_path}}/**/*.{rb,erb,haml,slim,js,coffee,handlebars,mustache}")
47
- end
48
-
49
- # The parser will use this as the function basename when parsing translations.
50
- def js_gettext_function
51
- '__'
52
- end
53
- end
54
-