Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from deep_translator import GoogleTranslator # type: ignore
- from tqdm import tqdm # ใช้สำหรับแสดงแถบแสดงความคืบหน้า
- # กำหนดไฟล์ต้นฉบับและผลลัพธ์
- input_file = "en_US.lang"
- output_file = "th_TH.lang"
- def translate_lang_file(input_file, output_file):
- try:
- with open(input_file, "r", encoding="utf-8") as infile:
- lines = infile.readlines()
- translated_lines = []
- total_lines = len(lines)
- # ใช้ tqdm เพื่อแสดงแถบความคืบหน้า
- with tqdm(total=total_lines, desc="Translating", unit="line") as pbar:
- for line in lines:
- # ข้ามบรรทัดคอมเมนต์หรือบรรทัดว่าง
- if line.strip().startswith("##") or not line.strip():
- translated_lines.append(line)
- pbar.update(1)
- continue
- # ตรวจสอบว่ามี key=value หรือไม่
- if "=" in line:
- key, value = line.split("=", 1)
- # แปลเฉพาะค่าภาษาอังกฤษเป็นภาษาไทย
- translated_value = GoogleTranslator(source='en', target='th').translate(value.strip())
- translated_lines.append(f"{key}={translated_value}\n")
- else:
- translated_lines.append(line)
- pbar.update(1) # อัปเดตความคืบหน้า
- # เขียนผลลัพธ์ลงไฟล์ใหม่
- with open(output_file, "w", encoding="utf-8") as outfile:
- outfile.writelines(translated_lines)
- print(f"แปลไฟล์สำเร็จ: {output_file}")
- except Exception as e:
- print(f"เกิดข้อผิดพลาด: {e}")
- # เรียกใช้ฟังก์ชัน
- translate_lang_file(input_file, output_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement