Module: Dune::Dashboard::I18n
Instance Method Summary collapse
-
#config ⇒ Object
Load configuration file for partial exporting and custom output directory.
-
#config? ⇒ Boolean
Check if configuration file exist.
- #config_file ⇒ Object
- #default_locales_path ⇒ Object
-
#export! ⇒ Object
Export translations to JavaScript, considering settings from configuration file.
- #export_dir ⇒ Object
- #flat_hash(data, prefix = '', result = {}) ⇒ Object
-
#save(translations, file) ⇒ Object
Convert translations to JSON string and save file.
-
#translations ⇒ Object
Initialize and return translations.
- #vendor_dir ⇒ Object
Instance Method Details
#config ⇒ Object
Load configuration file for partial exporting and custom output directory
67 68 69 70 71 72 73 |
# File 'lib/dune/dashboard/i18n.rb', line 67 def config if config? (YAML.load_file(config_file) || {}).with_indifferent_access else {} end end |
#config? ⇒ Boolean
Check if configuration file exist
76 77 78 |
# File 'lib/dune/dashboard/i18n.rb', line 76 def config? File.file? config_file end |
#config_file ⇒ Object
9 10 11 |
# File 'lib/dune/dashboard/i18n.rb', line 9 def config_file Dune::Dashboard::Engine.root.join('config/ember-i18n.yml') end |
#default_locales_path ⇒ Object
104 105 106 |
# 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! ⇒ Object
Export translations to JavaScript, considering settings from configuration file
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 ⇒ Object
13 14 15 |
# 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 = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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) ⇒ Object
Convert translations to JSON string and save file.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 ⇒ Object
Initialize and return translations
96 97 98 99 100 101 102 |
# 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 |