lib/i18n/js.rb in i18n-js-3.0.0.rc5 vs lib/i18n/js.rb in i18n-js-3.0.0.rc6
- old
+ new
@@ -1,26 +1,21 @@
require "i18n"
-require "FileUtils" unless defined?(FileUtils)
+require "fileutils"
module I18n
module JS
- if defined?(Rails)
+ require "i18n/js/dependencies"
+ if JS::Dependencies.rails?
require "i18n/js/middleware"
require "i18n/js/engine"
end
# deep_merge by Stefan Rusterholz, see <http://www.ruby-forum.com/topic/142809>.
MERGER = proc do |key, v1, v2|
Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : v2
end
- # Detect if Rails app has asset pipeline support.
- #
- def self.has_asset_pipeline?
- Rails.configuration.respond_to?(:assets) && Rails.configuration.assets.enabled
- end
-
# The configuration file. This defaults to the `config/i18n-js.yml` file.
#
def self.config_file
@config_file ||= "config/i18n-js.yml"
end
@@ -33,11 +28,12 @@
end
end
def self.segments_per_locale(pattern, scope)
I18n.available_locales.each_with_object({}) do |locale, segments|
- result = scoped_translations("#{locale}.#{scope}")
+ scope = [scope] unless scope.respond_to?(:each)
+ result = scoped_translations(scope.collect{|s| "#{locale}.#{s}"})
next if result.empty?
segment_name = ::I18n.interpolate(pattern,{:locale => locale})
segments[segment_name] = result
end
@@ -85,11 +81,12 @@
# Load configuration file for partial exporting and
# custom output directory
def self.config
if config?
- (YAML.load_file(config_file) || {}).with_indifferent_access
+ erb = ERB.new(File.read(config_file)).result
+ (YAML.load(erb) || {}).with_indifferent_access
else
{}
end
end
@@ -101,13 +98,14 @@
# Convert translations to JSON string and save file.
def self.save(translations, file)
FileUtils.mkdir_p File.dirname(file)
File.open(file, "w+") do |f|
- f << %(I18n.translations = );
- f << translations.to_json
- f << %(;)
+ f << %(I18n.translations || (I18n.translations = {});\n)
+ translations.each do |locale, translations_for_locale|
+ f << %(I18n.translations["#{locale}"] = #{translations_for_locale.to_json};\n);
+ end
end
end
def self.scoped_translations(scopes) # :nodoc:
result = {}
@@ -130,20 +128,20 @@
translations.each do |scope, translations|
tmp = scopes.empty? ? translations : filter(translations, scopes)
results[scope.to_sym] = tmp unless tmp.nil?
end
return results
- elsif translations.has_key?(scope.to_sym)
+ elsif translations.respond_to?(:has_key?) && translations.has_key?(scope.to_sym)
return {scope.to_sym => scopes.empty? ? translations[scope.to_sym] : filter(translations[scope.to_sym], scopes)}
end
nil
end
# Initialize and return translations
def self.translations
::I18n.backend.instance_eval do
init_translations unless initialized?
- translations
+ translations.slice(*::I18n.available_locales)
end
end
def self.deep_merge(target, hash) # :nodoc:
target.merge(hash, &MERGER)