diff --git a/app/assets/javascripts/moirai_translation_controller.js b/app/assets/javascripts/moirai_translation_controller.js index 50f0fe6..b0e8ca3 100644 --- a/app/assets/javascripts/moirai_translation_controller.js +++ b/app/assets/javascripts/moirai_translation_controller.js @@ -27,6 +27,15 @@ export default class MoiraiTranslationController extends Controller { value: event.target.innerText } }) + }) + .then(response => response.json()) + .then(data => { + if (data?.fallback_translation) { + event.target.innerText = data.fallback_translation + } + }) + .catch(error => { + console.error('Error:', error); }); } } diff --git a/app/controllers/moirai/translation_files_controller.rb b/app/controllers/moirai/translation_files_controller.rb index 3ae9b2c..a6f35dd 100644 --- a/app/controllers/moirai/translation_files_controller.rb +++ b/app/controllers/moirai/translation_files_controller.rb @@ -39,8 +39,17 @@ def open_pr def handle_update(translation) if translation_params[:value].blank? || translation_same_as_current? translation.destroy - flash.notice = "Translation #{translation.key} was successfully deleted." - redirect_to_translation_file(translation.file_path) + respond_to do |format| + format.json do + render json: { + fallback_translation: get_fallback_translation + } + end + format.html do + flash.notice = "Translation #{translation.key} was successfully deleted." + redirect_to_translation_file(translation.file_path) + end + end return end @@ -60,6 +69,10 @@ def handle_create return end + if translation_params[:value].blank? && request.format.json? + return render json: {fallback_translation: get_fallback_translation} + end + translation = Translation.new(translation_params) if translation.save @@ -115,5 +128,14 @@ def translation_same_as_current? translation_params[:value] == @file_handler.parse_file(file_paths.first)[translation_params[:key]] end + + def get_fallback_translation + file_paths = KeyFinder.new.file_paths_for(translation_params[:key], locale: translation_params[:locale]) + + return "" if file_paths.empty? + return "" unless file_paths.all? { |file_path| File.exist?(file_path) } + + @file_handler.parse_file(file_paths.first)[translation_params[:key]] + end end end diff --git a/test/controllers/moirai/translation_files_controller_test.rb b/test/controllers/moirai/translation_files_controller_test.rb index bc6da81..8584c67 100644 --- a/test/controllers/moirai/translation_files_controller_test.rb +++ b/test/controllers/moirai/translation_files_controller_test.rb @@ -63,6 +63,12 @@ class TranslationFilesControllerTest < ActionDispatch::IntegrationTest assert_equal translation_count_before, Moirai::Translation.count end + test "create translation with blank value JSON" do + post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: ""}}, as: :json + assert_response :ok + assert_equal "Deutsch", JSON.parse(response.body)["fallback_translation"] + end + # Update action tests test "update translation with blank value" do count_before = Moirai::Translation.count @@ -74,6 +80,12 @@ class TranslationFilesControllerTest < ActionDispatch::IntegrationTest assert_equal count_before - 1, Moirai::Translation.count end + test "update translation with blank value JSON" do + post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: ""}}, as: :json + assert_response :ok + assert_equal "Deutsch", JSON.parse(response.body)["fallback_translation"] + end + test "update translation with non-blank new value" do post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: "Hochdeutsch"}}