Hardcoded locale fallback to English (#582)

* Hardcoded locale fallback to English

* Fallbacks for locale in proto requests in case the locale fallback fails
This commit is contained in:
Daniel Markstedt 2021-12-28 17:44:12 -08:00 committed by GitHub
parent 2c60a848a8
commit 338b6af92c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 21 deletions

View File

@ -66,7 +66,7 @@ def list_images():
command = proto.PbCommand()
command.operation = proto.PbOperation.DEFAULT_IMAGE_FILES_INFO
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -123,7 +123,7 @@ def create_new_image(file_name, file_type, size):
command = proto.PbCommand()
command.operation = proto.PbOperation.CREATE_IMAGE
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
command.params["file"] = file_name + "." + file_type
command.params["size"] = str(size)
@ -144,7 +144,7 @@ def delete_image(file_name):
command = proto.PbCommand()
command.operation = proto.PbOperation.DELETE_IMAGE
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
command.params["file"] = file_name
@ -163,7 +163,7 @@ def rename_image(file_name, new_file_name):
command = proto.PbCommand()
command.operation = proto.PbOperation.RENAME_IMAGE
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
command.params["from"] = file_name
command.params["to"] = new_file_name

View File

@ -25,7 +25,7 @@ def get_server_info():
command = proto.PbCommand()
command.operation = proto.PbOperation.SERVER_INFO
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -84,7 +84,7 @@ def get_reserved_ids():
command = proto.PbCommand()
command.operation = proto.PbOperation.RESERVED_IDS_INFO
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -106,7 +106,7 @@ def get_network_info():
command = proto.PbCommand()
command.operation = proto.PbOperation.NETWORK_INTERFACES_INFO
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -125,7 +125,7 @@ def get_device_types():
command = proto.PbCommand()
command.operation = proto.PbOperation.DEVICE_TYPES_INFO
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -148,7 +148,7 @@ def get_image_files_info():
command = proto.PbCommand()
command.operation = proto.PbOperation.DEFAULT_IMAGE_FILES_INFO
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -177,7 +177,7 @@ def attach_image(scsi_id, **kwargs):
"""
command = proto.PbCommand()
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
devices = proto.PbDeviceDefinition()
devices.id = int(scsi_id)
@ -253,7 +253,7 @@ def detach_by_id(scsi_id, unit=None):
command.operation = proto.PbOperation.DETACH
command.devices.append(devices)
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -269,7 +269,7 @@ def detach_all():
command = proto.PbCommand()
command.operation = proto.PbOperation.DETACH_ALL
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -292,7 +292,7 @@ def eject_by_id(scsi_id, unit=None):
command.operation = proto.PbOperation.EJECT
command.devices.append(devices)
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -312,7 +312,7 @@ def list_devices(scsi_id=None, unit=None):
command = proto.PbCommand()
command.operation = proto.PbOperation.DEVICES_INFO
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
# If method is called with scsi_id parameter, return the info on those devices
# Otherwise, return the info on all attached devices
@ -390,7 +390,7 @@ def reserve_scsi_ids(reserved_scsi_ids):
command.operation = proto.PbOperation.RESERVE_IDS
command.params["ids"] = ",".join(reserved_scsi_ids)
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -408,7 +408,7 @@ def set_log_level(log_level):
command.operation = proto.PbOperation.LOG_LEVEL
command.params["level"] = str(log_level)
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -426,7 +426,7 @@ def shutdown_pi(mode):
command.operation = proto.PbOperation.SHUT_DOWN
command.params["mode"] = str(mode)
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()
@ -443,7 +443,7 @@ def is_token_auth():
command = proto.PbCommand()
command.operation = proto.PbOperation.CHECK_AUTHENTICATION
command.params["token"] = current_app.config["TOKEN"]
command.params["locale"] = session["language"]
command.params["locale"] = session["language"] or "en"
data = send_pb_command(command.SerializeToString())
result = proto.PbResult()

View File

@ -92,10 +92,13 @@ def get_locale():
try:
language = session["language"]
except KeyError:
language = None
if language is not None:
language = ""
logging.warning("The default locale could not be detected. Falling back to English.")
if language:
return language
# Hardcoded fallback to "en" when the user agent does not send an accept-language header
language = request.accept_languages.best_match(LANGUAGES) or "en"
return language
return request.accept_languages.best_match(LANGUAGES)
def get_supported_locales():