Dien-Hoa commited on
Commit
fa7018d
·
1 Parent(s): 1826c30

refactor translate 1 and multi languages

Browse files
Files changed (2) hide show
  1. main.py +2 -2
  2. translator.py +36 -21
main.py CHANGED
@@ -1,6 +1,6 @@
1
  from fasthtml.common import *
2
  from fasthtml.xtend import CheckboxX
3
- from translator import languages as VALID_LANGUAGES, process_sbv_file
4
  import tempfile
5
  from starlette.requests import Request
6
  from typing import List
@@ -72,7 +72,7 @@ def translate_and_process_sbv(content: bytes, filename: str, languages: List[str
72
  output_dir = os.path.join(temp_dir, "translations")
73
  os.makedirs(output_dir, exist_ok=True)
74
 
75
- process_sbv_file(input_file, output_dir, selected_languages)
76
 
77
  return create_translation_files(temp_dir, output_dir, filename, selected_languages)
78
  except Exception as e:
 
1
  from fasthtml.common import *
2
  from fasthtml.xtend import CheckboxX
3
+ from translator import languages as VALID_LANGUAGES, translate_to_languages
4
  import tempfile
5
  from starlette.requests import Request
6
  from typing import List
 
72
  output_dir = os.path.join(temp_dir, "translations")
73
  os.makedirs(output_dir, exist_ok=True)
74
 
75
+ translate_to_languages(input_file, output_dir, selected_languages)
76
 
77
  return create_translation_files(temp_dir, output_dir, filename, selected_languages)
78
  except Exception as e:
translator.py CHANGED
@@ -90,39 +90,53 @@ def reconstruct_sbv_content(blocks):
90
  def get_output_file_path(output_dir, base_filename, lang):
91
  return os.path.join(output_dir, f"{base_filename}_{lang}.sbv")
92
 
93
- def process_sbv_file(input_file, output_dir, selected_languages):
94
  """
95
- Process an SBV subtitle file and translate it into multiple languages.
96
-
97
- This function reads an input SBV file, parses its content, translates it into
98
- the specified languages, and saves the translated versions as separate files.
99
 
100
  Args:
101
  input_file (str): Path to the input SBV file.
102
- output_dir (str): Path to the directory where translated files will be saved.
103
- selected_languages (list): List of target languages for translation.
104
 
105
  Returns:
106
- None
107
-
108
- Side effects:
109
- - Creates the output directory if it doesn't exist.
110
- - Writes translated SBV files to the output directory.
111
- - Prints progress information to the console.
112
  """
113
  lines = read_sbv_file(input_file)
114
  base_filename = get_base_filename(input_file)
115
  create_output_directory(output_dir)
116
  blocks = parse_sbv_file(lines)
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  for lang in tqdm(selected_languages, desc="Translating languages"):
119
- print(f"\nStarting translation for language: {lang}")
120
- translated_blocks = translate_blocks(blocks, lang)
121
- output_content = reconstruct_sbv_content(translated_blocks)
122
- output_file = get_output_file_path(output_dir, base_filename, lang)
123
- write_sbv_file(output_file, output_content)
124
- print(f"Translated {lang} file saved: {output_file}")
125
-
126
  if __name__ == "__main__":
127
  parser = argparse.ArgumentParser(description="Translate SBV subtitle files to multiple languages.")
128
  parser.add_argument("input_file", help="Path to the input .sbv file")
@@ -130,4 +144,5 @@ if __name__ == "__main__":
130
  parser.add_argument("--languages", nargs="+", choices=languages, default=languages, help="Languages to translate to")
131
  args = parser.parse_args()
132
 
133
- process_sbv_file(args.input_file, args.output_dir, args.languages)
 
 
90
  def get_output_file_path(output_dir, base_filename, lang):
91
  return os.path.join(output_dir, f"{base_filename}_{lang}.sbv")
92
 
93
+ def translate_to_language(input_file, output_dir, language):
94
  """
95
+ Translate an SBV subtitle file to a single language.
 
 
 
96
 
97
  Args:
98
  input_file (str): Path to the input SBV file.
99
+ output_dir (str): Path to the directory where translated file will be saved.
100
+ language (str): Target language for translation.
101
 
102
  Returns:
103
+ str: Path to the translated output file.
 
 
 
 
 
104
  """
105
  lines = read_sbv_file(input_file)
106
  base_filename = get_base_filename(input_file)
107
  create_output_directory(output_dir)
108
  blocks = parse_sbv_file(lines)
109
 
110
+ print(f"\nStarting translation for language: {language}")
111
+ translated_blocks = translate_blocks(blocks, language)
112
+ output_content = reconstruct_sbv_content(translated_blocks)
113
+ output_file = get_output_file_path(output_dir, base_filename, language)
114
+ write_sbv_file(output_file, output_content)
115
+ print(f"Translated {language} file saved: {output_file}")
116
+ return output_file
117
+
118
+ def translate_to_languages(input_file, output_dir, selected_languages):
119
+ """
120
+ Translate an SBV subtitle file to multiple languages.
121
+
122
+ Args:
123
+ input_file (str): Path to the input SBV file.
124
+ output_dir (str): Path to the directory where translated files will be saved.
125
+ selected_languages (list): List of target languages for translation.
126
+
127
+ Returns:
128
+ list: Paths to the translated output files.
129
+ """
130
+ create_output_directory(output_dir)
131
+ translated_files = []
132
+
133
  for lang in tqdm(selected_languages, desc="Translating languages"):
134
+ output_file = translate_to_language(input_file, output_dir, lang)
135
+ translated_files.append(output_file)
136
+
137
+ return translated_files
138
+
139
+ # Update the main block to use the new functions
 
140
  if __name__ == "__main__":
141
  parser = argparse.ArgumentParser(description="Translate SBV subtitle files to multiple languages.")
142
  parser.add_argument("input_file", help="Path to the input .sbv file")
 
144
  parser.add_argument("--languages", nargs="+", choices=languages, default=languages, help="Languages to translate to")
145
  args = parser.parse_args()
146
 
147
+ translated_files = translate_to_languages(args.input_file, args.output_dir, args.languages)
148
+ print(f"\nTranslation completed. {len(translated_files)} files created.")