2024-03-13 17:58:13 +00:00
|
|
|
import argostranslate.translate
|
|
|
|
import argostranslate.package
|
2024-03-13 20:40:34 +00:00
|
|
|
from package_mangement.note_all_packages import write_installed_packages_to_csv
|
2024-03-13 17:58:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def install_language(from_code, to_code):
|
2024-03-13 20:40:34 +00:00
|
|
|
try:
|
2024-03-13 17:58:13 +00:00
|
|
|
argostranslate.package.update_package_index()
|
|
|
|
available_packages = argostranslate.package.get_available_packages()
|
2024-03-13 20:40:34 +00:00
|
|
|
# Convert the filter result into a list
|
|
|
|
filtered_packages = list(
|
2024-03-13 17:58:13 +00:00
|
|
|
filter(
|
|
|
|
lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
|
|
|
|
)
|
2024-03-13 20:40:34 +00:00
|
|
|
)
|
|
|
|
# Check if the filtered list is empty
|
|
|
|
if not filtered_packages:
|
|
|
|
print(f"No available package for translating from {from_code} to {to_code}")
|
|
|
|
return 1
|
|
|
|
|
|
|
|
# If we have at least one package, proceed to download and install
|
|
|
|
available_package = filtered_packages[0]
|
2024-03-13 17:58:13 +00:00
|
|
|
download_path = available_package.download()
|
|
|
|
argostranslate.package.install_from_path(download_path)
|
2024-03-13 20:40:34 +00:00
|
|
|
|
|
|
|
# After successful installation, append package details to the CSV file
|
|
|
|
write_installed_packages_to_csv()
|
|
|
|
|
2024-03-13 17:58:13 +00:00
|
|
|
return 0
|
2024-03-13 20:40:34 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"An error occurred: {e}")
|
|
|
|
return e
|