Skip to content

Commit

Permalink
[Fix] Tray icon language doesn't follow config
Browse files Browse the repository at this point in the history
  • Loading branch information
arielj committed Jan 16, 2025
1 parent ed52e16 commit 11a6e66
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/backend/backend_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { GameStatus, RecentGame } from 'common/types'
type BackendEvents = {
gameStatusUpdate: (payload: GameStatus) => void
recentGamesChanged: (recentGames: RecentGame[]) => void
languageChanged: () => void
settingChanged: (obj: {
key: string
oldValue: unknown
Expand Down
3 changes: 3 additions & 0 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ import {
getGameSdl
} from 'backend/storeManagers/legendary/library'
import { storeMap } from 'common/utils'
import { backendEvents } from './backend_events'

app.commandLine?.appendSwitch('ozone-platform-hint', 'auto')

Expand Down Expand Up @@ -448,6 +449,8 @@ if (!gotTheLock) {
logInfo(['Changing Language to:', language], LogPrefix.Backend)
await i18next.changeLanguage(language)
gameInfoStore.clear()
GlobalConfig.get().setSetting('language', language)
backendEvents.emit('languageChanged')
})

downloadAntiCheatData()
Expand Down
42 changes: 40 additions & 2 deletions src/backend/tray_icon/__tests__/tray_icon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { backendEvents } from '../../backend_events'
import { GlobalConfig } from '../../config'
import { RecentGame } from 'common/types'
import { configStore } from '../../constants'
import i18next from 'i18next'

jest.mock('../../logger/logfile')
jest.mock('../../config')
Expand All @@ -24,6 +25,10 @@ describe('TrayIcon', () => {

describe('content', () => {
describe('contextMenu', () => {
beforeEach(() => {
setRecentGames([])
})

it('shows recent games first', () => {
const menu = testingExportsTrayIcon.contextMenu(mainWindow, [
{ title: 'game 1', appName: '123456' }
Expand Down Expand Up @@ -52,8 +57,9 @@ describe('TrayIcon', () => {
click: expect.any(Function)
})
})

describe('when recent games change', () => {
it('updates the content when recent games change', async () => {
it('updates the content', async () => {
setRecentGames([{ title: 'game 1', appName: '12345' }])

const appIcon = await initTrayIcon(mainWindow)
Expand Down Expand Up @@ -90,7 +96,7 @@ describe('TrayIcon', () => {
})
})

it('limits the games based on config', async () => {
it('limits the number games displayed based on config', async () => {
// limits to maxRecentGames config
GlobalConfig['setConfigValue']('maxRecentGames', 3)

Expand Down Expand Up @@ -135,6 +141,38 @@ describe('TrayIcon', () => {
})
})
})

describe('when language changes', () => {
it('reloads the menu with the new language', async () => {
// mock some translation
const original_t = i18next.t
jest.spyOn(i18next, 't').mockImplementation((key) => {
if (key === 'tray.quit') {
return i18next.language === 'es' ? 'Salir' : 'Quit'
}
return key as string
})

// check it renders english
i18next.language = 'en'
const appIcon = await initTrayIcon(mainWindow)
let items = appIcon['menu']
expect(items[items.length - 1].label).toEqual('Quit')

// change language
i18next.language = 'es'
backendEvents.emit('languageChanged')
// wait for a moment since the event handler is async
await wait(5)

// check it renders spanish
items = appIcon['menu']
expect(items[items.length - 1].label).toEqual('Salir')

// reset t function mock
i18next.t = original_t
})
})
})

it('limits the number of recent games to show at creation', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/backend/tray_icon/tray_icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const initTrayIcon = async (mainWindow: BrowserWindow) => {
mainWindow.show()
})

ipcMain.on('changeLanguage', async () => {
backendEvents.on('languageChanged', async () => {
await loadContextMenu()
})

Expand Down

0 comments on commit 11a6e66

Please sign in to comment.