lib/i18n/assets/sprockets.rb in i18n-assets-0.0.5 vs lib/i18n/assets/sprockets.rb in i18n-assets-0.0.6
- old
+ new
@@ -1,24 +1,31 @@
unless defined?(Sprockets::LOCALIZABLE_ASSETS_REGEX)
require 'sprockets'
module Sprockets
- LOCALIZABLE_ASSETS_EXT = %w( js css )
- LOCALIZABLE_ASSETS_REGEX = Regexp.new("\\.(?:#{ LOCALIZABLE_ASSETS_EXT * '|' })")
- LOCALIZABLE_COMPILABLE_ASSETS_REGEX = Regexp.new("\\.(?:#{ LOCALIZABLE_ASSETS_EXT * '|' })\\..+$")
+ DEFAULT_LOCALIZABLE_ASSETS_PATTERN = %w( *.js *.css )
GLOBAL_ASSET_REGEX = /^(https?)?:\/\//
+ def self.localizable?(path)
+ path = path.gsub(Rails.root.to_s, '')
+
+ patterns = Rails.configuration.assets.localize || DEFAULT_LOCALIZABLE_ASSETS_PATTERN
+ patterns.any? do |pattern|
+ File.fnmatch?(pattern, path)
+ end
+ end
+
module Helpers
module RailsHelper
alias_method :asset_path_without_locale, :asset_path
# prevent asset from caching by adding timestamp
def asset_path(source, options = {})
path = asset_path_without_locale(source, options)
- if !digest_assets? && path =~ LOCALIZABLE_ASSETS_REGEX
+ if !digest_assets? && Sprockets.localizable?(path)
separator = path =~ /\?/ ? '&' : '?'
"#{ path }#{ separator }t=#{ Time.now.to_i }"
else
path
end
@@ -40,11 +47,11 @@
manifest[asset.logical_path] = digest_path
manifest[aliased_path_for(asset.logical_path)] = digest_path
end
end
- if logical_path =~ LOCALIZABLE_ASSETS_REGEX
+ if Sprockets.localizable?(logical_path)
I18n.available_locales.each do |locale|
I18n.locale = locale
process.call
end
else
@@ -64,22 +71,22 @@
alias_method :logical_path_without_locale, :logical_path
# add locale for css and js files
def logical_path
path = logical_path_without_locale
- if !Rails.env.development? && path =~ LOCALIZABLE_ASSETS_REGEX
+ if !Rails.env.development? && Sprockets.localizable?(path)
"#{ I18n.locale }/#{ path }"
else
path
end
end
protected
alias_method :dependency_fresh_without_check?, :dependency_fresh?
def dependency_fresh?(environment, dep)
- return false if Rails.configuration.assets.prevent_caching && dep.pathname.to_s =~ LOCALIZABLE_COMPILABLE_ASSETS_REGEX
+ return false if Rails.configuration.assets.prevent_caching && Sprockets.localizable?(dep.pathname.to_s)
dependency_fresh_without_check?(environment, dep)
end
end
@@ -131,10 +138,12 @@
def local_resource?(source)
source !~ Sprockets::GLOBAL_ASSET_REGEX
end
def file_localizable?(source, options)
- source =~ Sprockets::LOCALIZABLE_ASSETS_REGEX || Sprockets::LOCALIZABLE_ASSETS_EXT.include?(options.try(:[], :ext))
+ ext = options.try(:[], :ext)
+ source = "#{ source }.#{ ext }" if ext && !source.ends_with?(".#{ ext }")
+ Sprockets.localizable?(source)
end
def file_already_localized?(source)
source.starts_with?("#{ I18n.locale }/")
end