Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the simply-static domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/wp-includes/functions.php on line 6121
2024 年 8 月

修改MagicMirror2的 compliments 模块的中文字体风格

/* 修改 compliments 模块的中文字体风格 */
.compliments {
  font-size: 30px; /* 可以根据需要调整这个值 */
  color: #00ff00; /* 设置字体颜色为绿色 */
  font-family: "PingFang Light", "PingFang SC", sans-serif; /* 设置中文字体风格为 PingFang Light */
}

翻译

pip install googletrans==4.0.0-rc1
import json
from googletrans import Translator

def translate_file(input_file, output_file):
    # 初始化翻译器
    translator = Translator()

    with open(input_file, 'r', encoding='utf-8') as file:
        lines = file.readlines()

    translated_lines = []
    
    for line in lines:
        translated_lines.append(line)  # 保留原文
        if line.strip():  # 仅翻译非空行
            translation = translator.translate(line.strip(), src='en', dest='zh-cn')
            translated_line = line[:len(line) - len(line.lstrip())] + translation.text + ',\n'
            translated_lines.append(translated_line)

    with open(output_file, 'w', encoding='utf-8') as file:
        file.writelines(translated_lines)

# 指定输入和输出文件路径
input_file = 'input.json'
output_file = 'output.json'

# 运行翻译程序
translate_file(input_file, output_file)

同一行的程序

import json
from googletrans import Translator

def translate_text(text, src='en', dest='zh-cn'):
    translator = Translator()
    translated = translator.translate(text, src=src, dest=dest)
    return translated.text

def translate_json_object(obj, translator):
    if isinstance(obj, dict):
        return {k: translate_json_object(v, translator) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [translate_json_object(i, translator) for i in obj]
    elif isinstance(obj, str):
        return obj + '\\' + translate_text(obj)
    else:
        return obj

def translate_json_file(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as file:
        data = json.load(file)
    
    translator = Translator()
    translated_data = translate_json_object(data, translator)
    
    with open(output_file, 'w', encoding='utf-8') as file:
        json.dump(translated_data, file, ensure_ascii=False, indent=4)

input_file = 'input.json'
output_file = 'output.json'
translate_json_file(input_file, output_file)