i18n-assets 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTRhZjRlMjIxODI2MzI2NjUwYzQ0M2UwZjQwOTNjYTgwNjlmNzQwZg==
4
+ MWE5NDUyNWQxMGQ1MmIzYmY4YjMxMWI4NTJkZWJlMTQ1OTVkZjg1OQ==
5
5
  data.tar.gz: !binary |-
6
- Yzg4ZWEzZWQ4ZWE0NDZhODZlYzhiZGQwYzQwYWNkODcxYWNjNzExNA==
6
+ NzUxNjFkNTVhZDQ5YTIwN2RhZjNhMjA1NTI3ZTA1NDVlYjRmZjMyOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTE0MGE0NTk4Y2VhOTRjYzE4NDljZmNiNWI3NDE5YzY0MjAxNWEwMGZlMDEy
10
- ODg4ZDExMmQ1ODY0NmVjYzk4MzJhYjQ3OGMwMmVkN2M0YTU5ZjZmM2U5NGIz
11
- ZTJiYWQ2YjhkMjEwY2QyZDFhYmVlN2M5MmE2OWQyZTVmNjUzNmY=
9
+ OTRmNTZmNjIwNmEwOGVhMzQyNmU0MTM2M2VhMjdkYjBkY2MwNjY0MDg2NDg1
10
+ MTM0NTNmNWVjMzJkYTNmZDk0YTVkYWExMjA4ZDFiODBkM2U0NmNkZDVlMTYx
11
+ NDExZTA3ZmIxOTg0OTU3M2VhZDViZTY0NTJmMTA1MjQ2NDFhNjI=
12
12
  data.tar.gz: !binary |-
13
- OGExYWFmMTA1NWIyZTI1NmM3OGJkZDBiMDRlY2YwZjAzY2RmODQ0MTdhNzU3
14
- MjhkNDhiMDUzOTYwNjRjODViMjAwNWRmZDQ1YTgwYjc1ODFhNzIzZDY3ZDNk
15
- MTZkMzAzNTc5NDM5MTFhNDQ1NjJjMzc0ZDIwNWJiZDkwZWNmOTA=
13
+ ZmU4ZDk3YTdlN2EyNTRlNmJhMmQyYTBjNjk2OTg4MjI2NjdmY2IxZTA5Y2Y1
14
+ NDJmM2JkNzVkYzZkMTdhMjNjYTRkNDNiYWE2ZDIwYTk4NTI2MGYzY2Y0NmMy
15
+ OWUzNWY4YTE2YWQ1NDM1MjljYWNmZDlhNThjMjYxMWVkYTUwY2M=
data/.gitignore CHANGED
@@ -22,3 +22,5 @@ config/secrets.yml
22
22
 
23
23
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
24
24
  .rvmrc
25
+
26
+ /pkg
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i18n-assets (0.0.3)
5
+ actionpack (~> 3.2.18)
6
+ sprockets (~> 2.2.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (3.2.18)
12
+ activemodel (= 3.2.18)
13
+ activesupport (= 3.2.18)
14
+ builder (~> 3.0.0)
15
+ erubis (~> 2.7.0)
16
+ journey (~> 1.0.4)
17
+ rack (~> 1.4.5)
18
+ rack-cache (~> 1.2)
19
+ rack-test (~> 0.6.1)
20
+ sprockets (~> 2.2.1)
21
+ activemodel (3.2.18)
22
+ activesupport (= 3.2.18)
23
+ builder (~> 3.0.0)
24
+ activesupport (3.2.18)
25
+ i18n (~> 0.6, >= 0.6.4)
26
+ multi_json (~> 1.0)
27
+ builder (3.0.4)
28
+ erubis (2.7.0)
29
+ hike (1.2.3)
30
+ i18n (0.6.9)
31
+ journey (1.0.4)
32
+ multi_json (1.10.1)
33
+ rack (1.4.5)
34
+ rack-cache (1.2)
35
+ rack (>= 0.4)
36
+ rack-test (0.6.2)
37
+ rack (>= 1.0)
38
+ rake (10.3.2)
39
+ sprockets (2.2.2)
40
+ hike (~> 1.2)
41
+ multi_json (~> 1.0)
42
+ rack (~> 1.0)
43
+ tilt (~> 1.1, != 1.3.0)
44
+ tilt (1.4.1)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.5)
51
+ i18n-assets!
52
+ rake
data/README.md CHANGED
@@ -22,6 +22,8 @@ Just add `.erb` extension to your `.js` and `.css` files and use standard I18n h
22
22
 
23
23
  On assets precompilation localized versions of your files will be generated.
24
24
 
25
+ If you need to prevent `.js.erb` or `.css.erb` files from caching, you can add `config.assets.prevent_caching = true` to the `environments/development.rb` file.
26
+
25
27
  ## Example
26
28
 
27
29
  You can check an example usage here: https://github.com/maximgladkov/localized_assets_precompilation_example_app
data/i18n-assets.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
 
24
24
  spec.add_dependency 'sprockets', "~> 2.2.2"
25
+ spec.add_dependency 'actionpack', "~> 3.2.18"
25
26
  end
@@ -0,0 +1,140 @@
1
+ unless defined?(Sprockets::LOCALIZABLE_ASSETS_REGEX)
2
+ require 'sprockets'
3
+
4
+ module Sprockets
5
+ LOCALIZABLE_ASSETS_EXT = %w( js css )
6
+ LOCALIZABLE_ASSETS_REGEX = Regexp.new("\\.(?:#{ LOCALIZABLE_ASSETS_EXT * '|' })")
7
+ LOCALIZABLE_COMPILABLE_ASSETS_REGEX = Regexp.new("\\.(?:#{ LOCALIZABLE_ASSETS_EXT * '|' })\\..+$")
8
+
9
+ module Helpers
10
+ module RailsHelper
11
+
12
+ alias_method :asset_path_without_locale, :asset_path
13
+
14
+ # prevent asset from caching by adding timestamp
15
+ def asset_path(source, options = {})
16
+ path = asset_path_without_locale(source, options)
17
+
18
+ if path =~ LOCALIZABLE_ASSETS_REGEX
19
+ separator = path =~ /\?/ ? '&' : '?'
20
+ "#{ path }#{ separator }t=#{ Time.now.to_i }"
21
+ else
22
+ path
23
+ end
24
+ end
25
+
26
+ alias_method :path_to_asset, :asset_path
27
+
28
+ end
29
+ end
30
+
31
+ class StaticCompiler
32
+
33
+ def compile
34
+ manifest = {}
35
+ env.each_logical_path(paths) do |logical_path|
36
+ process = lambda do
37
+ if asset = env.find_asset(logical_path)
38
+ digest_path = write_asset(asset)
39
+ manifest[asset.logical_path] = digest_path
40
+ manifest[aliased_path_for(asset.logical_path)] = digest_path
41
+ end
42
+ end
43
+
44
+ if logical_path =~ LOCALIZABLE_ASSETS_REGEX
45
+ I18n.available_locales.each do |locale|
46
+ I18n.locale = locale
47
+ process.call
48
+ end
49
+ else
50
+ process.call
51
+ end
52
+ end
53
+
54
+ I18n.locale = I18n.default_locale
55
+
56
+ write_manifest(manifest) if @manifest
57
+ end
58
+
59
+ end
60
+
61
+ class Asset
62
+
63
+ alias_method :logical_path_without_locale, :logical_path
64
+
65
+ # add locale for css and js files
66
+ def logical_path
67
+ path = logical_path_without_locale
68
+ if !Rails.env.development? && path =~ LOCALIZABLE_ASSETS_REGEX
69
+ "#{ I18n.locale }/#{ path }"
70
+ else
71
+ path
72
+ end
73
+ end
74
+
75
+ protected
76
+
77
+ alias_method :dependency_fresh_without_check?, :dependency_fresh?
78
+ def dependency_fresh?(environment, dep)
79
+ return false if Rails.configuration.assets.prevent_caching && dep.pathname.to_s =~ LOCALIZABLE_COMPILABLE_ASSETS_REGEX
80
+ dependency_fresh_without_check?(environment, dep)
81
+ end
82
+
83
+ end
84
+
85
+ class Base
86
+
87
+ # set locale for asset request
88
+ def call(env)
89
+ locale = extract_locale(env['PATH_INFO'])
90
+ env['PATH_INFO'].gsub!(Regexp.new("^/#{ locale }"), '') if locale
91
+ I18n.locale = Rack::Request.new(env).params['locale'] || locale || I18n.default_locale
92
+ super
93
+ end
94
+
95
+ # add locale to assets cache key
96
+ def cache_key_for(path, options)
97
+ "#{path}:#{I18n.locale}:#{options[:bundle] ? '1' : '0'}"
98
+ end
99
+
100
+ private
101
+
102
+ def extract_locale(path)
103
+ locale = path[/^\/([a-z\-_]+?)\//, 1]
104
+ if I18n.available_locales.map(&:to_s).include? locale
105
+ locale
106
+ else
107
+ nil
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
115
+ module ActionView
116
+ class AssetPaths
117
+ alias_method :compute_public_path_without_locale, :compute_public_path
118
+
119
+ def compute_public_path(source, dir, options = nil)
120
+ source = prepend_locale(source) if file_localizable?(source, options) && !file_already_localized?(source)
121
+ compute_public_path_without_locale(source, dir, options)
122
+ end
123
+
124
+ private
125
+
126
+ def prepend_locale(source)
127
+ "#{ I18n.locale }/#{ source }"
128
+ end
129
+
130
+ def file_localizable?(source, options)
131
+ source =~ Sprockets::LOCALIZABLE_ASSETS_REGEX || Sprockets::LOCALIZABLE_ASSETS_EXT.include?(options.try(:[], :ext))
132
+ end
133
+
134
+ def file_already_localized?(source)
135
+ source.starts_with?("#{ I18n.locale }/")
136
+ end
137
+
138
+ end
139
+ end
140
+ end
@@ -1,5 +1,5 @@
1
1
  module I18n
2
2
  module Assets
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/lib/i18n/assets.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "i18n/assets/version"
2
- require "i18n/assets/manifest"
2
+ require "i18n/assets/sprockets"
3
3
 
4
4
  module I18n
5
5
  module Assets
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxim Gladkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-13 00:00:00.000000000 Z
11
+ date: 2014-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.2.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: actionpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.18
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.18
55
69
  description: Ever wanted to use rails localization helpers inside your assets? Your
56
70
  dream just came true!
57
71
  email:
@@ -62,12 +76,13 @@ extra_rdoc_files: []
62
76
  files:
63
77
  - .gitignore
64
78
  - Gemfile
79
+ - Gemfile.lock
65
80
  - LICENSE.txt
66
81
  - README.md
67
82
  - Rakefile
68
83
  - i18n-assets.gemspec
69
84
  - lib/i18n/assets.rb
70
- - lib/i18n/assets/manifest.rb
85
+ - lib/i18n/assets/sprockets.rb
71
86
  - lib/i18n/assets/version.rb
72
87
  homepage: https://github.com/maximgladkov/i18n-assets
73
88
  licenses:
@@ -94,4 +109,3 @@ signing_key:
94
109
  specification_version: 4
95
110
  summary: Rails assets localization made easy
96
111
  test_files: []
97
- has_rdoc:
@@ -1,89 +0,0 @@
1
- require 'sprockets'
2
-
3
- module Sprockets
4
- LOCALIZABLE_ASSETS_REGEX = /\.(?:js|css)/
5
-
6
- module Helpers
7
- module RailsHelper
8
-
9
- # add locale to asset_prefix, if assets compiled
10
- def asset_prefix_with_locale
11
- if Rails.env.development?
12
- Rails.application.config.assets.prefix
13
- else
14
- "#{ Rails.application.config.assets.prefix }/#{ I18n.locale }"
15
- end
16
- end
17
-
18
- alias_method :asset_path_without_locale, :asset_path
19
-
20
- # prevent asset from caching by adding timestamp
21
- def asset_path(source, options = {})
22
- assert_prefix = asset_prefix_with_locale
23
- asset_path = asset_path_without_locale(source, options)
24
-
25
- if asset_path =~ LOCALIZABLE_ASSETS_REGEX
26
- separator = asset_path =~ /\?/ ? '&' : '?'
27
- "#{ asset_path }#{ separator }locale=#{ I18n.locale }&t=#{ Time.now.to_i }"
28
- else
29
- asset_path
30
- end
31
- end
32
-
33
- alias_method :path_to_asset, :asset_path
34
-
35
- end
36
- end
37
-
38
- class StaticCompiler
39
-
40
- alias_method :compile_without_manifest, :compile
41
-
42
- # run compile for each available locale
43
- def compile(*paths)
44
- I18n.available_locales.each do |locale|
45
- env.logger.info "Compiling assets for #{ locale.upcase } locale..."
46
-
47
- I18n.locale = locale
48
-
49
- manifest = Sprockets::Manifest.new(env, target)
50
- manifest.compile *paths
51
- end
52
-
53
- I18n.locale = I18n.default_locale
54
- end
55
-
56
- end
57
-
58
- class Asset
59
-
60
- alias_method :digest_path_without_locale, :digest_path
61
-
62
- # add locale for css and js files
63
- def digest_path
64
- digest_path = digest_path_without_locale
65
- if digest_path =~ LOCALIZABLE_ASSETS_REGEX
66
- "#{ I18n.locale }/#{ digest_path }"
67
- else
68
- digest_path
69
- end
70
- end
71
-
72
- end
73
-
74
- class Base
75
-
76
- # set locale for asset request
77
- def call(env)
78
- I18n.locale = Rack::Request.new(env).params['locale'] || I18n.default_locale
79
- super
80
- end
81
-
82
- # add locale to assets cache key
83
- def cache_key_for(path, options)
84
- "#{path}:#{I18n.locale}:#{options[:bundle] ? '1' : '0'}"
85
- end
86
-
87
- end
88
-
89
- end