class MnoEnterprise::Frontend::LocalesGenerator

Public Class Methods

new(path) click to toggle source
# File lib/mno_enterprise/frontend/locales_generator.rb, line 8
def initialize(path)
  @path = path
end

Public Instance Methods

generate_json() click to toggle source

Generate JSON locales

# File lib/mno_enterprise/frontend/locales_generator.rb, line 13
def generate_json
  translations_hash = get_translations

  translations_hash.each do |locale, translations|
    generate_locale(locale, translations)
  end
end

Private Instance Methods

flatten_translations(prefix, x, locale) click to toggle source

Flatten key print_translations(“”, {foo: {bar: 'baz'}}, locale)

=> locale = {'foo.bar' => 'baz'}
# File lib/mno_enterprise/frontend/locales_generator.rb, line 43
def flatten_translations(prefix, x, locale)
  if x.is_a? Hash
    if (not prefix.empty?)
      prefix += "."
    end
    x.each {|key, value|
      flatten_translations(prefix + key.to_s, value, locale)
    }
  else
    locale[prefix] = x
    locale
  end
end
generate_locale(locale_code, translation_hash) click to toggle source

Write the json file

# File lib/mno_enterprise/frontend/locales_generator.rb, line 30
def generate_locale(locale_code, translation_hash)
  locale = {}
  flatten_translations('', translation_hash, locale)

  output_file = File.join(@path, "#{locale_code}.locale.json")

  File.open(output_file, 'w') {|f| f.write(JSON.pretty_generate(locale)) }
  puts "--> Generated #{output_file}"
end
get_translations() click to toggle source

Return the I18n translation hash

# File lib/mno_enterprise/frontend/locales_generator.rb, line 24
def get_translations
  I18n.translate(:foo) # Need to do this to force I18n init
  I18n.backend.send(:translations)
end