lib/i18n/js.rb in i18n-js-3.0.0.rc7 vs lib/i18n/js.rb in i18n-js-3.0.0.rc8
- old
+ new
@@ -4,10 +4,11 @@
require "i18n/js/utils"
module I18n
module JS
require "i18n/js/dependencies"
+ require "i18n/js/fallback_locales"
if JS::Dependencies.rails?
require "i18n/js/middleware"
require "i18n/js/engine"
end
@@ -35,10 +36,12 @@
def self.segments_per_locale(pattern, scope)
I18n.available_locales.each_with_object({}) do |locale, segments|
scope = [scope] unless scope.respond_to?(:each)
result = scoped_translations(scope.collect{|s| "#{locale}.#{s}"})
+ merge_with_fallbacks!(result, locale, scope) if use_fallbacks?
+
next if result.empty?
segment_name = ::I18n.interpolate(pattern,{:locale => locale})
segments[segment_name] = result
end
@@ -140,9 +143,31 @@
# Initialize and return translations
def self.translations
::I18n.backend.instance_eval do
init_translations unless initialized?
translations.slice(*::I18n.available_locales)
+ end
+ end
+
+ def self.use_fallbacks?
+ fallbacks != false
+ end
+
+ def self.fallbacks
+ config.fetch(:fallbacks) do
+ # default value
+ true
+ end
+ end
+
+ # deep_merge! given result with result for fallback locale
+ def self.merge_with_fallbacks!(result, locale, scope)
+ result[locale] ||= {}
+ fallback_locales = FallbackLocales.new(fallbacks, locale)
+
+ fallback_locales.each do |fallback_locale|
+ fallback_result = scoped_translations(scope.collect{|s| "#{fallback_locale}.#{s}"}) # NOTE: Duplicated code here
+ result[locale] = Utils.deep_merge(fallback_result[fallback_locale], result[locale])
end
end
### Export i18n.js