gettext_i18n_rails_js 0.0.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e826dcad9f42c2d8174bc464a82311b316452392
4
+ data.tar.gz: 903eea917ab8c3da12239881b08e4b535539f4d8
5
+ SHA512:
6
+ metadata.gz: d01e882ca2b2d58357277a5248a24ef865ef939ea53d2233dd7d12ca5a6aeca495240e2ea77a859d667f23a61791c7016bc6a708bd3d880848e139fbf95c3dce
7
+ data.tar.gz: e4f0055692e3a56bc5383c0d84397f921d33437680a05b6cc84ad8dc67ad2eea5557363e278a97d24625a71d26883688617158015f3bc60ef3b32f19b0656047
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## [0.1.0](https://github.com/webhippie/gettext_i18n_rails_js/releases/tag/v0.1.0) - 2015-02-24
4
+
5
+ * Transfer from github.com/nubis/gettext_i18n_rails_js
6
+ * Added TravisCI, Rubocop and Coveralls
7
+ * Updated structure to my opinionated gem style
8
+ * Changed default handlebars function to ```__```
9
+ * Added extended ```gettext_i18n_rails_js.yml```
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
4
+ Copyright (c) 2015 Webhippie <http://www.webhippie.de>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,49 +1,114 @@
1
1
  # GettextI18nRailsJs
2
- [![Build Status](https://secure.travis-ci.org/nubis/gettext_i18n_rails_js.png?branch=master)](https://travis-ci.org/nubis/gettext_i18n_rails_js)
3
2
 
4
- GettextI18nRailsJs extends [gettext_i18n_rails](https://github.com/grosser/gettext_i18n_rails) making your .po files available to client side javascript as JSON
3
+ [![Gem Version](http://img.shields.io/gem/v/gettext_i18n_rails_js.svg)](https://rubygems.org/gems/gettext_i18n_rails_js)
4
+ [![Build Status](https://secure.travis-ci.org/webhippie/gettext_i18n_rails_js.svg)](https://travis-ci.org/webhippie/gettext_i18n_rails_js)
5
+ [![Code Climate](https://codeclimate.com/github/webhippie/gettext_i18n_rails_js.svg)](https://codeclimate.com/github/webhippie/gettext_i18n_rails_js)
6
+ [![Test Coverage](https://codeclimate.com/github/webhippie/gettext_i18n_rails_js/badges/coverage.svg)](https://codeclimate.com/github/webhippie/gettext_i18n_rails_js)
7
+ [![Dependency Status](https://gemnasium.com/webhippie/gettext_i18n_rails_js.svg)](https://gemnasium.com/webhippie/gettext_i18n_rails_js)
8
+
9
+ Extends [gettext_i18n_rails](https://github.com/grosser/gettext_i18n_rails),
10
+ making your .PO files available to client side javascript as JSON. It will find
11
+ translations inside your .js, .coffee, .handlebars and .mustache files, then it
12
+ will create JSON versions of your .PO files so you can serve them with the rest
13
+ of your assets, thus letting you access all your translations offline from
14
+ client side javascript.
15
+
16
+
17
+ ## Versions
18
+
19
+ This gem is tested on the following versions:
20
+
21
+ * Ruby
22
+ * 1.9.3
23
+ * 2.0.0
24
+ * 2.1.0
25
+ * Rails
26
+ * 3.2.21
27
+ * 4.0.13
28
+ * 4.1.9
29
+ * 4.2.0
5
30
 
6
- It will find translations inside your .js and .coffee files, then it will create JSON versions of your .PO files so you can serve them with the rest of your assets, thus letting you access all your translations offline from client side javascript.
7
31
 
8
32
  ## Installation
9
33
 
10
- Requires rails 3.2 or later.
34
+ ```ruby
35
+ gem "gettext_i18n_rails_js", "~> 1.0.0"
36
+ ```
37
+
38
+
39
+ ## Usage
40
+
41
+ set up you rails application with gettext support as usual, afterwards just
42
+ execute the following rake task to export your translations to JSON:
43
+
44
+ ```bash
45
+ rake gettext:po_to_json
46
+ ```
47
+
48
+ Per default this will reconstruct the ```locale/<lang>/app.po``` structure as
49
+ javascript files inside ```app/assets/javascripts/locale/<lang>/app.js```
50
+
51
+ The gem provides the [Jed](https://github.com/SlexAxton/Jed) library to use the
52
+ generated javascript files. It also provides a global ```__``` function that
53
+ maps to `Jed#gettext`. The Jed instance used by the client side ```__```
54
+ function is pre-configured with the ```lang``` attribute specified in your main
55
+ HTML tag. Before anything, make sure your page's HTML tag includes a valid
56
+ ```lang``` attribute, for example:
57
+
58
+ ```haml
59
+ %html{ manifest: "", lang: I18n.locale }
60
+ ```
61
+
62
+ Once you're sure your page is configured with a locale, then you should add
63
+ both your javascript locale files and the provided javascripts to your
64
+ application.js
65
+
66
+ ```js
67
+ //= require_tree ./locale
68
+ //= require gettext/all
69
+ ```
70
+
71
+ The default function name is ```window.__```, to avoid conflicts with
72
+ underscore.js. If you want to alias the function to something else in your
73
+ javascript you should also instruct the javascript and coffeescript parser to
74
+ look for a different function when finding your translations within the config
75
+ file ```config/gettext_i18n_rails_js.yml```, these are valid available options:
76
+
77
+ ```yml
78
+ output_path: "app/assets/javascripts/locale"
79
+ handlebars_function: "__"
80
+ javascript_function: "__"
81
+ jed_options:
82
+ pretty: false
83
+ ```
11
84
 
12
- #### Add the following to your gemfile:
13
85
 
14
- gem 'gettext_i18n_rails_js'
86
+ ## Todo
15
87
 
16
- ## To convert your PO files into javascript files you can run:
88
+ * More deep testing against multiple Rails versions
89
+ * Extend the current test suite, especially handlebars
17
90
 
18
- rake gettext:po_to_json
19
91
 
20
- This will reconstruct the `locale/<lang>/app.po` structure as javascript files inside `app/assets/javascripts/locale/<lang>/app.js`
92
+ ## Contributing
21
93
 
22
- ## Using translations in your javascript
94
+ Fork -> Patch -> Spec -> Push -> Pull Request
23
95
 
24
- The gem provides the Jed library to use the generated javascript files. (http://slexaxton.github.com/Jed/some)
25
- It also provides a global `__` function that maps to `Jed#gettext`.
26
- The Jed instance used by the client side `__` function is pre-configured with the 'lang' specified in your main html tag.
27
- Before anything, make sure your page's html tag includes a valid 'lang' attribute, for example:
28
96
 
29
- %html{:manifest => '', :lang => "#{I18n.locale}"}
97
+ ## Authors
30
98
 
31
- Once you're sure your page is configured with a locale, then you should add both your javascript locale files and the provided javascripts to your application.js
99
+ * [Thomas Boerger](https://github.com/tboerger)
100
+ * [Nubis](https://github.com/nubis)
101
+ * [Other contributors](https://github.com/webhippie/gettext_i18n_rails_js/graphs/contributors)
32
102
 
33
- //= require_tree ./locale
34
- //= require gettext/all
35
103
 
36
- ## Avoiding conflicts with other libraries
104
+ ## License
37
105
 
38
- The default function name is 'window.__', to avoid conflicts with 'underscore.js'. If you want to alias the function to something
39
- else in your javascript you should also instruct the javascript and coffeescript parser to look for a different function
40
- when finding your translations:
106
+ MIT
41
107
 
42
- lib/tasks/gettext.rake:
43
108
 
44
- namespace :gettext do
45
- def js_gettext_function
46
- '_' #just revert to the traditional underscore.
47
- end
48
- end
109
+ ## Copyright
49
110
 
111
+ ```
112
+ Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
113
+ Copyright (c) 2015 Webhippie <http://www.webhippie.de>
114
+ ```
@@ -1,6 +1,48 @@
1
- require 'gettext_i18n_rails'
2
- require 'gettext_i18n_rails_js/engine'
3
- require 'gettext_i18n_rails_js/js_and_coffee_parser' if RUBY_PLATFORM != 'java'
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
4
+ # Copyright (c) 2015 Webhippie <http://www.webhippie.de>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #
25
+
26
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
27
+
28
+ if File.exist? ENV["BUNDLE_GEMFILE"]
29
+ require "bundler"
30
+ Bundler.setup(:default)
31
+ else
32
+ gem "rails", version: ">= 3.2.0"
33
+ gem "gettext", version: ">= 3.0.2"
34
+ gem "gettext_i18n_rails", version: ">= 0.7.1"
35
+ gem "po_to_json", version: ">= 0.1.0"
36
+ end
37
+
38
+ require "rails"
39
+ require "gettext"
40
+ require "gettext_i18n_rails"
41
+ require "po_to_json"
42
+
43
+ require_relative "gettext_i18n_rails_js/version"
44
+ require_relative "gettext_i18n_rails_js/parser"
45
+ require_relative "gettext_i18n_rails_js/engine"
4
46
 
5
47
  module GettextI18nRailsJs
6
48
  end
@@ -1,6 +1,28 @@
1
- # We need a rails engine so that the asset pipeline knows there are assets
2
- # provided by this gem
3
- require 'rails'
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
4
+ # Copyright (c) 2015 Webhippie <http://www.webhippie.de>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #
25
+
4
26
  module GettextI18nRailsJs
5
27
  module Rails
6
28
  class Engine < ::Rails::Engine
@@ -0,0 +1,33 @@
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
4
+ # Copyright (c) 2015 Webhippie <http://www.webhippie.de>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #
25
+
26
+ require_relative "parser/base"
27
+ require_relative "parser/javascript"
28
+ require_relative "parser/handlebars"
29
+
30
+ module GettextI18nRailsJs
31
+ module Parser
32
+ end
33
+ end
@@ -0,0 +1,111 @@
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
4
+ # Copyright (c) 2015 Webhippie <http://www.webhippie.de>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #
25
+
26
+ require "gettext/tools/xgettext"
27
+ require "gettext_i18n_rails/gettext_hooks"
28
+
29
+ module GettextI18nRailsJs
30
+ module Parser
31
+ module Base
32
+ extend self
33
+
34
+ # The gettext function name can be configured at the module level as
35
+ # gettext_function. This is to provide a way to avoid
36
+ # conflicts with other javascript libraries. You only need to define
37
+ # the base function name to replace "_" and all the other variants
38
+ # (s_, n_, N_) will be deduced automatically.
39
+ attr_accessor :gettext_function
40
+
41
+ def gettext_function
42
+ @gettext_function ||= "__"
43
+ end
44
+
45
+ # We're lazy and klumsy, so this is a regex based parser that looks for
46
+ # invocations of the various gettext functions. Once captured, we scan
47
+ # them once again to fetch all the function arguments. Invoke regex
48
+ # captures like this:
49
+ #
50
+ # javascript source: "#{ __('hello') } #{ __("wor)ld") }"
51
+ # matches:
52
+ # [0]: __('hello')
53
+ # [1]: __
54
+ # [2]: 'hello'
55
+ #
56
+ # javascript source: __('item', 'items', 33)
57
+ # matches:
58
+ # [0]: __('item', 'items', 33)
59
+ # [1]: __
60
+ # [2]: 'item', 'items', 33
61
+ #
62
+ # handlebars source: "{{ _ "foo"}}"
63
+ # matches:
64
+ # [0]: __('foo')
65
+ # [1]: __
66
+ # [2]: 'foo'
67
+ #
68
+ # handlebars source: "{{ _ "foo" "foos" 3}}"
69
+ # matches:
70
+ # [0]: __('foo', 'foos', 3)
71
+ # [1]: __
72
+ # [2]: 'foo', 'foos', 3
73
+ def parse(file, _msgids = [])
74
+ collect_for(file) do |function, arguments, line|
75
+ key = arguments.scan(
76
+ /('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")/
77
+ ).collect do |match|
78
+ match.first[1..-2]
79
+ end.join(separator_for(function))
80
+
81
+ next if key == ""
82
+ results_for(key, file, line)
83
+ end
84
+ end
85
+
86
+ protected
87
+
88
+ def cleanup_value(value)
89
+ value
90
+ .gsub("\n", "\n")
91
+ .gsub("\t", "\t")
92
+ .gsub("\0", "\0")
93
+ end
94
+
95
+ def separator_for(value)
96
+ if value == "n#{gettext_function}"
97
+ "\000"
98
+ else
99
+ "\004"
100
+ end
101
+ end
102
+
103
+ def results_for(key, file, line)
104
+ [
105
+ cleanup_value(key),
106
+ [file, line].join(":")
107
+ ]
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,84 @@
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
4
+ # Copyright (c) 2015 Webhippie <http://www.webhippie.de>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #
25
+
26
+ require "gettext/tools/xgettext"
27
+ require "gettext_i18n_rails/gettext_hooks"
28
+ require_relative "base"
29
+
30
+ module GettextI18nRailsJs
31
+ module Parser
32
+ module Handlebars
33
+ include Base
34
+ extend self
35
+
36
+ def target?(file)
37
+ [
38
+ /\.handlebars\Z/,
39
+ /\.handlebars.erb\Z/,
40
+ /\.hbs\Z/,
41
+ /\.hbs.erb\Z/,
42
+ /\.mustache\Z/,
43
+ /\.mustache.erb\Z/
44
+ ].any? { |regexp| file.match regexp }
45
+ end
46
+
47
+ protected
48
+
49
+ def collect_for(value)
50
+ ::File.read(
51
+ value
52
+ ).scan(invoke_regex).collect do |_whole, function, arguments|
53
+ yield(function, arguments, 1)
54
+ end.compact
55
+ end
56
+
57
+ def invoke_regex
58
+ #
59
+ # * Matches the function handlebars helper call grouping "{{"
60
+ # * A parenthesis to start the arguments to the function
61
+ # * Then double quote string
62
+ # * Remaining arguments
63
+ # * Function call closing parenthesis
64
+ #
65
+
66
+ /
67
+ \B[{]{2}(
68
+ ([snN]?#{gettext_function})
69
+ \s+
70
+ (
71
+ ".*?"
72
+ .*?
73
+ )
74
+ )
75
+ [}]{2}
76
+ /x
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ GettextI18nRails::GettextHooks.add_parser(
83
+ GettextI18nRailsJs::Parser::Handlebars
84
+ )