Python解決Flutter項(xiàng)目簡體字問題的方法
前言
作為面向大陸外市場的應(yīng)用,我們經(jīng)常編寫代碼的時(shí)候往往忘記切換繁體字導(dǎo)致上線后出現(xiàn)簡體字。因?yàn)檠芯肯聵I(yè)內(nèi)相關(guān)插件,看看怎么好解決這個(gè)問題。 OpenCC 支持語言比較多,所以基于此嘗試了用 Python 去實(shí)現(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)建一個(gè)名為 pre-commit 的文件,Git 會在執(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í),可能我們需要激活對應(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('請輸入項(xiàng)目的地址')
sys.exit('請輸入項(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)目簡體字問題的方法的詳細(xì)內(nèi)容,更多關(guān)于Python Flutter簡體字的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python實(shí)現(xiàn)爬蟲抓取小說功能示例【抓取金庸小說】
這篇文章主要介紹了python實(shí)現(xiàn)爬蟲抓取小說功能,結(jié)合具體實(shí)例形式分析了使用Python爬蟲抓取金庸小說的具體操作技巧,需要的朋友可以參考下2019-08-08
Python實(shí)現(xiàn)字典的遍歷與排序功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)字典的遍歷與排序功能,結(jié)合實(shí)例形式分析了Python字典的遍歷與排序相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2017-12-12
Python中多線程thread與threading的實(shí)現(xiàn)方法
這篇文章主要介紹了Python中多線程thread與threading的實(shí)現(xiàn)方法,很重要的應(yīng)用,需要的朋友可以參考下2014-08-08
python正則實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了python正則實(shí)現(xiàn)計(jì)算器功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
pandas之分組groupby()的使用整理與總結(jié)
這篇文章主要介紹了pandas之分組groupby()的使用整理與總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Python Pygame實(shí)現(xiàn)落球游戲詳解
本文主要介紹了利用Pygame實(shí)現(xiàn)落球小游戲,即屏幕上落下一個(gè)球,通過鼠標(biāo)移動,地下的木塊如果接上則加分,否則就減去一命,三條命用完則游戲結(jié)束。感興趣的可以學(xué)習(xí)2022-01-01
跟老齊學(xué)Python之總結(jié)參數(shù)的傳遞
這篇文章主要介紹了Python參數(shù)的傳遞的總結(jié),非常的實(shí)用,有需要的朋友可以參考下2014-10-10

