language beginning

This commit is contained in:
SpikeHD
2022-05-11 23:48:05 -07:00
parent 0f22ca3413
commit 8ba3e7022d
3 changed files with 21 additions and 20 deletions

View File

@@ -2,6 +2,7 @@ import React from 'react'
import Checkbox from './common/Checkbox' import Checkbox from './common/Checkbox'
import BigButton from './common/BigButton' import BigButton from './common/BigButton'
import { getConfig, saveConfig } from '../../utils/configuration' import { getConfig, saveConfig } from '../../utils/configuration'
import Tr, { translate } from '../../utils/language'
import { invoke } from '@tauri-apps/api/tauri' import { invoke } from '@tauri-apps/api/tauri'
import './ServerLaunchSection.css' import './ServerLaunchSection.css'
@@ -11,7 +12,9 @@ interface IProps {
} }
interface IState { interface IState {
grasscutterEnabled: boolean grasscutterEnabled: boolean;
buttonLabel: string;
checkboxLabel: string;
} }
export default class ServerLaunchSection extends React.Component<IProps, IState> { export default class ServerLaunchSection extends React.Component<IProps, IState> {
@@ -19,7 +22,9 @@ export default class ServerLaunchSection extends React.Component<IProps, IState>
super(props) super(props)
this.state = { this.state = {
grasscutterEnabled: false grasscutterEnabled: false,
buttonLabel: '',
checkboxLabel: ''
} }
this.toggleGrasscutter = this.toggleGrasscutter.bind(this) this.toggleGrasscutter = this.toggleGrasscutter.bind(this)
@@ -30,8 +35,12 @@ export default class ServerLaunchSection extends React.Component<IProps, IState>
const config = await getConfig() const config = await getConfig()
this.setState({ this.setState({
grasscutterEnabled: config.toggle_grasscutter grasscutterEnabled: config.toggle_grasscutter,
buttonLabel: await translate('main.launch_button'),
checkboxLabel: await translate('main.gc_enable')
}) })
console.log(this.state)
} }
async toggleGrasscutter() { async toggleGrasscutter() {
@@ -48,7 +57,7 @@ export default class ServerLaunchSection extends React.Component<IProps, IState>
if (!config.game_path) return if (!config.game_path) return
// Connect to proxy // Connect to proxy
await invoke('connect', { port: 8365 }) if (config.toggle_grasscutter) await invoke('connect', { port: 8365 })
// Launch the program // Launch the program
await invoke('run_program', { path: config.game_path }) await invoke('run_program', { path: config.game_path })
@@ -58,9 +67,9 @@ export default class ServerLaunchSection extends React.Component<IProps, IState>
return ( return (
<div id="playButton"> <div id="playButton">
<div id="serverControls"> <div id="serverControls">
<Checkbox id="enableGC" label="Connect via Grasscutter" onChange={this.toggleGrasscutter} checked={this.state.grasscutterEnabled}/> <Checkbox id="enableGC" label={this.state.checkboxLabel} onChange={this.toggleGrasscutter} checked={this.state.grasscutterEnabled}/>
</div> </div>
<BigButton text="Launch" onClick={this.playGame} id="officialPlay" /> <BigButton text={this.state.buttonLabel} onClick={this.playGame} id="officialPlay" />
</div> </div>
) )
} }

View File

@@ -7,18 +7,10 @@ interface IProps {
id: string; id: string;
} }
interface IState { export default class BigButton extends React.Component<IProps, never> {
text: string;
}
export default class BigButton extends React.Component<IProps, IState> {
constructor(props: IProps) { constructor(props: IProps) {
super(props) super(props)
this.state = {
text: props.text
}
this.handleClick = this.handleClick.bind(this) this.handleClick = this.handleClick.bind(this)
} }
@@ -29,7 +21,7 @@ export default class BigButton extends React.Component<IProps, IState> {
render() { render() {
return ( return (
<div className="BigButton" onClick={this.handleClick} id={this.props.id}> <div className="BigButton" onClick={this.handleClick} id={this.props.id}>
<div className="BigButtonText">{this.state.text}</div> <div className="BigButtonText">{this.props.text}</div>
</div> </div>
) )
} }

View File

@@ -30,8 +30,6 @@ export default class Tr extends React.Component<IProps, IState> {
invoke('get_lang', { lang: language }).then((response) => { invoke('get_lang', { lang: language }).then((response) => {
const translation_obj = JSON.parse(response as string || '{}') const translation_obj = JSON.parse(response as string || '{}')
console.log(translation_obj)
// Traversal // Traversal
if (text.includes('.')) { if (text.includes('.')) {
const keys = text.split('.') const keys = text.split('.')
@@ -66,16 +64,18 @@ export async function translate(text: string) {
const language = await getConfigOption('language') || 'en' const language = await getConfigOption('language') || 'en'
const translation_json = JSON.parse(await invoke('get_lang', { lang: language }) || '{}') const translation_json = JSON.parse(await invoke('get_lang', { lang: language }) || '{}')
console.log(translation_json)
// Traversal // Traversal
if (text.includes('.')) { if (text.includes('.')) {
const keys = text.split('.') const keys = text.split('.')
let translation = '' let translation: string | Record<string, string> = translation_json
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
if (!translation) { if (!translation) {
translation = '' translation = ''
} else { } else {
translation = translation_json[keys[i]] || '' translation = typeof translation !== 'string' ? translation[keys[i]] : translation as string
} }
} }