Advertisement
atom1533163153

Untitled

Mar 5th, 2025
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | Software | 0 0
  1. from deep_translator import GoogleTranslator  # type: ignore
  2. from tqdm import tqdm  # ใช้สำหรับแสดงแถบแสดงความคืบหน้า
  3.  
  4. # กำหนดไฟล์ต้นฉบับและผลลัพธ์
  5. input_file = "en_US.lang"
  6. output_file = "th_TH.lang"
  7.  
  8. def translate_lang_file(input_file, output_file):
  9.     try:
  10.         with open(input_file, "r", encoding="utf-8") as infile:
  11.             lines = infile.readlines()
  12.  
  13.         translated_lines = []
  14.         total_lines = len(lines)
  15.  
  16.         # ใช้ tqdm เพื่อแสดงแถบความคืบหน้า
  17.         with tqdm(total=total_lines, desc="Translating", unit="line") as pbar:
  18.             for line in lines:
  19.                 # ข้ามบรรทัดคอมเมนต์หรือบรรทัดว่าง
  20.                 if line.strip().startswith("##") or not line.strip():
  21.                     translated_lines.append(line)
  22.                     pbar.update(1)
  23.                     continue
  24.  
  25.                 # ตรวจสอบว่ามี key=value หรือไม่
  26.                 if "=" in line:
  27.                     key, value = line.split("=", 1)
  28.  
  29.                     # แปลเฉพาะค่าภาษาอังกฤษเป็นภาษาไทย
  30.                     translated_value = GoogleTranslator(source='en', target='th').translate(value.strip())
  31.                     translated_lines.append(f"{key}={translated_value}\n")
  32.                 else:
  33.                     translated_lines.append(line)
  34.  
  35.                 pbar.update(1)  # อัปเดตความคืบหน้า
  36.  
  37.         # เขียนผลลัพธ์ลงไฟล์ใหม่
  38.         with open(output_file, "w", encoding="utf-8") as outfile:
  39.             outfile.writelines(translated_lines)
  40.  
  41.         print(f"แปลไฟล์สำเร็จ: {output_file}")
  42.  
  43.     except Exception as e:
  44.         print(f"เกิดข้อผิดพลาด: {e}")
  45.  
  46. # เรียกใช้ฟังก์ชัน
  47. translate_lang_file(input_file, output_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement