Skip to content

Commit

Permalink
添加"js原生选择器 翻译元素"功能
Browse files Browse the repository at this point in the history
1. 主要针对顶部 "拉取请求" 中间空格问题
2. 修补 "Type" 多义词 默认翻译为"请键入", 特定位置翻译为"类型"
  • Loading branch information
maboloshi committed Feb 10, 2022
1 parent 26086a4 commit c4a3284
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ I18N.conf = {
};

I18N.zh = {
"selector": [ // 元素筛选器规则
["body > div.position-relative.js-header-wrapper > header > div.Header-item.Header-item--full.flex-column.flex-md-row.width-full.flex-order-2.flex-md-order-none.mr-0.mr-md-3.mt-3.mt-md-0.Details-content--hidden-not-important.d-md-flex > nav > a:nth-child(2)", "拉取请求"], // 顶部条 拉取请求
["#type-options > summary > span:nth-child(1)", "类型"], // 个人主页 --> 仓库标签页-->类型筛选器 Type
["#review-changes-modal > summary > span.js-review-changes", "审查更改"], // 拉取请求 更改的文件
],

"title": { // 标题翻译
"static": { // 静态翻译
"Sign in to GitHub · GitHub": "登录 GitHub · GitHub",
Expand Down
20 changes: 20 additions & 0 deletions main(greasyfork).user.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
var page = getPage();

transTitle(); // 页面标题翻译
translateBySelector(); // Selector 翻译
traverseNode(document.body); // 立即翻译页面
watchUpdate();

Expand Down Expand Up @@ -72,6 +73,7 @@

new m(function(mutations) {
transTitle();
translateBySelector(); // Selector 翻译 目前先跟随 url 即页面标题变化
}).observe(
document.querySelector('title'),
{ characterData: true, childList: true }
Expand Down Expand Up @@ -361,4 +363,22 @@
};
}

/**
* js原生选择器 翻译元素
*
* 2022-02-04 19:46:44
* 灵感参考自:k1995/github-i18n-plugin
*/
function translateBySelector() {
res = I18N[lang].selector;
if (res) {
for (var i = 0, len = res.length; i < len; i++) {
let element = document.querySelector(res[i][0])
if (element) {
element.textContent = res[i][1];
}
}
}
}

})(window, document);
20 changes: 20 additions & 0 deletions main.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
var page = getPage();

transTitle(); // 页面标题翻译
translateBySelector(); // Selector 翻译
traverseNode(document.body); // 立即翻译页面
watchUpdate();

Expand Down Expand Up @@ -72,6 +73,7 @@

new m(function(mutations) {
transTitle();
translateBySelector(); // Selector 翻译 目前先跟随 url 即页面标题变化
}).observe(
document.querySelector('title'),
{ characterData: true, childList: true }
Expand Down Expand Up @@ -362,4 +364,22 @@
};
}

/**
* js原生选择器 翻译元素
*
* 2022-02-04 19:46:44
* 灵感参考自:k1995/github-i18n-plugin
*/
function translateBySelector() {
res = I18N[lang].selector;
if (res) {
for (var i = 0, len = res.length; i < len; i++) {
let element = document.querySelector(res[i][0])
if (element) {
element.textContent = res[i][1];
}
}
}
}

})(window, document);

0 comments on commit c4a3284

Please sign in to comment.