forked from CQYC-OIer/5-Timer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
let nowlang='en'; | ||
|
||
var langText= | ||
{ | ||
'en': | ||
{ | ||
'倒计时': 'Countdown Timer', | ||
'未开始计时': 'Not Started', | ||
'分钟(默认 5)': 'Minutes (Default 5)', | ||
'秒(默认 0)': 'Seconds (Default 0)', | ||
'毫秒(默认 0)': 'Milliseconds (Default 0)', | ||
'有 bug?使用暂无发现 bug 的': 'Got a bug? Use a version with no bugs found yet: ', | ||
'旧版': 'Click here', | ||
'开始计时': 'Start Timing', | ||
'重置计时器': 'Reset Timer', | ||
'停止计时': 'Stop Timing', | ||
'保存': 'Save', | ||
'正在计时': 'Now Timing', | ||
'超时': 'Time Out' | ||
}, | ||
'zh': | ||
{ | ||
'Countdown Timer': '倒计时', | ||
'Not Started': '未开始计时', | ||
'Minutes (Default 5)': '分钟(默认 5)', | ||
'Seconds (Default 0)': '秒(默认 0)', | ||
'Milliseconds (Default 0)': '毫秒(默认 0)', | ||
'Got a bug? Use a version with no bugs found yet: ': '有 bug?使用暂无发现 bug 的', | ||
'Click here': '旧版', | ||
'Start Timing': '开始计时', | ||
'Reset Timer': '重置计时器', | ||
'Stop Timing': '停止计时', | ||
'Save': '保存', | ||
'Now Timing': '正在计时', | ||
'Time Out': '超时', | ||
} | ||
}; | ||
|
||
function translatePage(language) | ||
{ | ||
var currentLangText=langText[language]; | ||
function translateNode(node) | ||
{ | ||
if(node.nodeType===Node.TEXT_NODE) | ||
{ | ||
Object.keys(currentLangText).forEach(function(key) | ||
{ | ||
if(node.textContent.includes(key)) | ||
{ | ||
node.textContent=node.textContent.replace(key,currentLangText[key]); | ||
} | ||
}); | ||
} | ||
else if(node.nodeType===Node.ELEMENT_NODE) | ||
{ | ||
if(node.tagName.toLowerCase()==='input') | ||
{ | ||
var placeholder=node.getAttribute('placeholder'); | ||
if(placeholder) | ||
{ | ||
Object.keys(currentLangText).forEach(function(key) | ||
{ | ||
if(placeholder.includes(key)) node.setAttribute('placeholder',placeholder.replace(key,currentLangText[key])); | ||
}); | ||
} | ||
} | ||
else if(node.childNodes&&node.childNodes.length>0) for(var i=0;i<node.childNodes.length;i++) translateNode(node.childNodes[i]); | ||
} | ||
} | ||
translateNode(document.body); | ||
} | ||
|
||
setInterval(function() | ||
{ | ||
translatePage(nowlang); | ||
},10); | ||
|
||
document.addEventListener('DOMContentLoaded',function() | ||
{ | ||
var languageSwitch=document.getElementById('language-switch'); | ||
var languageOptions=document.getElementById('language-options'); | ||
var englishOption=document.getElementById('english'); | ||
var chineseOption=document.getElementById('chinese'); | ||
var userLanguage=navigator.language||navigator.userLanguage; | ||
var defaultLanguage=userLanguage.startsWith('zh')?'zh':'en'; | ||
nowlang=defaultLanguage; | ||
languageOptions.style.display='none'; | ||
languageSwitch.addEventListener('click',function() | ||
{ | ||
if(languageOptions.style.display==='none') languageOptions.style.display='block'; | ||
else languageOptions.style.display='none'; | ||
}); | ||
englishOption.addEventListener('click',function(event) | ||
{ | ||
event.preventDefault(); | ||
nowlang='en'; | ||
languageOptions.style.display='none'; | ||
}); | ||
chineseOption.addEventListener('click',function(event) | ||
{ | ||
event.preventDefault(); | ||
nowlang='zh'; | ||
languageOptions.style.display='none'; | ||
}); | ||
}); | ||
|