module Dune::Dashboard::I18n
Public Instance Methods
config()
click to toggle source
Load configuration file for partial exporting and custom output directory
# File lib/dune/dashboard/i18n.rb, line 67 def config if config? (YAML.load_file(config_file) || {}).with_indifferent_access else {} end end
config?()
click to toggle source
Check if configuration file exist
# File lib/dune/dashboard/i18n.rb, line 76 def config? File.file? config_file end
config_file()
click to toggle source
# File lib/dune/dashboard/i18n.rb, line 9 def config_file Dune::Dashboard::Engine.root.join('config/ember-i18n.yml') end
default_locales_path()
click to toggle source
# File lib/dune/dashboard/i18n.rb, line 104 def default_locales_path Dir[Dune::Dashboard::Engine.root.join('config', 'ember_locales', '*.yml').to_s] end
export!()
click to toggle source
Export translations to JavaScript, considering settings from configuration file
# File lib/dune/dashboard/i18n.rb, line 23 def export! puts "Exporting translations:\n" if config[:split] translations.keys.each do |locale| if translations[:en].nil? puts 'Missing english translation' exit end puts "\nLocale: #{locale}" fallback_english_hash = flat_hash(translations[:en]) translations_hash = flat_hash(translations[locale]) if locale != :en translations_hash.each do |key, value| english_fallback = fallback_english_hash[key] if value == nil || value == "" puts " #{key} missing!" puts " taking english default: '#{english_fallback}'" translations_hash[key] = english_fallback end end end save(translations_hash, File.join(export_dir, "translations_#{locale}.js")) end else save(flat_hash(translations), File.join(export_dir, 'translations.js')) end end
export_dir()
click to toggle source
# File lib/dune/dashboard/i18n.rb, line 13 def export_dir Dune::Dashboard::Engine.root.join('app/assets/javascripts/i18n') end
flat_hash(data, prefix = '', result = {})
click to toggle source
# File lib/dune/dashboard/i18n.rb, line 51 def flat_hash(data, prefix = '', result = {}) data.each do |key, value| current_prefix = prefix.present? ? "#{prefix}.#{key}" : key if !value.is_a?(Hash) result[current_prefix] = value.respond_to?(:stringify_keys) ? value.stringify_keys : value else flat_hash(value, current_prefix, result) end end result.stringify_keys end
save(translations, file)
click to toggle source
Convert translations to JSON string and save file.
# File lib/dune/dashboard/i18n.rb, line 81 def save(translations, file) file = ::Rails.root.join(file) FileUtils.mkdir_p File.dirname(file) variable_to_assign = config.fetch(:variable, 'Ember.I18n.translations') File.open(file, 'w+') do |f| f << variable_to_assign f << ' = ' f << JSON.pretty_generate(translations).html_safe f << ';' end end
translations()
click to toggle source
Initialize and return translations
# File lib/dune/dashboard/i18n.rb, line 96 def translations ::I18n.load_path = default_locales_path ::I18n.backend.instance_eval do init_translations unless initialized? translations end end
vendor_dir()
click to toggle source
# File lib/dune/dashboard/i18n.rb, line 17 def vendor_dir Dune::Dashboard::Engine.root.join('vendor/assets/javascripts/') end