diff --git a/examples/webapp/app.py b/examples/webapp/app.py index 839e197..9daf556 100644 --- a/examples/webapp/app.py +++ b/examples/webapp/app.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 -from flask import Flask, jsonify, make_response, redirect, render_template, request +from flask import Flask, jsonify, make_response, redirect, render_template, request, send_file +import os +import tempfile import pseudol10nutil.transforms as xforms -from pseudol10nutil import PseudoL10nUtil +from pseudol10nutil import PseudoL10nUtil, POFileUtil app = Flask(__name__) appname = "pseudol10nutil" @@ -106,5 +108,69 @@ def do_pseudo_ui(): return render_template("pseudolocalize_template.html", **default_options) +@app.route(ui_base_url + "po_upload", methods=["POST"]) +def do_pseudo_po_upload(): + input_temp_file_path = None + output_temp_file_path = None + try: + if "po_file" not in request.files: + return make_response( + jsonify({"error": "400 Error: No file part in the request."}), 400 + ) + + file = request.files["po_file"] + + if file.filename == "": + return make_response( + jsonify({"error": "400 Error: No file selected for uploading."}), 400 + ) + + if not file.filename.endswith(".po"): + return make_response( + jsonify( + {"error": "400 Error: Invalid file type. Please upload a .po file."} + ), + 400, + ) + + # Create temporary files + fd_input, input_temp_file_path = tempfile.mkstemp(suffix=".po") + os.close(fd_input) # close file descriptor as pofileutil will open and close the file + fd_output, output_temp_file_path = tempfile.mkstemp(suffix=".po") + os.close(fd_output) # close file descriptor as pofileutil will open and close the file + + + file.save(input_temp_file_path) + + pofileutil = POFileUtil() + # Apply the same transforms as the UI text input for consistency + # Default to diacritics, square brackets, and padding + # This could be made configurable in the UI later if needed + current_transforms = [ + xforms.transliterate_diacritic, + xforms.pad_length, + xforms.square_brackets + ] + pofileutil.transforms = current_transforms + pofileutil.pseudolocalizefile(input_temp_file_path, output_temp_file_path) + + return send_file( + output_temp_file_path, + as_attachment=True, + download_name="pseudolocalized.po", + mimetype="application/x-po", + ) + except Exception as e: + # Log the exception e for debugging if necessary + return make_response( + jsonify({"error": f"500 Error: Internal server error during processing. {str(e)}"}), 500 + ) + finally: + if input_temp_file_path and os.path.exists(input_temp_file_path): + os.remove(input_temp_file_path) + if output_temp_file_path and os.path.exists(output_temp_file_path): + os.remove(output_temp_file_path) + + if __name__ == "__main__": app.run(host="0.0.0.0", debug=True) diff --git a/examples/webapp/templates/pseudolocalize_template.html b/examples/webapp/templates/pseudolocalize_template.html index c8c9517..8ec96e0 100644 --- a/examples/webapp/templates/pseudolocalize_template.html +++ b/examples/webapp/templates/pseudolocalize_template.html @@ -48,5 +48,13 @@