Undid storing categories in strings.js, instead using the localization method in songselect.js. category info gets passed down to view.js.

This commit is contained in:
mnvdk 2020-04-27 02:12:09 +02:00
parent 14629ad12a
commit 800e136f91
6 changed files with 35 additions and 58 deletions

View File

@ -106,16 +106,11 @@ class Loader{
}) })
this.addPromise(this.ajax("/api/categories").then(cats => { this.addPromise(this.ajax("/api/categories").then(cats => {
let jsonCategories = JSON.parse(cats) assets.categories = JSON.parse(cats)
assets.categories.forEach(cat => {
for (var i in jsonCategories) { //rename the song_skin property and add category title to categories array cat.songSkin = cat.song_Skin //rename the song_skin property and add category title to categories array
let cat = jsonCategories[i]
cat.songSkin = cat.song_Skin
delete cat.song_Skin delete cat.song_Skin
assets.categories.push(cat) });
let title = cat.title
categories[title] = cat.title_lang
}
assets.categories.push({ assets.categories.push({
title: "default", title: "default",
@ -150,7 +145,6 @@ class Loader{
if(this.error){ if(this.error){
return return
} }
separateStrings() //iterate over strings and apply translations where required
assets.categories //load category backgrounds to DOM assets.categories //load category backgrounds to DOM
.filter(cat=>cat.songSkin && cat.songSkin.bg_img) .filter(cat=>cat.songSkin && cat.songSkin.bg_img)

View File

@ -145,8 +145,13 @@ class Settings{
} }
return this.allLanguages[0] return this.allLanguages[0]
} }
setLang(lang, noEvent, stringRepo){ setLang(lang, noEvent){
stringRepo = lang strings = lang
var boldFonts = strings.font === "Microsoft YaHei, sans-serif"
loader.screen.style.fontFamily = strings.font
loader.screen.style.fontWeight = boldFonts ? "bold" : ""
loader.screen.classList[boldFonts ? "add" : "remove"]("bold-fonts")
if(!noEvent){ if(!noEvent){
pageEvents.send("language-change", lang.id) pageEvents.send("language-change", lang.id)

View File

@ -80,6 +80,8 @@ class SongSelect{
for(let song of assets.songs){ for(let song of assets.songs){
var title = this.getLocalTitle(song.title, song.title_lang) var title = this.getLocalTitle(song.title, song.title_lang)
var subtitle = this.getLocalTitle(title === song.title ? song.subtitle : "", song.subtitle_lang) var subtitle = this.getLocalTitle(title === song.title ? song.subtitle : "", song.subtitle_lang)
let category = assets.categories.find(cat=>cat.id == song.category_id)
var categoryName = this.getLocalTitle(category.title, category.title_lang)
this.songs.push({ this.songs.push({
id: song.id, id: song.id,
title: title, title: title,
@ -87,7 +89,8 @@ class SongSelect{
subtitle: subtitle, subtitle: subtitle,
skin: song.category in this.songSkin ? this.songSkin[song.category] : this.songSkin.default, skin: song.category in this.songSkin ? this.songSkin[song.category] : this.songSkin.default,
courses: song.courses, courses: song.courses,
category: song.category, originalCategory: song.category,
category: categoryName,
category_id: song.category_id, category_id: song.category_id,
preview: song.preview || 0, preview: song.preview || 0,
type: song.type, type: song.type,
@ -103,8 +106,8 @@ class SongSelect{
}) })
} }
this.songs.sort((a, b) => { this.songs.sort((a, b) => {
var catA = a.category in this.songSkin ? this.songSkin[a.category] : this.songSkin.default var catA = a.originalCategory in this.songSkin ? this.songSkin[a.originalCategory] : this.songSkin.default
var catB = b.category in this.songSkin ? this.songSkin[b.category] : this.songSkin.default var catB = b.originalCategory in this.songSkin ? this.songSkin[b.originalCategory] : this.songSkin.default
if(catA.sort === catB.sort){ if(catA.sort === catB.sort){
if(a.order === b.order){ if(a.order === b.order){
return a.id > b.id ? 1 : -1 return a.id > b.id ? 1 : -1
@ -261,7 +264,7 @@ class SongSelect{
} }
this.songSelect = document.getElementById("song-select") this.songSelect = document.getElementById("song-select")
var cat = this.songs[this.selectedSong].category var cat = this.songs[this.selectedSong].originalCategory
this.drawBackground(cat) this.drawBackground(cat)
this.previewId = 0 this.previewId = 0
@ -769,6 +772,7 @@ class SongSelect{
"folder": selectedSong.id, "folder": selectedSong.id,
"difficulty": diff, "difficulty": diff,
"category": selectedSong.category, "category": selectedSong.category,
"category_id":selectedSong.category_id,
"type": selectedSong.type, "type": selectedSong.type,
"offset": selectedSong.offset, "offset": selectedSong.offset,
"songSkin": selectedSong.songSkin, "songSkin": selectedSong.songSkin,
@ -1043,8 +1047,8 @@ class SongSelect{
]) ])
}) })
var category = this.songs[this.selectedSong].category
var selectedSong = this.songs[this.selectedSong] var selectedSong = this.songs[this.selectedSong]
var category = selectedSong.category
this.draw.category({ this.draw.category({
ctx: ctx, ctx: ctx,
x: winW / 2 - 280 / 2 - 30, x: winW / 2 - 280 / 2 - 30,
@ -1069,8 +1073,9 @@ class SongSelect{
id: category + selectedSong.skin.outline id: category + selectedSong.skin.outline
}, ctx => { }, ctx => {
if(category){ if(category){
if(category in strings.categories){ let cat = assets.categories.find(cat=>cat.title === category)
var categoryName = strings.categories[category] if(cat){
var categoryName = this.getLocalTitle(cat.title, cat.title_lang)
}else{ }else{
var categoryName = category var categoryName = category
} }
@ -1167,7 +1172,7 @@ class SongSelect{
} }
if(this.songs[this.selectedSong].action !== "back"){ if(this.songs[this.selectedSong].action !== "back"){
var cat = this.songs[this.selectedSong].category var cat = this.songs[this.selectedSong].originalCategory
this.drawBackground(cat) this.drawBackground(cat)
} }
} }

View File

@ -1073,13 +1073,10 @@ var translations = {
ko: "가사가있는" ko: "가사가있는"
} }
} }
var categories = {}
var allStrings = {} var allStrings = {}
var categoryStrings = {}
function separateStrings(){ function separateStrings(){
for(var j in languageList){ for(var j in languageList){
var lang = languageList[j] var lang = languageList[j]
allStrings[lang] = { allStrings[lang] = {
@ -1097,20 +1094,13 @@ function separateStrings(){
for(var i in obj){ for(var i in obj){
translateObj(obj[i], i, str[name]) translateObj(obj[i], i, str[name])
} }
}else{ //key appears in string obj but has no language values, so just apply the key name to each language for consistency and as fallback
str[name] = name
} }
} }
for(var i in translations){ for(var i in translations){
translateObj(translations[i], i, str) translateObj(translations[i], i, str)
} }
categoryStrings[lang] = {}
var str = categoryStrings[lang]
for(var i in categories){
translateObj(categories[i], i, str)
}
} }
} }
separateStrings()

View File

@ -11,7 +11,7 @@ class Titlescreen{
this.disclaimerCopyright = document.getElementById("title-disclaimer-copyright") this.disclaimerCopyright = document.getElementById("title-disclaimer-copyright")
this.logo = new Logo() this.logo = new Logo()
} }
this.setLang() this.setLang(allStrings[settings.getItem("language")])
if(songId){ if(songId){
if(localStorage.getItem("tutorial") === "true"){ if(localStorage.getItem("tutorial") === "true"){
@ -76,20 +76,8 @@ class Titlescreen{
}, 500) }, 500)
} }
} }
setLang(){ setLang(lang, noEvent){
let stringLang = allStrings[settings.getItem("language")] settings.setLang(lang, true)
let categoryLang = categoryStrings[settings.getItem("language")]
strings = stringLang
strings.categories = categoryLang
settings.setLang(stringLang, true, allStrings)
settings.setLang(categoryLang, true, categoryStrings)
let boldFonts = strings.font === "Microsoft YaHei, sans-serif"
loader.screen.style.fontFamily = strings.font
loader.screen.style.fontWeight = boldFonts ? "bold" : ""
loader.screen.classList[boldFonts ? "add" : "remove"]("bold-fonts")
if(this.songId){ if(this.songId){
return return

View File

@ -296,9 +296,9 @@
var _x = 628 - _w var _x = 628 - _w
var _y = 88 - _h var _y = 88 - _h
let category = assets.categories.find(cat=>cat.title == selectedSong.category) let category = assets.categories.find(cat=>cat.id == selectedSong.category_id)
if(category != null && category.songSkin != null && category.songSkin.infoFill != null){ if(category != null && category.songSkin != null && category.songSkin.infoFill != null){
ctx.fillStyle = assets.categories.find(cat=>cat.title == selectedSong.category).songSkin.infoFill ctx.fillStyle = category.songSkin.infoFill
}else{ }else{
ctx.fillStyle = assets.categories.find(cat=>cat.title == 'default').songSkin.infoFill ctx.fillStyle = assets.categories.find(cat=>cat.title == 'default').songSkin.infoFill
} }
@ -310,14 +310,9 @@
}) })
ctx.fill() ctx.fill()
if(selectedSong.category in strings.categories){
var categoryName = strings.categories[selectedSong.category]
}else{
var categoryName = selectedSong.category
}
this.draw.layeredText({ this.draw.layeredText({
ctx: ctx, ctx: ctx,
text: categoryName, text: selectedSong.category,
fontSize: 15, fontSize: 15,
fontFamily: this.font, fontFamily: this.font,
align: "center", align: "center",