lib/i18n/js.rb in i18n-js-3.0.0.rc6 vs lib/i18n/js.rb in i18n-js-3.0.0.rc7

- old
+ new

@@ -1,30 +1,35 @@ require "i18n" require "fileutils" +require "i18n/js/utils" + module I18n module JS 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 + DEFAULT_CONFIG_PATH = "config/i18n-js.yml" + DEFAULT_EXPORT_DIR_PATH = "public/javascripts" # The configuration file. This defaults to the `config/i18n-js.yml` file. # - def self.config_file - @config_file ||= "config/i18n-js.yml" + def self.config_file_path + @config_file_path ||= DEFAULT_CONFIG_PATH end + def self.config_file_path=(new_path) + @config_file_path = new_path + end # Export translations to JavaScript, considering settings # from configuration file def self.export + export_i18n_js + translation_segments.each do |filename, translations| save(translations, filename) end end @@ -57,63 +62,59 @@ segments[options[:file]] = result unless result.empty? end end end - def self.export_dir - "public/javascripts" - end - def self.filtered_translations {}.tap do |result| translation_segments.each do |filename, translations| - deep_merge!(result, translations) + Utils.deep_merge!(result, translations) end end end def self.translation_segments if config? && config[:translations] configured_segments else - {"#{export_dir}/translations.js" => translations} + {"#{DEFAULT_EXPORT_DIR_PATH}/translations.js" => translations} end end # Load configuration file for partial exporting and # custom output directory def self.config if config? - erb = ERB.new(File.read(config_file)).result + erb = ERB.new(File.read(config_file_path)).result (YAML.load(erb) || {}).with_indifferent_access else {} end end # Check if configuration file exist def self.config? - File.file? config_file + File.file? config_file_path end # 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 || (I18n.translations = {});\n) - translations.each do |locale, translations_for_locale| + Utils.strip_keys_with_nil_values(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 = {} [scopes].flatten.each do |scope| - deep_merge! result, filter(translations, scope) + Utils.deep_merge! result, filter(translations, scope) end result end @@ -142,14 +143,29 @@ init_translations unless initialized? translations.slice(*::I18n.available_locales) end end - def self.deep_merge(target, hash) # :nodoc: - target.merge(hash, &MERGER) - end - def self.deep_merge!(target, hash) # :nodoc: - target.merge!(hash, &MERGER) + ### Export i18n.js + begin + # Copy i18n.js + def self.export_i18n_js + return if export_i18n_js_dir_path.nil? + + FileUtils.mkdir_p(export_i18n_js_dir_path) + + i18n_js_path = File.expand_path('../../../app/assets/javascripts/i18n.js', __FILE__) + FileUtils.cp(i18n_js_path, export_i18n_js_dir_path) + end + def self.export_i18n_js_dir_path + return @export_i18n_js_dir_path if defined?(@export_i18n_js_dir_path) + + @export_i18n_js_dir_path = DEFAULT_EXPORT_DIR_PATH + end + # Setting this to nil would disable i18n.js exporting + def self.export_i18n_js_dir_path=(new_path) + @export_i18n_js_dir_path = new_path + end end end end