Class: Interpret::TranslationsController

Inherits:
BaseController show all
Defined in:
app/controllers/interpret/translations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/interpret/translations_controller.rb', line 58

def create
  @translation = Interpret::Translation.new params[:translation]

  if @translation.save
    flash[:notice] = "New translation created for #{@translation.key}"
    redirect_to :back
  else
    flash[:alert] = "Error when creating a new translation"
    redirect_to :back
  end
end

#destroyObject



70
71
72
73
74
75
76
# File 'app/controllers/interpret/translations_controller.rb', line 70

def destroy
  @translation = Interpret::Translation.find(params[:id])

  @translation.destroy
  flash[:notice] = "Translation #{@translation.key} destroyed."
  redirect_to :back
end

#editObject



32
33
34
# File 'app/controllers/interpret/translations_controller.rb', line 32

def edit
  @translation = Interpret::Translation.find(params[:id])
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/interpret/translations_controller.rb', line 10

def index
  key = params[:key]
  t = Interpret::Translation.arel_table
  if key
    @translations = Interpret::Translation.allowed.locale(I18n.locale).where(t[:key].matches("#{CGI.escape(key)}.%")).order("translations.key ASC")
    if I18n.locale != I18n.default_locale
      @references = Interpret::Translation.allowed.locale(I18n.default_locale).where(t[:key].matches("#{CGI.escape(key)}.%")).order("translations.key ASC")
    end
  else
    @translations = Interpret::Translation.allowed.locale(I18n.locale).where(t[:key].does_not_match("%.%")).order("translations.key ASC")
    if I18n.locale != I18n.default_locale
      @references = Interpret::Translation.allowed.locale(I18n.default_locale).where(t[:key].does_not_match("%.%")).order("translations.key ASC")
    end
  end

  # not show translations inside nested folders, \w avoids dots
  @translations = @translations.select{|x| x.key =~ /#{key}\.\w+$/} if key
  @references = @references.select{|x| x.key =~ /#{key}\.\w+$/} if key && @references

  @total_keys_number = Interpret::Translation.locale(I18n.locale).count
end

#updateObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/interpret/translations_controller.rb', line 36

def update
  @translation = Interpret::Translation.find(params[:id])
  old_value = @translation.value

  respond_to do |format|
    if @translation.update_attributes(params[:translation].presence || params[:interpret_translation])
      msg = ""
      msg << "By [#{current_interpret_user}]. " if current_interpret_user
      msg << "Locale: [#{@translation.locale}], key: [#{@translation.key}]. The translation has been changed from [#{old_value}] to [#{@translation.value}]"
      Interpret.logger.info msg

      format.html { redirect_to :back }
      format.xml  { head :ok }
      format.json { head :ok }
    else
      format.html { redirect_to :back }
      format.xml  { render :xml => @translation.errors, :status => :unprocessable_entity }
      format.json { render :status => :unprocessable_entity }
    end
  end
end

#welcomeObject



6
7
8
# File 'app/controllers/interpret/translations_controller.rb', line 6

def welcome
  redirect_to root_url
end