Add Patch.addLanguage for language plugins

This commit is contained in:
KatieFrogs 2022-03-03 18:19:16 +03:00
parent 05bda50655
commit 6f10718bd2
2 changed files with 114 additions and 3 deletions

View File

@ -159,6 +159,34 @@ class Plugins{
} }
return input return input
} }
isObject(input){
return input && typeof input === "object" && input.constructor === Object
}
deepMerge(target, ...sources){
sources.forEach(source => {
if(this.isObject(target) && this.isObject(source)){
for(var i in source){
if(this.isObject(source[i])){
if(!target[i]){
target[i] = {}
}
this.deepMerge(target[i], source[i])
}else if(source[i]){
target[i] = source[i]
}
}
}
})
return target
}
arrayDel(array, item){
var index = array.indexOf(item)
if(index !== -1){
array.splice(index, 1)
return true
}
return false
}
hasSettings(){ hasSettings(){
for(var i = 0; i < this.allPlugins.length; i++){ for(var i = 0; i < this.allPlugins.length; i++){
@ -499,14 +527,32 @@ class EditFunction extends EditValue{
class Patch{ class Patch{
edits = [] edits = []
addedLanguages = []
addEdits(...args){ addEdits(...args){
args.forEach(arg => this.edits.push(arg)) args.forEach(arg => this.edits.push(arg))
} }
addLanguage(lang, forceSet, fallback="en"){
if(fallback){
lang = plugins.deepMerge({}, allStrings[fallback], lang)
}
this.addedLanguages.push({
lang: lang,
forceSet: forceSet
})
}
beforeStart(){ beforeStart(){
this.edits.forEach(edit => edit.start()) this.edits.forEach(edit => edit.start())
this.addedLanguages.forEach(obj => {
settings.addLang(obj.lang, obj.forceSet)
})
} }
beforeStop(){ beforeStop(){
this.edits.forEach(edit => edit.stop()) for(var i = this.edits.length; i--;){
this.edits[i].stop()
}
for(var i = this.addedLanguages.length; i--;){
settings.removeLang(this.addedLanguages[i].lang)
}
} }
beforeUnload(){ beforeUnload(){
this.edits.forEach(edit => edit.unload()) this.edits.forEach(edit => edit.unload())

View File

@ -159,6 +159,58 @@ class Settings{
pageEvents.send("language-change", lang.id) pageEvents.send("language-change", lang.id)
} }
} }
addLang(lang, forceSet){
allStrings[lang.id] = lang
if(lang.categories){
assets.categories.forEach(category => {
if("title_lang" in category && lang.categories[category.title_lang.en]){
category.title_lang[lang.id] = lang.categories[category.title_lang.en]
}
})
}
languageList.push(lang.id)
this.allLanguages.push(lang.id)
this.items.language.default = this.getLang()
if(forceSet){
this.storage.language = lang.id
}else{
try{
this.storage.language = localStorage.getItem("lang")
}catch(e){}
if(this.items.language.options.indexOf(this.storage.language) === -1){
this.storage.language = null
}
}
if(settings.getItem("language") === lang.id){
settings.setLang(lang)
}
}
removeLang(lang){
delete allStrings[lang.id]
assets.categories.forEach(category => {
if("title_lang" in category){
delete category.title_lang[lang.id]
}
})
var index = languageList.indexOf(lang.id)
if(index !== -1){
languageList.splice(index, 1)
}
var index = this.allLanguages.indexOf(lang.id)
if(index !== -1){
this.allLanguages.splice(index, 1)
}
this.items.language.default = this.getLang()
try{
this.storage.language = localStorage.getItem("lang")
}catch(e){}
if(this.items.language.options.indexOf(this.storage.language) === -1){
this.storage.language = null
}
if(lang.id === strings.id){
settings.setLang(allStrings[this.getItem("language")])
}
}
} }
class SettingsView{ class SettingsView{
@ -204,6 +256,10 @@ class SettingsView{
} }
}, this.windowSymbol) }, this.windowSymbol)
if(this.customSettings){
pageEvents.add(window, "language-change", event => this.setLang(), this.windowSymbol)
}
var gamepadEnabled = false var gamepadEnabled = false
if("getGamepads" in navigator){ if("getGamepads" in navigator){
var gamepads = navigator.getGamepads() var gamepads = navigator.getGamepads()
@ -441,8 +497,12 @@ class SettingsView{
this.mode = "latency" this.mode = "latency"
this.latencySet() this.latencySet()
} }
if(this.customSettings){
pageEvents.send("plugins")
}else{
pageEvents.send("settings") pageEvents.send("settings")
} }
}
getElement(name){ getElement(name){
return loader.screen.getElementsByClassName(name)[0] return loader.screen.getElementsByClassName(name)[0]
} }
@ -1014,7 +1074,9 @@ class SettingsView{
return title return title
} }
setLang(lang){ setLang(lang){
if(lang){
settings.setLang(lang) settings.setLang(lang)
}
if(failedTests.length !== 0){ if(failedTests.length !== 0){
showUnsupported(strings) showUnsupported(strings)
} }
@ -1098,6 +1160,9 @@ class SettingsView{
this.gamepad.clean() this.gamepad.clean()
assets.sounds["bgm_settings"].stop() assets.sounds["bgm_settings"].stop()
pageEvents.remove(window, ["mouseup", "touchstart", "touchmove", "touchend", "blur"], this.windowSymbol) pageEvents.remove(window, ["mouseup", "touchstart", "touchmove", "touchend", "blur"], this.windowSymbol)
if(this.customSettings){
pageEvents.remove(window, "language-change", this.windowSymbol)
}
for(var i in this.items){ for(var i in this.items){
this.removeTouchEnd(this.items[i].settingBox) this.removeTouchEnd(this.items[i].settingBox)
} }