Răsfoiți Sursa

feat: HuanGetText遇到重复定义时给出提示

SongZihuan 3 ani în urmă
părinte
comite
1d82b02287
2 a modificat fișierele cu 47 adăugiri și 28 ștergeri
  1. 46 27
      lang/hgt.py
  2. 1 1
      src/main.c

+ 46 - 27
lang/hgt.py

@@ -1,6 +1,7 @@
 import os
 import sys
 import re
+import warnings
 
 from typing import List, Tuple, Dict
 
@@ -71,38 +72,53 @@ for file in file_list:
         while data:
             result: List[str] = pattern.findall(data)
             for i in result:
-                if i[0] in flat_list and flat_list[i[0]][0] != "":  # 若果是空字串则可以覆盖
-                    continue
-
-                if i[0] not in flat_list:
-                    tmp = i[1]
-                    if tmp == "\"<base-tr>\"":
-                        tmp = f"\"{base_tr.get(i[0], None)}\""
-                    default_ = tmp.replace('\n', '\\n').replace('\r', '')
-                    flat_list[i[0]] = (default_,)  # 生成一个数组
+                tmp = i[1]
+                if tmp == "\"<base-tr>\"":
+                    tmp = f"\"{base_tr.get(i[0], None)}\""
+                default_ = tmp.replace('\n', '\\n').replace('\r', '')
+
+                if i[0] in flat_list:
+                    if flat_list[i[0]][0] != default_ and default_ != '""' and flat_list[i[0]][0] != '""':
+                        # 若果是空字串则可以覆盖
+                        warnings.warn(f"Double define text: {i[0]}")
+                        continue
+                    elif default_ == '""':
+                        default_ = flat_list[i[0]][0]
+                flat_list[i[0]] = (default_,)  # 生成一个数组
             data = f.readline()
 
-with open(os.path.join(output_dir, f"_ht.c"), "w", encoding="utf-8") as fc:
-    with open(os.path.join(output_dir, f"_ht.h"), "w", encoding="utf-8") as fh:
-        head = f'''/*
+with open(os.path.join(output_dir, f"_ht.py"), "w", encoding="utf-8") as fpy:
+    with open(os.path.join(output_dir, f"_ht.c"), "w", encoding="utf-8") as fc:
+        with open(os.path.join(output_dir, f"_ht.h"), "w", encoding="utf-8") as fh:
+            head = f'''/*
  * File: _ht.c/_ht.c
  * The file is automatically generated by Huan-GetText
  * Encoding: utf-8
  */'''
-        fc.write(head + '\n\n')
-        fh.write(head + '\n\n')
+            fc.write(head + '\n\n')
+            fc.write(f"#include \"_ht.h\"\n")
+            fc.write("#undef HT_getText\n")
 
-        fh.write(f"#include \"{export_h}.h\"\n")
-        fc.write(f"#include \"_ht.h\"\n")
-        fh.write("#define HT_getText(name, ...) ((char *)(HT_TEXT_ ## name))\n")
-        fh.write(f"{export} int HT_initGetText(char *lang);\n")
-        fc.write("#undef HT_getText\n")
+            fh.write(head + '\n\n')
+            fh.write("#ifndef HT_GETTEXT_H\n")
+            fh.write("#define HT_GETTEXT_H\n")
 
-        for i in flat_list:
-            fc.write(f"{export} const char *HT_TEXT_{i} = {flat_list[i][0]};\n")
-            fh.write(f"{export} const char *HT_TEXT_{i};\n")
+            fh.write("#ifdef HT_getText\n")
+            fh.write("#error \"Double define HT_getText\"\n")
+            fh.write("#endif\n")
+
+            fh.write(f"#include \"{export_h}.h\"\n")
+            fh.write("#define HT_getText(name, ...) ((char *)(HT_TEXT_ ## name))\n")
+            fh.write(f"{export} int HT_initGetText(char *lang);\n")
+
+            fpy.write("# Example for tr file" + '\n\n')
 
-        fc.write('''\n
+            for i in flat_list:
+                fc.write(f"{export} const char *HT_TEXT_{i} = {flat_list[i][0]};\n")
+                fh.write(f"{export} const char *HT_TEXT_{i};\n")
+                fpy.write(f"# {i}: str = \"\"  # {flat_list[i][0]}\n")
+
+            fc.write('''\n
 /* define initGetText */
 /* need dlfcn or dlfcn-win32 */
 
@@ -124,11 +140,13 @@ int HT_initGetText(char *lang) {
     atexit(destructExit);
     char **tmp;\n\n''')
 
-        for i in flat_list:
-            fc.write(f'''    tmp = dlsym(handle, "HT__TEXT_{i}");\n''')
-            fc.write(f'''    if (tmp != NULL) HT_TEXT_{i} = *tmp;\n\n''')
+            for i in flat_list:
+                fc.write(f'''    tmp = dlsym(handle, "HT__TEXT_{i}");\n''')
+                fc.write(f'''    if (tmp != NULL) HT_TEXT_{i} = *tmp;\n\n''')
+
+            fc.write('    return 0;\n}\n')
 
-        fc.write('    return 0;\n}\n')
+            fh.write("#endif\n")
 
 for i in flat_list:
     print(f"TEXT: {i}")
@@ -149,5 +167,6 @@ for t in translation_list:
         with open(os.path.join(translation_output, f"{name}.c"), "w", encoding="utf-8") as fc:
             fc.write(f"#include \"{export_h}.h\"\n")
             for i in flat_list:  # 根据 flat_list 生成变量
+                var[i].strip()  # 去除首尾不需要的符号
                 res = var[i].replace('\n', '\\n').replace('\r', '')
                 fc.write(f"{export} const char *const HT__TEXT_{i} = \"{res}\";\n")

+ 1 - 1
src/main.c

@@ -113,7 +113,7 @@ static void printWelcomeInfo(void) {
 
 static void printHelp(void) {
     printf_stdout(0, "aFunlang %s:\n", HT_getText(usage_n, "Usage"));
-    printf_stdout(0, "%s\n", HT_getText(help_info, ""));
+    printf_stdout(0, "%s\n", HT_getText(help_info, "<base-tr>"));
 }
 
 /*