Repository: kawaii-flesh/TraNX-Server
Pull request #6: Feature/cn 2
Author: login: Sorata
Created: 2025-05-30T08:42:18Z | Merged: 2025-05-30T15:31:26Z

Chinese is expanded into simplified and traditional characters


Diff:
diff --git a/.gitignore b/.gitignore
index 7a0ecf0..98c0dc6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,4 +17,5 @@ include/
 lib/
 lib64
 share/
-data
\ No newline at end of file
+data
+tranx-venv
diff --git a/server.py b/server.py
index 109e8cd..6c385b6 100644
--- a/server.py
+++ b/server.py
@@ -14,7 +14,7 @@
 import helpers
 from tnx_translator import Translator, get_translator
 
-LANGUAGE_CODES = ['eng', 'rus', 'ukr', 'deu', 'fra', 'jpn', 'kor', 'zho']
+LANGUAGE_CODES = ['eng', 'rus', 'ukr', 'deu', 'fra', 'jpn', 'kor', 'zho', 'zht']
 SAVE_DIR = "./data"
 DEFAULT_CONFIG = {
     "version": "2.2.0",
@@ -66,6 +66,8 @@ def get_config_path():
 
 def load_config():
     config_path = get_config_path()
+    config_needs_saving = False # Flag to indicate if config was created/reset
+
     if os.path.exists(config_path):
         with open(config_path, 'r') as f:
             config = json.load(f)
@@ -75,16 +77,23 @@ def load_config():
                 new_path = f"{config_path}.v{config.get('version', '1.0.0')}"
                 os.rename(config_path, new_path)
                 config = DEFAULT_CONFIG.copy()
-                with open(config_path, 'w') as new_f:
-                    json.dump(config, new_f, indent=4)
-            load_models_from_config(config)
-            return config
+                config_needs_saving = True # Mark for saving after adjustment
     else:
         config = DEFAULT_CONFIG.copy()
+        config_needs_saving = True # Mark for saving after adjustment
+
+    # Adjust paddleocr lang based on translation src_lang
+    if config["translation"]["src_lang"] in ["zho", "zht"]:
+        config["paddleocr"]["lang"] = "ch"
+    else:
+        config["paddleocr"]["lang"] = "en"
+
+    if config_needs_saving:
         with open(config_path, 'w') as f:
-            json.dump(config, f, indent=4)
-        load_models_from_config(config)
-        return config
+            json.dump(config, f, indent=4) # Save the (potentially) modified config
+
+    load_models_from_config(config)
+    return config
 
 def save_config(config):
     config_path = get_config_path()
@@ -180,17 +189,27 @@ def save_config_pid(pid):
     global global_pid
     global_pid = pid
     try:
-        new_config = request.json
-        config = load_config()
-        if 'image_processing' in new_config:
-            config['image_processing'].update(new_config['image_processing'])
-        if 'paddleocr' in new_config:
-            config['paddleocr'].update(new_config['paddleocr'])
-        if 'translation' in new_config:
-            config['translation'].update(new_config['translation'])
-        if 'text_processing' in new_config:
-            config['text_processing'].update(new_config['text_processing'])
-        save_config(config)
+        new_config_data = request.json # Renamed for clarity
+        config = load_config() # Load current config (it will have paddleocr.lang already adjusted by load_config)
+
+        # Apply updates from new_config_data
+        if 'image_processing' in new_config_data:
+            config['image_processing'].update(new_config_data['image_processing'])
+        if 'paddleocr' in new_config_data: # User might directly change paddleocr settings
+            config['paddleocr'].update(new_config_data['paddleocr'])
+        if 'text_processing' in new_config_data:
+            config['text_processing'].update(new_config_data['text_processing'])
+        
+        # Handle translation update and dependent paddleocr lang
+        if 'translation' in new_config_data:
+            config['translation'].update(new_config_data['translation'])
+            # Now, adjust paddleocr.lang based on the new src_lang
+            if config["translation"]["src_lang"] in ["zh", "cht"]:
+                config["paddleocr"]["lang"] = "ch"
+            else:
+                config["paddleocr"]["lang"] = "en"
+        
+        save_config(config) # save_config just dumps to json and calls load_models_from_config
         process_current_image()
         return jsonify({"status": "success"})
     except Exception as e:
diff --git a/templates/index.html b/templates/index.html
index 3f42a49..cf92774 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -300,7 +300,8 @@ <h3>Translation Settings</h3>
                     <option value="jpn" {% if config.translation.src_lang=='jpn' %}selected{% endif %}>Japanese</option>
                     <option value="kor" {% if config.translation.src_lang=='kor' %}selected{% endif %}>Korean</option>
                     <option value="rus" {% if config.translation.src_lang=='rus' %}selected{% endif %}>Russian</option>
-                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Simplified Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zht' %}selected{% endif %}>Traditional Chinese</option>
                     <option value="ukr" {% if config.translation.src_lang=='ukr' %}selected{% endif %}>Ukrainian
                     </option>
                 </select>
@@ -314,7 +315,8 @@ <h3>Translation Settings</h3>
                     </option>
                     <option value="kor" {% if config.translation.dest_lang=='kor' %}selected{% endif %}>Korean</option>
                     <option value="rus" {% if config.translation.dest_lang=='rus' %}selected{% endif %}>Russian</option>
-                    <option value="zho" {% if config.translation.dest_lang=='zho' %}selected{% endif %}>Chinese</option>
+                    <option value="zho" {% if config.translation.dest_lang=='zho' %}selected{% endif %}>Simplified Chinese</option>
+                    <option value="zho" {% if config.translation.dest_lang=='zht' %}selected{% endif %}>Traditional Chinese</option>
                     <option value="ukr" {% if config.translation.dest_lang=='ukr' %}selected{% endif %}>Ukrainian
                     </option>
                 </select>
diff --git a/tnx_translator/aliyun_translator.py b/tnx_translator/aliyun_translator.py
index 72af3e9..d488e15 100644
--- a/tnx_translator/aliyun_translator.py
+++ b/tnx_translator/aliyun_translator.py
@@ -27,6 +27,7 @@ def __init__(self, access_key_id: str, access_key_secret: str):
         'kor': 'ko',
         'rus': 'ru',
         'zho': 'zh',
+        'zht': 'zh-tw',
         'ukr': 'ru'  # Aliyun doesn't support Ukrainian, fallback to Russian
     }
 
diff --git a/tnx_translator/baidu_translator.py b/tnx_translator/baidu_translator.py
index 9f14b40..7378a7a 100644
--- a/tnx_translator/baidu_translator.py
+++ b/tnx_translator/baidu_translator.py
@@ -14,6 +14,7 @@ class BaiduTranslator(Translator):
         'kor': 'kor',
         'rus': 'ru',
         'zho': 'zh',
+        'zht': 'cht',
         'ukr': 'ukr'
     }
 
diff --git a/tnx_translator/tencent_translator.py b/tnx_translator/tencent_translator.py
index 35552a8..21526a1 100644
--- a/tnx_translator/tencent_translator.py
+++ b/tnx_translator/tencent_translator.py
@@ -15,6 +15,7 @@ class TencentTranslator(Translator):
         'kor': 'ko',
         'rus': 'ru',
         'zho': 'zh',
+        'zht': 'zh-TW',
         'ukr': 'uk'
     }
 
diff --git a/tnx_translator/youdao_translator.py b/tnx_translator/youdao_translator.py
index 01f614b..b6ba5dc 100644
--- a/tnx_translator/youdao_translator.py
+++ b/tnx_translator/youdao_translator.py
@@ -15,6 +15,7 @@ class YoudaoTranslator(Translator):
         'kor': 'ko',
         'rus': 'ru',
         'zho': 'zh-CHS',
+        'zht': 'zh-CHT',
         'ukr': 'uk'
     }
 


Reviews:
user: login: kawaii-flesh
state: COMMENTED
author_association: OWNER
submitted_at: 2025-05-30T12:58:12Z
user: login: Sorata
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-30T13:20:59Z
user: login: kawaii-flesh
state: COMMENTED
author_association: OWNER
submitted_at: 2025-05-30T13:35:55Z
user: login: Sorata
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-30T13:59:23Z
user: login: kawaii-flesh
state: COMMENTED
author_association: OWNER
submitted_at: 2025-05-30T15:13:17Z

Review comments:
diff_hunk: @@ -300,7 +300,8 @@ <h3>Translation Settings</h3>
                     <option value="jpn" {% if config.translation.src_lang=='jpn' %}selected{% endif %}>Japanese</option>
                     <option value="kor" {% if config.translation.src_lang=='kor' %}selected{% endif %}>Korean</option>
                     <option value="rus" {% if config.translation.src_lang=='rus' %}selected{% endif %}>Russian</option>
-                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Simplified Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zht' %}selected{% endif %}>Traditional Chinese</option>
path: templates/index.html
user: login: kawaii-flesh
body: Please add Traditional Chinese to the OCR section
created_at: 2025-05-30T12:58:12Z
author_association: OWNER
diff_hunk: @@ -300,7 +300,8 @@ <h3>Translation Settings</h3>
                     <option value="jpn" {% if config.translation.src_lang=='jpn' %}selected{% endif %}>Japanese</option>
                     <option value="kor" {% if config.translation.src_lang=='kor' %}selected{% endif %}>Korean</option>
                     <option value="rus" {% if config.translation.src_lang=='rus' %}selected{% endif %}>Russian</option>
-                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Simplified Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zht' %}selected{% endif %}>Traditional Chinese</option>
path: templates/index.html
user: login: Sorata
body: It seems that OCR does not need to be adjusted at the moment, this is mainly used for the translation API interface.
created_at: 2025-05-30T13:20:59Z
author_association: CONTRIBUTOR
diff_hunk: @@ -300,7 +300,8 @@ <h3>Translation Settings</h3>
                     <option value="jpn" {% if config.translation.src_lang=='jpn' %}selected{% endif %}>Japanese</option>
                     <option value="kor" {% if config.translation.src_lang=='kor' %}selected{% endif %}>Korean</option>
                     <option value="rus" {% if config.translation.src_lang=='rus' %}selected{% endif %}>Russian</option>
-                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Simplified Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zht' %}selected{% endif %}>Traditional Chinese</option>
path: templates/index.html
user: login: kawaii-flesh
body: If the language is available as source, it should be available for OCR source
created_at: 2025-05-30T13:35:55Z
author_association: OWNER
diff_hunk: @@ -300,7 +300,8 @@ <h3>Translation Settings</h3>
                     <option value="jpn" {% if config.translation.src_lang=='jpn' %}selected{% endif %}>Japanese</option>
                     <option value="kor" {% if config.translation.src_lang=='kor' %}selected{% endif %}>Korean</option>
                     <option value="rus" {% if config.translation.src_lang=='rus' %}selected{% endif %}>Russian</option>
-                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Simplified Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zht' %}selected{% endif %}>Traditional Chinese</option>
path: templates/index.html
user: login: Sorata
body: > If the language is available as source, it should be available for OCR source

I submitted an implementation, but I don't know if it is correct. Please check it for me. If it is not correct, please help me correct it so that I can learn better.
created_at: 2025-05-30T13:59:23Z
author_association: CONTRIBUTOR
diff_hunk: @@ -300,7 +300,8 @@ <h3>Translation Settings</h3>
                     <option value="jpn" {% if config.translation.src_lang=='jpn' %}selected{% endif %}>Japanese</option>
                     <option value="kor" {% if config.translation.src_lang=='kor' %}selected{% endif %}>Korean</option>
                     <option value="rus" {% if config.translation.src_lang=='rus' %}selected{% endif %}>Russian</option>
-                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zho' %}selected{% endif %}>Simplified Chinese</option>
+                    <option value="zho" {% if config.translation.src_lang=='zht' %}selected{% endif %}>Traditional Chinese</option>
path: templates/index.html
user: login: kawaii-flesh
body: I meant to just add traditional Chinese to the OCR Settings section, which is located in index.html, but in reality, it makes no sense to explicitly set the language for recognition, since it is assumed that it is equivalent to the language from which you want to translate. I think I'll do the same with the rest of the languages.
created_at: 2025-05-30T15:13:17Z
author_association: OWNER

PR comments:
user: login: kawaii-flesh
created_at: 2025-05-30T12:47:24Z
body: Thank you for your PR!
author_association: OWNER
user: login: kawaii-flesh
created_at: 2025-05-30T12:48:52Z
body: The virtual environment `tranx-venv` was included in the commit. It needs to be removed
author_association: OWNER
user: login: Sorata
created_at: 2025-05-30T13:34:53Z
body: > The virtual environment `tranx-venv` was included in the commit. It needs to be removed

LOL, accidentally submitted this folder
author_association: CONTRIBUTOR