app/controllers/lolita/i18n_controller.rb in lolita-i18n-0.3.2 vs app/controllers/lolita/i18n_controller.rb in lolita-i18n-0.4.0

- old
+ new

@@ -3,37 +3,55 @@ before_filter :authenticate_lolita_user!, :set_current_locale layout "lolita/application" def index - @translation_keys=Lolita.i18n.flatten_keys + authorization_proxy.authorize!(:read, self.resource_class) + @translations = i18n_request.translations(@active_locale) + + if params[:sort] && params[:sort].to_s == "1" + @translations = i18n_request.sort_translations(@translations) + end end def update + authorization_proxy.authorize!(:update, self.resource_class) respond_to do |format| format.json do - error = false begin - Lolita::I18n::Backend.set(params[:id],params[:translation]) - rescue Lolita::I18n::Exceptions::MissingInterpolationArgument => e - error = e.to_s + if saved = i18n_request.update_key + notice(::I18n.t("lolita-i18n.Successful update")) + else + error(::I18n.t("lolita-i18n.Error")) + end + render :nothing => true, :json => {error: !saved && ::I18n.t("lolita-i18n.Error") } + rescue Lolita::I18n::Exceptions::MissingInterpolationArgument => er + render :nothing => true, :json => {error: er.to_s} + rescue Exception => e + render :nothing => true, :json => {error: "Key is not saved. Some error accured." } end - render :nothing => true, :json => {error: error} end end end private - def is_lolita_resource? - true + def lolita_mapping + Lolita.mappings[:i18n] end def next_locale ::I18n::available_locales.collect{|locale| locale if locale != ::I18n.default_locale}.compact.first end def set_current_locale - @active_locale = (params[:active_locale] || next_locale).to_sym + @active_locale = ::I18n.available_locales.include?(params[:active_locale].to_s.to_sym) ? params[:active_locale] : nil + @active_locale ||= next_locale + @active_locale = @active_locale.to_sym + @active_locale = nil if @active_locale == ::I18n.default_locale + end + + def i18n_request + @i18n_request ||= Lolita::I18n::Request.new(params) end end \ No newline at end of file