mirror of
https://github.com/an-anime-team/an-anime-game-launcher.git
synced 2025-03-17 21:50:11 +03:00
Add Language Function
This commit is contained in:
parent
fc01a7daeb
commit
04b7b5abfe
5 changed files with 76 additions and 6 deletions
6
entry.js
6
entry.js
|
@ -80,6 +80,12 @@ app.whenReady().then(() => {
|
|||
if (BrowserWindow.getAllWindows().length === 0)
|
||||
createWindow();
|
||||
});
|
||||
|
||||
// This has to be here otherwise webContents is invalid.
|
||||
ipcMain.on('changelang', (event, args) => {
|
||||
app.commandLine.appendSwitch('lang', Genshinlib.getConfig().lang.launcher);
|
||||
mainWindow.webContents.send('changelang', { 'lang': args.lang });
|
||||
});
|
||||
});
|
||||
|
||||
// Quit when all windows are closed, except on macOS. There, it's common
|
||||
|
|
|
@ -18,6 +18,11 @@ export class LauncherUI
|
|||
return this._launcherState;
|
||||
}
|
||||
|
||||
public static refreshLang (langcode: string)
|
||||
{
|
||||
i18n.updatelang(langcode);
|
||||
}
|
||||
|
||||
public static setState (state: LauncherState)
|
||||
{
|
||||
$('#downloader-panel').css('display', 'none');
|
||||
|
|
|
@ -3,12 +3,12 @@ const fs = require('fs');
|
|||
let loadedLanguage: any;
|
||||
|
||||
function i18n(): any {
|
||||
if(fs.existsSync(path.join(path.dirname(__dirname), 'locales', navigator.language.toLowerCase() + '.json'))) {
|
||||
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', navigator.language.toLowerCase() + '.json'), 'utf8'));
|
||||
}
|
||||
else {
|
||||
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', 'en.json'), 'utf8'));
|
||||
}
|
||||
if(fs.existsSync(path.join(path.dirname(__dirname), 'locales', navigator.language.toLowerCase() + '.json'))) {
|
||||
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', navigator.language.toLowerCase() + '.json'), 'utf8'));
|
||||
}
|
||||
else {
|
||||
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', 'en.json'), 'utf8'));
|
||||
}
|
||||
};
|
||||
|
||||
i18n.prototype.translate = function(phrase: any) {
|
||||
|
@ -21,4 +21,17 @@ i18n.prototype.translate = function(phrase: any) {
|
|||
return translation
|
||||
}
|
||||
|
||||
i18n.prototype.updatelang = function(newlang: string) {
|
||||
let samecode = new RegExp(`(${newlang.toLowerCase().replace(/-.*$/, '')}.*){2}`, 'g');
|
||||
|
||||
samecode.test(newlang.toLowerCase()) ? newlang = newlang.toLowerCase().replace(/-.*$/, '') : newlang = newlang.toLowerCase();
|
||||
|
||||
if(fs.existsSync(path.join(path.dirname(__dirname), 'locales', newlang + '.json'))) {
|
||||
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', newlang + '.json'), 'utf8'));
|
||||
}
|
||||
else {
|
||||
loadedLanguage = JSON.parse(fs.readFileSync(path.join(path.dirname(__dirname), 'locales', 'en.json'), 'utf8'));
|
||||
}
|
||||
}
|
||||
|
||||
export default new (i18n as any);
|
|
@ -20,6 +20,13 @@ $(() => {
|
|||
document.title = 'Genshin Impact Linux Launcher - ' + Genshinlib.version;
|
||||
|
||||
LauncherUI.setState('game-launch-available');
|
||||
|
||||
ipcRenderer.on('changelang', (event: void, data: any) => {
|
||||
Genshinlib.getBackgroundUri().then(uri => $('body').css('background-image', `url(${ uri })`));
|
||||
LauncherUI.refreshLang(data.lang);
|
||||
LauncherUI.setState(LauncherUI.launcherState);
|
||||
});
|
||||
|
||||
Genshinlib.getBackgroundUri().then(uri => $('body').css('background-image', `url(${ uri })`));
|
||||
|
||||
fetch(`https://genshin.mihoyo.com/launcher/10/${Genshinlib.lang.launcher}?api_url=https%3A%2F%2Fapi-os-takumi.mihoyo.com%2Fhk4e_global&prev=false`)
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { ipcRenderer } = require('electron');
|
||||
|
||||
import $ from 'cash-dom';
|
||||
import i18n from './i18n';
|
||||
import { Genshinlib } from './Genshinlib';
|
||||
|
||||
$(() => {
|
||||
|
||||
$("*[i18id]").each((i, el) => {
|
||||
el.innerText = i18n.translate(el.getAttribute('i18id')?.toString());
|
||||
});
|
||||
|
@ -24,6 +26,7 @@ $(() => {
|
|||
$(`.menu-item[anchor=${anchor}]`).addClass('menu-item-active');
|
||||
});
|
||||
|
||||
// Select the saved options in launcher.json on load.
|
||||
$(`#voice-list option[value="${Genshinlib.getConfig().lang.voice}"]`).prop('selected', true);
|
||||
$(`#language-list option[value="${Genshinlib.getConfig().lang.launcher}"]`).prop('selected', true);
|
||||
|
||||
|
@ -47,6 +50,42 @@ $(() => {
|
|||
}
|
||||
});
|
||||
|
||||
$('#language-list').on('change', (e) => {
|
||||
let activeLNG = Genshinlib.getConfig().lang.launcher;
|
||||
|
||||
if (activeLNG != e.target.value)
|
||||
{
|
||||
Genshinlib.updateConfig({
|
||||
lang: {
|
||||
launcher: e.target.value,
|
||||
voice: Genshinlib.getConfig().lang.voice
|
||||
}
|
||||
});
|
||||
|
||||
// This is required as the file name changes on the API but since we don't call the API before checking if the time is null or expired we set time to null here.
|
||||
Genshinlib.updateConfig({
|
||||
background: {
|
||||
time: null,
|
||||
file: Genshinlib.getConfig().background.file
|
||||
}
|
||||
});
|
||||
|
||||
// Send language updates
|
||||
i18n.updatelang(e.target.value);
|
||||
ipcRenderer.send('changelang', { 'lang': e.target.value });
|
||||
$("*[i18id]").each((i, el) => {
|
||||
el.innerText = i18n.translate(el.getAttribute('i18id')?.toString());
|
||||
});
|
||||
|
||||
$(`#language-list option[value="${activeLNG}"]`).removeProp('selected');
|
||||
$(`#language-list option[value="${e.target.value}"]`).prop('selected', true);
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('New language can\' be changed to the already set language');
|
||||
}
|
||||
});
|
||||
|
||||
let activeRunner = Genshinlib.getConfig().runner;
|
||||
|
||||
Genshinlib.getRunners().then(runners => runners.forEach(category => {
|
||||
|
|
Loading…
Add table
Reference in a new issue