module TranslationHandler class TranslationsController < ApplicationController layout 'application' $element = 0 def make_html(hash, opts = {}) return if !hash.is_a?(Hash) indent_level = opts.fetch(:indent_level) { 0 } out = " " * indent_level + "" end def prepare_yml_hash(hash) return if !hash.is_a?(Hash) hash.each do |key, value| if value.is_a?(Hash) prepare_yml_hash(value) else $element += 1 hash[key] = params["field_#{$element}"] end end end def update base_locale_file = YAML.load_file(Rails.root + "config/locales/#{params[:id]}.yml").to_hash $element = 0 prepared_yaml = prepare_yml_hash(base_locale_file).to_yaml File.open(Rails.root + "config/locales/#{params[:id]}.yml", "w") do |f| f << prepared_yaml end redirect_to translation_handler_translations_url, :notice => "#{params[:id]} updated Successfully !" end def index @available_locales = [] Dir.glob("#{Rails.root}/config/locales/*.yml").each do |locale| @available_locales << locale.split('/').last.split('.yml').first end end def edit base_locale_file = YAML.load_file(Rails.root + "config/locales/#{params[:id]}.yml").to_hash $element = 0 @html = make_html(base_locale_file, {}) end def new end def create filename = params[:language] base_locale_file = YAML.load_file(Rails.root + "config/locales/en.yml").to_hash["en"] prepared_yaml = {filename => base_locale_file}.to_yaml File.open(Rails.root + "config/locales/#{params[:language]}.yml", "w") do |f| f << prepared_yaml end redirect_to translation_handler_translations_url, :notice => "#{params[:language]} created Successfully !" end end end