LayUI组件国际化多国语言版本脚本-下篇根据语种替换

最近项目需要使用多国语言版本,但是项目之前的代码使用了Layui组件,全网找不到layui的多语言,只能自己动手做,使用Python脚本提取组件中所有的中文,使用本文脚本将中文替换成相应的语种,

1、请将翻译后language.txt,放到layui目录的Models下。

2、读取language.txt,保存到字典中

python 复制代码
import io
import os
import re
import hashlib

filepath = "layui\\layui-en-US\\modules" 
#读取翻译文件
file_data_dict = {}

languagefile = "language.txt"

#读取语言文件
def read_file_to_dict(file_path):
    global languagefile
    global file_data_dict
    with open(file_path+"\\"+languagefile, 'r',encoding='utf-8') as file:
        for line in file.readlines():
            line = line.strip()  # 去除每行首尾的空白字符
            if line:  # 跳过空行
                key, value = line.split('=')
                key = key.replace(" ","")#去掉里面可能出现的空格,google翻译后会出现个别空格,正常人工翻译不用管
                file_data_dict[key] = value

3、根据字典文件的内容进行替换

python 复制代码
# 遍历指定目录,显示目录下的所有文件名
def each_file(filepath):
    for root, dirs, files in os.walk(filepath):
        for file in files:
            if(file.find(languagefile)== -1):#过滤语言包,避免重复运行一直累加
                filename = os.path.join(root, file)
                print(filename+"\n")
                filtered_chinese_chars = read_file(filename)

    
#读取文件并替换本文件
def read_file(filename):
    #if(filename.find('carousel.js')!= -1):
        chinese_pattern = re.compile('[\u4e00-\u9fff]+')
        new_lines = []
        with open(filename, 'r', encoding='utf-8') as file:
            for line in file:
                matches = chinese_pattern.findall(line)
                new_line = line
                for match in matches:
                    #过滤js文件里面 // 和*开头的行
                    if (not line.strip().startswith('//')) and (not line.strip().startswith('*')): 
                        #查找字符串面是否直接带// /*
                        index_target = line.find(match)
                        if index_target > 0:
                            preceding_text = line[:index_target]
                            if ("//" not in preceding_text) and ("/*" not in preceding_text):
                                #增加字符串的Md5标识,方便之后翻译后替换
                                md5 = hashlib.md5(match.encode('utf-8')).hexdigest()
                                file_name = os.path.basename(filename)
                                key = file_name+"_"+md5
                                value = file_data_dict[key]
                                #替换掉
                                print(line)
                                new_line = new_line.replace(match, value)
                                print(new_line)

                                #进行文件替换 file_data_dict
                                #print(match)
                new_lines.append(new_line)
            #保存文件
        with open(filename, 'w', encoding='utf-8') as file:
            file.writelines(new_lines)

4、执行主程序

python 复制代码
if __name__ == '__main__':
  read_file_to_dict(file_path=filepath)
  each_file(filepath)
相关推荐
snow_star_dream21 分钟前
(笔记)VSC python应用--函数补全注释添加
笔记·python
郝学胜-神的一滴31 分钟前
Python中的Mixin继承:灵活组合功能的强大模式
开发语言·python·程序人生
叫我:松哥32 分钟前
基于python强化学习的自主迷宫求解,集成迷宫生成、智能体训练、模型评估等
开发语言·人工智能·python·机器学习·pygame
晚霞的不甘32 分钟前
Flutter for OpenHarmony 创意实战:打造一款炫酷的“太空舱”倒计时应用
开发语言·前端·flutter·正则表达式·前端框架·postman
2601_9494800635 分钟前
Flutter for OpenHarmony音乐播放器App实战:定时关闭实现
javascript·flutter·原型模式
2301_7644413336 分钟前
2025年YOLO算法案例应用领域应用趋势
python·yolo
汗流浃背了吧,老弟!1 小时前
构建RAG系统时,如何选择合适的嵌入模型(Embedding Model)?
人工智能·python·embedding
盐真卿1 小时前
python第四部分:模块(每日更新)
开发语言·python
这儿有一堆花1 小时前
CSS 拟真光影设计:从扁平到深度的技术复盘
前端·css
喵手1 小时前
Python爬虫零基础入门【第九章:实战项目教学·第2节】“接口优先“项目:从 Network 还原 JSON 接口分页!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·接口优先·json接口分页