Python解決Flutter項(xiàng)目簡(jiǎn)體字問(wèn)題的方法
前言
作為面向大陸外市場(chǎng)的應(yīng)用,我們經(jīng)常編寫(xiě)代碼的時(shí)候往往忘記切換繁體字導(dǎo)致上線后出現(xiàn)簡(jiǎn)體字。因?yàn)檠芯肯聵I(yè)內(nèi)相關(guān)插件,看看怎么好解決這個(gè)問(wèn)題。 OpenCC 支持語(yǔ)言比較多,所以基于此嘗試了用 Python 去實(shí)現(xiàn)。
遇到問(wèn)題
1、不支持 m1 的芯片issue 。 最后采用的是他人修改后的包 ds-opencc
2、不過(guò) ds-opencc 要求 python 版本最低需要 3.11.x support macos arm64 記錄
結(jié)合 git hooks
結(jié)合 git hooks 我們可以很好在每次提交代碼去執(zhí)行一次腳本
1.創(chuàng)建 Git 鉤子 在你的 Git 倉(cāng)庫(kù)中,進(jìn)入 .git/hooks 目錄。創(chuàng)建一個(gè)名為 pre-commit 的文件,Git 會(huì)在執(zhí)行 git commit 之前調(diào)用這個(gè)鉤子
#!/bin/bash # 進(jìn)入你的項(xiàng)目目錄 cd "$(dirname "$0")/../.." # 執(zhí)行 Python 腳本 python3 path/to/your/chinese_convert.py -p "$(pwd)" # 檢查腳本執(zhí)行是否成功 if [ $? -ne 0 ]; then echo "轉(zhuǎn)換失敗,提交被取消!" exit 1 fi
當(dāng)然如果是使用了 pyenv 管理 python 版本時(shí),可能我們需要激活對(duì)應(yīng)的版本腳本可以改成如下
#!/bin/bash # 進(jìn)入你的項(xiàng)目目錄 cd "$(dirname "$0")/../.." path="$(pwd)" cd xxx/TWHouseScript # 確保 pyenv 已經(jīng)初始化 if command -v pyenv >/dev/null; then eval "$(pyenv init --path)" eval "$(pyenv init -)" else echo "pyenv 未安裝或未正確初始化" exit 1 fi # 激活虛擬環(huán)境 if pyenv versions | grep -q 'env3124'; then pyenv activate env3124 else echo "指定的 pyenv 虛擬環(huán)境不存在" exit 1 fi python3 chinese_convert.py -p "$path" # 檢查腳本執(zhí)行是否成功 if [ $? -ne 0 ]; then echo "轉(zhuǎn)換失敗,提交被取消!" exit 1 fi
2.賦予執(zhí)行權(quán)限
chmod +x .git/hooks/pre-commit
python 代碼
import os import sys import getopt import ds_opencc # 創(chuàng)建 OpenCC 實(shí)例 cc = ds_opencc.OpenCC('s2tw.json') def is_comment(line): # 判斷是否是 Dart 文件中的注釋 return line.strip().startswith('//') or line.strip().startswith('/*') or line.strip().endswith('*/') or line.strip().startswith('*') def convert_file(file_path): with open(file_path, 'r', encoding='utf-8') as file: lines = file.readlines() converted_lines = [] in_block_comment = False for line in lines: if '/*' in line and '*/' not in line: in_block_comment = True elif '*/' in line: in_block_comment = False if in_block_comment or is_comment(line): converted_lines.append(line) else: converted_lines.append(cc.convert(line)) with open(file_path, 'w', encoding='utf-8') as file: file.writelines(converted_lines) def convert_dart_files_in_directory(directory): print(f'Converting Dart files in {directory}...') for root, _, files in os.walk(directory): for file in files: if file.endswith('.dart'): convert_file(os.path.join(root, file)) # python chinese_convert.py -p '/Users/zhengzeqin/Desktop/GitLab/tw591_xxx' if __name__ == '__main__': argv = sys.argv[1:] # 項(xiàng)目路徑 project_path = "" try: opts, args = getopt.getopt(argv, "p:", ["path="]) except getopt.GetoptError: print('convert.py -p "項(xiàng)目路徑"') sys.exit(2) print("opts ===>", opts) for opt, arg in opts: if opt in ["-p", "--path"]: project_path = arg if len(project_path) == 0: print('請(qǐng)輸入項(xiàng)目的地址') sys.exit('請(qǐng)輸入項(xiàng)目的地址') # 獲取需要修復(fù)項(xiàng)目的路徑 if len(project_path) == 0: current_directory = os.path.dirname(os.path.abspath(__file__)) else: current_directory = project_path print(f'current_directory: {current_directory}') convert_dart_files_in_directory(current_directory)
以上就是Python解決Flutter項(xiàng)目簡(jiǎn)體字問(wèn)題的方法的詳細(xì)內(nèi)容,更多關(guān)于Python Flutter簡(jiǎn)體字的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 利用Python構(gòu)建Flutter應(yīng)用的教程詳解
- Python Numpy運(yùn)行報(bào)錯(cuò)IndexError與形狀不匹配的問(wèn)題解決辦法
- Python報(bào)錯(cuò):OSError:?[Errno?22]?Invalid?argument解決方案及應(yīng)用實(shí)例
- Python Numpy運(yùn)行報(bào)錯(cuò):IndexError: too many indices for array的分析及解決
- 解決python訓(xùn)練模型報(bào)錯(cuò):BrokenPipeError:?[Errno?32]?Broken?pipe
相關(guān)文章
python實(shí)現(xiàn)爬蟲(chóng)抓取小說(shuō)功能示例【抓取金庸小說(shuō)】
這篇文章主要介紹了python實(shí)現(xiàn)爬蟲(chóng)抓取小說(shuō)功能,結(jié)合具體實(shí)例形式分析了使用Python爬蟲(chóng)抓取金庸小說(shuō)的具體操作技巧,需要的朋友可以參考下2019-08-08Python實(shí)現(xiàn)字典的遍歷與排序功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)字典的遍歷與排序功能,結(jié)合實(shí)例形式分析了Python字典的遍歷與排序相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2017-12-12Python中多線程thread與threading的實(shí)現(xiàn)方法
這篇文章主要介紹了Python中多線程thread與threading的實(shí)現(xiàn)方法,很重要的應(yīng)用,需要的朋友可以參考下2014-08-08python正則實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了python正則實(shí)現(xiàn)計(jì)算器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12詳解Python openpyxl庫(kù)的基本應(yīng)用
這篇文章主要介紹了Python openpyxl庫(kù)的基本應(yīng)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-02-02pandas之分組groupby()的使用整理與總結(jié)
這篇文章主要介紹了pandas之分組groupby()的使用整理與總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Python Pygame實(shí)現(xiàn)落球游戲詳解
本文主要介紹了利用Pygame實(shí)現(xiàn)落球小游戲,即屏幕上落下一個(gè)球,通過(guò)鼠標(biāo)移動(dòng),地下的木塊如果接上則加分,否則就減去一命,三條命用完則游戲結(jié)束。感興趣的可以學(xué)習(xí)2022-01-01跟老齊學(xué)Python之總結(jié)參數(shù)的傳遞
這篇文章主要介紹了Python參數(shù)的傳遞的總結(jié),非常的實(shí)用,有需要的朋友可以參考下2014-10-10