Python解決Flutter項目簡體字問題的方法
前言
作為面向大陸外市場的應(yīng)用,我們經(jīng)常編寫代碼的時候往往忘記切換繁體字導(dǎo)致上線后出現(xiàn)簡體字。因為研究下業(yè)內(nèi)相關(guān)插件,看看怎么好解決這個問題。 OpenCC 支持語言比較多,所以基于此嘗試了用 Python 去實現(xiàn)。
遇到問題
1、不支持 m1 的芯片issue 。 最后采用的是他人修改后的包 ds-opencc
2、不過 ds-opencc 要求 python 版本最低需要 3.11.x support macos arm64 記錄
結(jié)合 git hooks
結(jié)合 git hooks 我們可以很好在每次提交代碼去執(zhí)行一次腳本

1.創(chuàng)建 Git 鉤子 在你的 Git 倉庫中,進(jìn)入 .git/hooks 目錄。創(chuàng)建一個名為 pre-commit 的文件,Git 會在執(zhí)行 git commit 之前調(diào)用這個鉤子
#!/bin/bash
# 進(jìn)入你的項目目錄
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 版本時,可能我們需要激活對應(yīng)的版本腳本可以改成如下
#!/bin/bash
# 進(jìn)入你的項目目錄
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 實例
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:]
# 項目路徑
project_path = ""
try:
opts, args = getopt.getopt(argv, "p:", ["path="])
except getopt.GetoptError:
print('convert.py -p "項目路徑"')
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('請輸入項目的地址')
sys.exit('請輸入項目的地址')
# 獲取需要修復(fù)項目的路徑
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項目簡體字問題的方法的詳細(xì)內(nèi)容,更多關(guān)于Python Flutter簡體字的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?Fastapi實現(xiàn)統(tǒng)一處理各種異常
這篇文章主要為大家詳細(xì)介紹了Python如何在Fastapi中實現(xiàn)統(tǒng)一處理各種異常,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-06-06
利用Python實現(xiàn)Excel表格轉(zhuǎn)換為HTML格式
在數(shù)據(jù)驅(qū)動的時代,Excel表格作為信息存儲的常用載體,其內(nèi)容如何高效地在Web端展示,成為了許多開發(fā)者和數(shù)據(jù)分析師面臨的挑戰(zhàn),本文將深入探討如何利用Python,實現(xiàn)Excel表格到HTML的批量、自動化轉(zhuǎn)換,需要的朋友可以參考下2025-10-10
Python?OpenCV形態(tài)學(xué)運(yùn)算示例詳解
這篇文章主要為大家介紹了OpenCV中的幾個形態(tài)學(xué)運(yùn)算,例如:腐蝕&膨脹、開&閉運(yùn)算、梯度運(yùn)算、頂帽運(yùn)算黑帽運(yùn)算,感興趣的可以了解一下2022-04-04
python中Ansible模塊的Playbook的具體使用
這篇文章主要介紹了python中Ansible模塊的Playbook的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

