// ==UserScript==
// @name GitHub汉化
// @namespace https://github.com/
// @version 1.0.0
// @description GitHub深度汉化脚本。
// @author byzk
// @match https://github.com/*
// @match https://gist.github.com/*
// @grant GM_registerMenuCommand
// @grant GM_getValue
// @grant GM_setValue
// @run-at document-start
// @noframes
// @license MIT
// ==/UserScript==
(function () {
"use strict";
// 开关控制(默认启用)
let isEnabled = GM_getValue("github_zh_enabled", true);
GM_registerMenuCommand(isEnabled ? "🟢 汉化状态:已开启 (点击切换)" : "⚪ 汉化状态:已关闭 (点击切换)", () => {
GM_setValue("github_zh_enabled", !isEnabled);
location.reload();
});
if (!isEnabled) return;
/* =========================================================================
* 1. 核心词库(纯正地道中文,去除冗余英文括号)
* ========================================================================= */
const EXACT_MAP = {
// ---------------- 图1:Projects 项目看板 ----------------
"Recently viewed": "最近查看",
"Created by me": "我创建的项目",
"Create your first GitHub project": "创建您的第一个 GitHub 项目看板",
// ---------------- 图2:我的代码仓库与贡献 ----------------
"My contributions": "我的贡献",
"My repositories": "我的代码仓库",
"My forks": "我的派生仓库",
"Admin access": "管理员权限",
"Views": "视图",
"No saved views": "暂无保存的视图",
"Relevance": "相关度",
"No repositories matched your search.": "没有与您的搜索匹配的代码仓库。",
"Try a different search query.": "请尝试其他搜索条件。",
"ProTip!": "实用小提示!",
// ---------------- 图3:Pull Requests 收件箱 (Inbox) ----------------
"Inbox": "收件箱",
"Authored by me": "我发起的 PR",
"Assigned to me": "指派给我的 PR",
"Involves me": "与我相关的 PR",
"Review requests": "审查请求",
"Needs your review": "需要您审查",
"Needs your teams' review": "需要您所在团队审查",
"Your drafts": "您的草稿",
"Waiting for review or checks": "等待审查或自动化检查",
"Needs action": "需要处理",
"Ready to merge": "可以合并",
"Select repositories": "选择代码仓库",
"Updated: Last month": "更新于:上个月",
"Customize your view": "自定义您的视图",
// ---------------- 图4:Issues 列表与搜索 ----------------
"Mentioning me": "提及我的",
"No results matched your search": "没有与您的搜索匹配的结果",
// ---------------- 图5:Copilot Chat 对话工作台 ----------------
"New chat": "新建对话",
"Agents": "智能体",
"Spaces": "知识空间",
"Ask": "提问",
"Debug": "调试分析",
"Agent": "智能体",
"Create issue": "创建议题",
"Write code": "编写代码",
"The GitHub Copilot app": "GitHub Copilot 桌面客户端",
"New": "新品",
"Download for Windows": "下载 Windows 版",
// ---------------- 图6:Codespaces 云端环境与快速模板 ----------------
"All": "全部",
"Templates": "模板库",
"Your instant dev environment": "您的即时云端开发环境",
"Go from code to commit faster on any project.": "在任何项目中,从编写代码到提交都更加迅速。",
"Go to docs": "查看开发文档",
"Explore quick start templates": "探索快速入门模板",
"See all": "查看全部",
"Use this template": "使用此模板",
"Blank": "空白模板",
"Getting started with GitHub Codespaces": "GitHub Codespaces 入门指南",
"Learn core concepts": "学习核心概念",
"Configure and manage": "配置与管理",
"Develop locally": "本地开发体验",
// ---------------- 图7:Explore 探索与 Trending 趋势榜 ----------------
"Topics": "热门话题",
"Trending": "趋势榜",
"Collections": "精选题集",
"Events": "社区活动",
"GitHub Sponsors": "GitHub 赞助者",
"Trending repositories": "热门趋势仓库",
"The Download": "下载动态 / 快讯",
// ---------------- 图8:Marketplace 应用与扩展商店 ----------------
"Featured": "精选推荐",
"Apps": "应用",
"Create extension": "创建扩展",
"Enhance your workflow with extensions": "使用扩展增强您的开发工作流",
"Recommended": "官方推荐",
"Recently added": "最近上架",
// ---------------- 图9:MCP (Model Context Protocol) 注册表与目录 ----------------
"Connect models to the real world": "将 AI 模型连接到现实世界",
"All MCP servers": "全部 MCP 服务器",
"Install": "安装",
// ---------------- 右上角用户主菜单与状态弹窗 ----------------
"Set status": "设置状态",
"Profile": "个人主页",
"Gists": "代码片段 (Gist)",
"Sponsors": "赞助管理",
"Copilot settings": "Copilot 设置",
"Try Enterprise": "试用企业版",
"Free": "免费版",
"Edit status": "编辑个人状态",
"What's happening": "分享近况",
"Working from home": "居家办公",
"Out sick": "生病请假",
"On vacation": "度假中",
"Focusing": "专注工作中",
"Busy": "忙碌状态",
"Expiration": "过期时间",
"Never": "永不过期",
"Visible to": "可见范围",
"Everyone": "所有人可见",
"Clear status": "清除状态",
"Create list": "创建收藏列表",
"Name ascending (A-Z)": "名称升序 (A-Z)",
"Name descending (Z-A)": "名称降序 (Z-A)",
"Last updated": "最近更新",
"Recently starred": "最近标星",
// ---------------- 全局搜索与筛选 ----------------
"Filter by": "按条件筛选",
"AI resources": "AI 资源库",
"More": "更多",
"Languages": "编程语言",
"Sort by": "排序方式",
"Sort by:": "排序方式:",
"Sort:": "排序:",
"Best match": "最佳匹配",
"Most stars": "最多标星",
"Fewest stars": "最少标星",
"Most forks": "最多派生",
"Fewest forks": "最少派生",
"Recently active": "最近活跃",
"Least recently active": "最久未活跃",
"Recently updated": "最近更新",
"Least recently updated": "最久未更新",
"Search syntax tips": "搜索语法提示",
"Give feedback": "提供反馈",
"Advanced search": "高级检索",
"All GitHub": "全站搜索",
"In this repository": "在当前仓库搜索",
"In this organization": "在当前组织搜索",
// ---------------- 顶部全局导航 ----------------
"Home": "主页",
"Feed": "动态关注流",
"All issues": "全部议题",
"All pull requests": "全部合并请求",
"All repositories": "全部代码仓库",
"MCP registry": "MCP 工具注册表",
"Top repositories": "常用代码库",
"Dashboard": "仪表盘",
"Pull requests": "合并请求",
"Issues": "议题",
"Codespaces": "云端开发环境",
"Marketplace": "应用市场",
"Explore": "探索发现",
"Notifications": "通知中心",
"New repository": "新建代码仓库",
"Import repository": "导入外部代码库",
"New codespace": "新建 Codespace",
"New gist": "新建代码片段",
"New organization": "新建组织",
"New project": "新建项目看板",
"Your profile": "个人主页",
"Your repositories": "我的代码库",
"Your projects": "我的项目看板",
"Your stars": "我的标星收藏",
"Your gists": "我的 Gist 片段",
"Your sponsors": "赞助与支持",
"Your organizations": "我的组织",
"Your enterprises": "我的企业",
"Feature preview": "新功能预览",
"Settings": "账号与系统设置",
"Sign in": "登录",
"Sign up": "注册账号",
"Sign out": "退出登录",
// ---------------- Settings 个人设置页与侧边栏 ----------------
"Public profile": "公开个人资料",
"Account": "账户设置",
"Appearance": "外观偏好",
"Accessibility": "无障碍访问",
"Access": "访问权限",
"Billing and licensing": "账单与许可证",
"Emails": "电子邮箱",
"Password and authentication": "密码与身份验证",
"Sessions": "当前会话",
"SSH and GPG keys": "SSH 与 GPG 密钥",
"Credentials": "凭据管理",
"Organizations": "组织管理",
"Enterprises": "企业设置",
"Moderation": "内容审核与管理",
"Blocked users": "已屏蔽用户",
"Interaction limits": "互动频率限制",
"Code, planning, and automation": "代码、规划与自动化",
"Repositories": "代码仓库",
"Packages": "依赖包",
"Copilot": "Copilot 智能副驾",
"Pages": "Pages 静态网站",
"Saved replies": "快捷回复",
"Integrations": "集成与扩展",
"Applications": "授权应用",
"Scheduled reminders": "定时提醒",
"Security": "安全设置",
"Code security and analysis": "代码安全与漏洞分析",
"Archive": "数据归档",
"Name": "姓名 / 昵称",
"Public email": "公开邮箱",
"Select a verified email to display": "选择一个已验证的邮箱进行展示",
"email settings": "邮箱设置",
"Bio": "个人简介",
"Tell us a little bit about yourself": "简要介绍一下您自己",
"Pronouns": "人称代词",
"Don't specify": "不指定",
"Custom": "自定义",
"URL": "个人主页 / 网站链接",
"Social accounts": "社交媒体账号",
"Company": "就职公司 / 机构",
"Location": "所在地 / 城市",
"Display current local time": "在个人主页显示我当前的本地时间",
"Update profile": "更新个人资料",
"Profile picture": "个人头像",
"Edit picture": "修改头像",
"Upload a photo...": "上传照片...",
"Contributions & Activity": "贡献与动态设置",
"Make profile private and hide activity": "将个人资料设为私有并隐藏动态",
"Include private contributions on my profile": "在个人主页统计中包含私有仓库的贡献",
// ---------------- 仓库核心选项卡与状态 ----------------
"Code": "代码",
"Actions": "自动化流水线",
"Projects": "项目规划",
"Wiki": "知识库",
"Insights": "数据洞察",
"Discussions": "社区讨论",
"Public": "公开",
"Private": "私有",
"Fork": "派生",
"Forks": "派生分支",
"Star": "标星",
"Starred": "已标星",
"Watch": "关注",
"Unwatch": "取消关注",
"Pin": "置顶",
"Unpin": "取消置顶",
"Compare": "对比差异",
"Commits": "提交记录",
"commit": "次提交",
"commits": "次提交",
"branch": "个分支",
"branches": "个分支",
"tag": "个标签",
"tags": "个标签",
"releases": "个发行版",
"release": "个发行版",
"environment": "个环境",
"environments": "个环境",
"contributors": "位贡献者",
"contributor": "位贡献者",
"Deployments": "部署记录",
"Releases": "版本发布",
"Latest": "最新版本",
// ---------------- 代码文件操作 ----------------
"Go to file": "快速查搜文件",
"Add file": "添加文件",
"Create new file": "创建新文件",
"Upload files": "上传已有文件",
"Clone": "克隆代码",
"Download ZIP": "下载 ZIP 压缩包",
"Open with GitHub Desktop": "使用 GitHub Desktop 打开",
"Open in Codespaces": "在 Codespaces 中打开",
"View code": "查看源码",
"History": "历史记录",
"Blame": "溯源 (Blame)",
"Raw": "原始数据",
"Copy raw contents": "复制原始代码",
"Download raw file": "下载源文件",
"Edit this file": "在线编辑该文件",
"Delete this file": "删除该文件",
"Commit changes...": "提交修改...",
"Commit changes": "提交修改",
"Cancel changes": "放弃修改",
// ---------------- Issue / PR 工作流 ----------------
"New issue": "提出新议题",
"New pull request": "发起合并请求",
"Open": "开启中",
"Closed": "已关闭",
"Merged": "已合并",
"Draft": "草稿态",
"Conversation": "对话交流",
"Checks": "CI 自动化检查",
"Files changed": "代码变动详情",
"Reviewers": "代码审查员",
"Assignees": "经办/负责人",
"Labels": "标签分类",
"Milestone": "里程碑目标",
"No milestone": "无里程碑",
"Development": "关联开发任务",
"Close issue": "关闭议题",
"Reopen issue": "重新开启",
"Close pull request": "关闭合并请求",
"Reopen pull request": "重新开启 PR",
"Merge pull request": "合并代码请求",
"Squash and merge": "压缩并合并",
"Rebase and merge": "变基并合并",
"Confirm merge": "确认合并",
"Leave a comment": "填写回复内容",
"Comment": "发表评论",
"Preview": "效果预览",
"Write": "编辑编写",
// ---------------- 常用通用按钮与词汇 ----------------
"Filters": "筛选条件",
"Sort": "排序规则",
"Author": "创建者/作者",
"Label": "按标签筛选",
"Milestones": "按里程碑筛选",
"Assignee": "按经办人筛选",
"Newest": "最新发布",
"Oldest": "最早发布",
"Most commented": "讨论最多",
"Least commented": "讨论最少",
"Save changes": "保存更改",
"Save": "保存",
"Cancel": "取消",
"Delete": "删除",
"Edit": "编辑",
"Update": "更新",
"Submit": "提交",
"Copy": "复制",
"Copied!": "已复制!",
"Search": "搜索",
"Filter": "筛选",
"Clear": "清空",
"Loading...": "加载中...",
"Overview": "概览",
"Stars": "收藏标星",
"Follow": "关注作者",
"Unfollow": "取消关注",
"Edit profile": "修改个人资料",
"Achievements": "成就勋章",
"Contribution activity": "贡献动态",
"Popular repositories": "热门代码库",
"Pinned": "精选置顶"
};
// ---------------- 长句子与段落精确/模糊映射 ----------------
const SENTENCE_MAP = [
// 图1:Projects 空状态
{
en: "Projects are a customizable, flexible tool for planning and tracking your work.",
zh: "项目看板是一个可自定义、灵活强大的工具,用于规划和跟踪您的开发工作。"
},
// 图2:仓库搜索提示
{
en: "Using a comma within certain filters will aggregate the results. For example: language:Go,Ruby will show all repositories that have either Go or Ruby as their primary language.",
zh: "在特定筛选条件中使用逗号可以聚合结果。例如:language:Go,Ruby 将显示以 Go 或 Ruby 为主要语言的所有代码仓库。"
},
// 图3:PR 收件箱自定义视图
{
en: "Tailor your Inbox to fit your workflow, or pick a different view to open by default at github.com/pulls",
zh: "定制您的收件箱以契合团队工作流,或选择其他视图作为访问 github.com/pulls 时的默认页面。"
},
// 图4:Issue 文档
{
en: "Learn more about searching and filtering issues and pull requests.",
zh: "深入了解如何检索和筛选议题与合并请求。"
},
// 图5:Copilot 状态与桌面端介绍
{
en: "Cleared for takeoff soon—your Copilot license is on its way. Please check back in a few minutes.",
zh: "即将就绪——您的 Copilot 许可证正在开通中,请几分钟后再回来查看。"
},
{
en: "An agent-driven desktop experience built natively on GitHub.",
zh: "基于 GitHub 原生构建的智能体驱动桌面端体验。"
},
// 图6:Codespaces 模板与入门
{
en: "Start with a blank canvas or import any packages you need.",
zh: "从空白画布开始,或随时导入您需要的任何开发依赖包。"
},
{
en: "A popular JavaScript library for building user interfaces based on UI components.",
zh: "一个广受欢迎的 JavaScript 界面库,用于基于 UI 组件构建用户前端。"
},
{
en: "New to Codespaces? Start here. Learn the core concepts and how to get started.",
zh: "刚接触 Codespaces?从这里开始,轻松掌握核心概念与上手流程。"
},
{
en: "Learn more about features like secret management and port forwarding.",
zh: "深入了解机密管理和网络端口转发等高级功能。"
},
{
en: "Access codespaces from within Visual Studio Code.",
zh: "直接在本地 Visual Studio Code 中无缝连接和开发云端环境。"
},
// 图7:Explore 推荐语
{
en: "Here's what we found based on your interests...",
zh: "根据您的开发技术与兴趣偏好,精选推荐如下内容:"
},
// 图8:Marketplace 介绍
{
en: "Tools from the community and partners to simplify tasks and automate processes",
zh: "来自全球社区与官方伙伴的高效工具,助您化繁为简并实现流程自动化。"
},
// 图9:MCP 服务器介绍
{
en: "Servers and tools from the community that connect models to files, APIs, databases, and more.",
zh: "来自开源社区的 MCP 服务与工具,将 AI 大模型连接到本地文件、API 接口和数据库等现实环境。"
},
// 状态弹窗
{
en: "When others mention you, assign you, or request your review, GitHub will let them know that you have limited availability.",
zh: "当他人提及您、指派任务或请求您审查代码时,GitHub 将提示对方您当前可用时间有限。"
},
{
en: "Your status will be cleared after the selected time.",
zh: "您的状态将在到达所选时间后自动清除。"
},
{
en: "Limit status visibility to a single organization.",
zh: "将状态可见范围限制为单个组织。"
},
// 设置页长提示
{
en: "Your name may appear around GitHub where you contribute or are mentioned. You can remove it at any time.",
zh: "您的姓名可能会出现在您参与贡献或被提及的 GitHub 页面中。您可以随时删除它。"
},
{
en: "You can manage verified email addresses in your email settings",
zh: "您可以在 邮箱设置 中管理已验证的邮箱地址"
},
{
en: "You can @mention other users and organizations to link to them.",
zh: "您可以使用 @提及 其他用户或组织以建立链接。"
},
{
en: "All of the fields on this page are optional and can be deleted at any time, and by filling them out, you're giving us consent to share this data wherever your user profile appears. Please see our privacy statement for more information.",
zh: "本页上的所有字段均为可选项且可随时删除。填写即代表您同意我们在展示您个人主页的地方共享这些数据。详情请参阅我们的隐私声明。"
},
{
en: "Display current local time",
zh: "在个人主页显示当前本地时间"
},
{
en: "Other users will see the time formatted as",
zh: "其他用户看到的时间格式将为"
}
];
// ---------------- 正则动态规则 ----------------
const REGEX_RULES = [
// 搜索结果:如 "903 results (220 ms)" 或 "903 results"
{ reg: /^([\d,]+)\s+results?(?:\s+\((\d+)\s*ms\))?$/i, rep: (m, p1, p2) => p2 ? `${p1} 条结果 (耗时 ${p2} 毫秒)` : `${p1} 条结果` },
// 搜索结果排序按钮文本:"Sort by: Best match" 等
{ reg: /^Sort\s+by:\s+(.+)$/i, rep: (m, p1) => `排序方式:${EXACT_MAP[p1.trim()] || p1}` },
// 更新时间前缀:"Updated on 2月16日" -> "更新于 2月16日"
{ reg: /^Updated\s+on\s+/i, rep: "更新于 " },
// 相对时间
{ reg: /^(\d+)\s+days?\s+ago$/i, rep: "$1 天前" },
{ reg: /^(\d+)\s+hours?\s+ago$/i, rep: "$1 小时前" },
{ reg: /^(\d+)\s+minutes?\s+ago$/i, rep: "$1 分钟前" },
{ reg: /^(\d+)\s+seconds?\s+ago$/i, rep: "$1 秒前" },
{ reg: /^just\s+now$/i, rep: "刚刚" },
{ reg: /^yesterday$/i, rep: "昨天" },
{ reg: /^(\d+)\s+months?\s+ago$/i, rep: "$1 个月前" },
{ reg: /^(\d+)\s+years?\s+ago$/i, rep: "$1 年前" },
// 数量与统计
{ reg: /^(\d+)\s+repositories$/i, rep: "$1 个代码仓库" },
{ reg: /^(\d+)\s+starred\s+topics$/i, rep: "$1 个收藏的话题" },
{ reg: /^(\d+)\s+starred\s+repositories$/i, rep: "$1 个收藏的仓库" },
{ reg: /^(\d+)\s+commits?$/i, rep: "$1 次提交" },
{ reg: /^(\d+)\s+branches?$/i, rep: "$1 个分支" },
{ reg: /^(\d+)\s+tags?$/i, rep: "$1 个标签" },
{ reg: /^(\d+)\s+open$/i, rep: "$1 个开启中" },
{ reg: /^(\d+)\s+closed$/i, rep: "$1 个已关闭" },
{ reg: /^(\d+)\s+watching$/i, rep: "$1 人关注" },
{ reg: /^(\d+)\s+forks?$/i, rep: "$1 次派生" },
{ reg: /^(\d+)\s+stars?$/i, rep: "$1 颗星" },
// 设置页社交占位符
{ reg: /^Link\s+to\s+social\s+profile\s+(\d+)$/i, rep: "社交平台主页链接 $1" }
];
// ---------------- 属性翻译字典 ----------------
const ATTR_MAP = {
"Search or jump to...": "搜索代码、仓库、Issue 或跳转...",
"Type to search": "输入关键字搜索",
"Type / to search": "输入 / 快速搜索",
"Search this repository": "在当前仓库中检索",
"Filter by name": "按名称筛选",
"Find a file": "查找文件",
"Add a description": "添加项目简介与说明",
"Leave a comment": "填写您的评论内容...",
"Copy URL to clipboard": "复制链接到剪贴板",
"Copy path": "复制相对文件路径",
"Copy permalink": "复制永久哈希链接",
"Switch branches or tags": "切换分支或标签版本",
"Tell us a little bit about yourself": "简要介绍一下您自己",
"Set a status...": "设置一条状态...",
"Ask anything or type @ to add context": "提问任何问题,或输入 @ 添加上下文...",
"Search for apps and actions": "搜索应用和 Actions...",
"Search MCPs": "搜索 MCP 服务器...",
"Sort by": "排序方式",
"Filter by": "按条件筛选"
};
/* =========================================================================
* 2. 安全保护过滤器:绝对不误伤代码区、Commit正文与用户评论
* ========================================================================= */
const IGNORE_TAGS = new Set([
"SCRIPT", "STYLE", "PRE", "CODE", "TEXTAREA", "SVG", "PATH", "KBD"
]);
function isProtectedNode(node) {
if (!node || node.nodeType !== Node.ELEMENT_NODE) return false;
if (IGNORE_TAGS.has(node.tagName)) return true;
if (node.closest(
'pre, code, .blob-wrapper, .blob-code, .blob-code-inner, .highlight, ' +
'.markdown-body, .comment-body, .commit-title, .commit-desc, ' +
'.diff-table, .react-code-lines, .react-code-text, [contenteditable="true"]'
)) {
return true;
}
return false;
}
/* =========================================================================
* 3. 翻译执行引擎(规范化空白符 + 多级模式匹配)
* ========================================================================= */
function normalizeSpaces(str) {
return str.replace(/\s+/g, " ").trim();
}
function translateText(text) {
if (!text) return text;
const trimmed = normalizeSpaces(text);
if (!trimmed) return text;
// 1. 精确短语匹配
if (EXACT_MAP[trimmed]) {
return text.replace(trimmed, EXACT_MAP[trimmed]);
}
// 2. 长句子匹配
for (const item of SENTENCE_MAP) {
if (trimmed === normalizeSpaces(item.en) || trimmed.indexOf(item.en) !== -1) {
return text.replace(item.en, item.zh);
}
}
// 3. 动态正则规则
for (const rule of REGEX_RULES) {
if (rule.reg.test(trimmed)) {
if (typeof rule.rep === "function") {
return text.replace(trimmed, trimmed.replace(rule.reg, rule.rep));
} else {
return text.replace(trimmed, trimmed.replace(rule.reg, rule.rep));
}
}
}
// 4. 复合前缀匹配 (如 "Updated on xxx")
if (/^Updated\s+on\s+/i.test(trimmed)) {
return text.replace(/^(\s*)Updated\s+on\s+/i, "$1更新于 ");
}
return text;
}
function translateAttributes(el) {
if (!el || el.nodeType !== Node.ELEMENT_NODE) return;
for (const attr of ["placeholder", "aria-label", "title"]) {
const val = el.getAttribute(attr);
if (val) {
const norm = normalizeSpaces(val);
if (ATTR_MAP[norm]) {
el.setAttribute(attr, ATTR_MAP[norm]);
} else if (EXACT_MAP[norm]) {
el.setAttribute(attr, EXACT_MAP[norm]);
} else {
for (const rule of REGEX_RULES) {
if (rule.reg.test(norm)) {
if (typeof rule.rep === "function") {
el.setAttribute(attr, norm.replace(rule.reg, rule.rep));
} else {
el.setAttribute(attr, norm.replace(rule.reg, rule.rep));
}
break;
}
}
}
}
}
// /
if (el.tagName === "INPUT" && (el.type === "submit" || el.type === "button")) {
const val = el.value ? normalizeSpaces(el.value) : "";
if (val && EXACT_MAP[val]) {
el.value = EXACT_MAP[val];
}
}
//