<commit_before># frozen_string_literal: true

class TranslationController < ApplicationController
  # ----------------------------
  #  :section: Edit Actions
  # ----------------------------

  def edit_translations
    @lang = get_language_and_authorize_user
    @ajax = false
    @page = params[:page]
    @tag = params[:commit] == :CANCEL.l ? nil : params[:tag]
    @strings = @lang.localization_strings
    @edit_tags = tags_to_edit(@tag, @strings)
    @show_tags = tags_to_show(@page, @strings)
    get_record_maps(@lang, @show_tags.keys + @edit_tags)
    update_translations(@edit_tags) if params[:commit] == :SAVE.l
    @form = build_form(@lang, @show_tags)
  rescue StandardError => e
    raise(e) if Rails.env.test? && @lang

    flash_error(*error_message(e))
    redirect_back_or_default("/")
  end

  def edit_translations_ajax_get
    @lang = get_language_and_authorize_user
    @ajax = true
    @tag = params[:tag]
    @strings = @lang.localization_strings
    @edit_tags = tags_to_edit(@tag, @strings)
    get_record_maps(@lang, @edit_tags)
    render(partial: "form")
  rescue StandardError => e
    msg = error_message(e).join("\n")
    render(plain: msg, status: :internal_server_error)
  end

  def edit_translations_ajax_post
    @lang = get_language_and_authorize_user
    @ajax = true
    @tag = params[:tag]
    @strings = @lang.localization_strings
    @edit_tags = tags_to_edit(@tag, @strings)
    get_record_maps(@lang, @edit_tags)
    update_translations(@edit_tags)
    render(partial: "ajax_post")
  rescue StandardError => e
    @error = error_message(e).join("\n")
    render(partial: "ajax_error")
  end

  # -------------------------------
  #  :section: Supporting Methods
  # -------------------------------

  def error_message(error)
    msg = [error.to_s]
    if Rails.env.development? && @lang
      for line in error.backtrace
        break if /action_controller.*perform_action/.match?(line)

        msg << line
      end
    end
    msg
  end

  def get_language_and_authorize_user
    locale = params[:locale] || I18n.locale
    lang = Language.find_by_locale(locale)
    validate_language_and_user(locale, lang)
    lang
  end

  def validate_language_and_user(locale, lang)
    raise(:edit_translations_bad_locale.t(locale: locale)) unless lang
    raise(:edit_translations_login_required.t) unless @user

    unless @user.is_successful_contributor?
      raise(:unsuccessful_contributor_warning.t)
    end
    return unless lang.official && !reviewer?

    raise(:edit_translations_reviewer_required.t)
  end

  def get_record_maps(lang, tags)<commit_msg>Rewrite method for readability<commit_after># frozen_string_literal: true

class TranslationController < ApplicationController
  # ----------------------------
  #  :section: Edit Actions
  # ----------------------------

  def edit_translations
    @lang = get_language_and_authorize_user
    @ajax = false
    @page = params[:page]
    @tag = params[:commit] == :CANCEL.l ? nil : params[:tag]
    @strings = @lang.localization_strings
    @edit_tags = tags_to_edit(@tag, @strings)
    @show_tags = tags_to_show(@page, @strings)
    get_record_maps(@lang, @show_tags.keys + @edit_tags)
    update_translations(@edit_tags) if params[:commit] == :SAVE.l
    @form = build_form(@lang, @show_tags)
  rescue StandardError => e
    raise(e) if Rails.env.test? && @lang

    flash_error(*error_message(e))
    redirect_back_or_default("/")
  end

  def edit_translations_ajax_get
    @lang = get_language_and_authorize_user
    @ajax = true
    @tag = params[:tag]
    @strings = @lang.localization_strings
    @edit_tags = tags_to_edit(@tag, @strings)
    get_record_maps(@lang, @edit_tags)
    render(partial: "form")
  rescue StandardError => e
    msg = error_message(e).join("\n")
    render(plain: msg, status: :internal_server_error)
  end

  def edit_translations_ajax_post
    @lang = get_language_and_authorize_user
    @ajax = true
    @tag = params[:tag]
    @strings = @lang.localization_strings
    @edit_tags = tags_to_edit(@tag, @strings)
    get_record_maps(@lang, @edit_tags)
    update_translations(@edit_tags)
    render(partial: "ajax_post")
  rescue StandardError => e
    @error = error_message(e).join("\n")
    render(partial: "ajax_error")
  end

  # -------------------------------
  #  :section: Supporting Methods
  # -------------------------------

  def error_message(error)
    msg = [error.to_s]
    if Rails.env.development? && @lang
      for line in error.backtrace
        break if /action_controller.*perform_action/.match?(line)

        msg << line
      end
    end
    msg
  end

  def get_language_and_authorize_user
    locale = params[:locale] || I18n.locale
    lang = Language.find_by_locale(locale)
    validate_language_and_user(locale, lang)
    lang
  end

  def validate_language_and_user(locale, lang)
    raise(:edit_translations_bad_locale.t(locale: locale)) unless lang
    raise(:edit_translations_login_required.t) unless @user
    raise(:unsuccessful_contributor_warning.t) \
      unless @user.is_successful_contributor?
    raise(:edit_translations_reviewer_required.t) if lang.official && !reviewer?
  end

  def get_record_maps(lang, tags)
{'fraction_non_alphanumeric': 0.14942307692307694, 'fraction_numerical': 0.0007692307692307692, 'mean_word_length': 3.1475279106858056, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '399870', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1648, 'n_words': 320}