之前项目里国际化的变量都是下划线命名法
现在要改成驼峰命名
国际化的变量深入项目各个文件, 1000多个, 实在没办法一个一个改了, 于是写了个python脚本, 完成了
import os
import re
def convert_to_camelcase(filepath):
try:
with open(filepath, 'r') as file:
content = file.read()
# 在这里编写正则表达式,并使用sub函数进行替换
new_content = re.sub(r"i18n_t\.(\w+)", underscore_to_camelcase, content)
with open(filepath, 'w') as file:
file.write(new_content)
except Exception as e:
print('处理文件时发生错误:{}, {}'.format(file, str(e)))
def underscore_to_camelcase(match):
name = match.group(0)
if not name.startswith("i18n_t."):
return
words = name.split('.')
newords = words[1].split('_')
print(newords)
camelcase_name = ''
for word in newords[0:]:
camelcase_word = word.capitalize()
camelcase_name += camelcase_word
print(camelcase_name)
print(camelcase_name)
return words[0] + "." + camelcase_name[0].lower() + camelcase_name[1:]
# 获取指定文件夹路径
folder_path = os.getcwd()
for root, dirs, files in os.walk(folder_path):
for filename in files:
filepath = os.path.join(root, filename)
print("Converting", filepath)
convert_to_camelcase(filepath)
网友评论