Skip to content

Commit

Permalink
Implement feature
Browse files Browse the repository at this point in the history
  • Loading branch information
CuddlyBunion341 committed Nov 13, 2024
1 parent 41111ed commit 45ef6c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/moirai_translation_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default class MoiraiTranslationController extends Controller {
})
.then(response => response.json())
.then(data => {
if (data?.fallback_translation?.value) {
event.target.innerText = data.fallback_translation.value
if (data?.fallback_translation) {
event.target.innerText = data.fallback_translation
}
})
.catch(error => {
Expand Down
21 changes: 20 additions & 1 deletion app/controllers/moirai/translation_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ 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
flash.discard
render json: {
fallback_translation: get_fallback_translation
}
end
format.html do
redirect_to_translation_file(translation.file_path)
end
end
return
end

Expand Down Expand Up @@ -115,5 +125,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
6 changes: 6 additions & 0 deletions test/controllers/moirai/translation_files_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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)["translation"]
end

test "update translation with non-blank new value" do
post translation_files_url, params: {translation: {key: "locales.german", locale: "de", value: "Hochdeutsch"}}

Expand Down

0 comments on commit 45ef6c4

Please sign in to comment.