Skip to content

Commit

Permalink
options: new "english" option
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed Dec 13, 2024
1 parent 4393f6f commit 15685c3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 4 additions & 1 deletion frontend/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { i18nFromTranslations } from '../../src/i18n'
import { getHFS } from '@hfs/shared'
import { state } from './state'

export default i18nFromTranslations(getHFS().lang || {})
const i18n = i18nFromTranslations(getHFS().lang || {})
i18n.state.disabled = state.disableTranslation
export default i18n
12 changes: 10 additions & 2 deletions frontend/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getHFS, hIcon, MAX_TILE_SIZE, SORT_BY_OPTIONS, THEME_OPTIONS } from './
import { MenuLink } from './menu'
import _ from 'lodash'
import i18n from './i18n'
const { t } = i18n
const { t, useI18N } = i18n

export function showOptions (){
newDialog({
Expand All @@ -20,6 +20,7 @@ export function showOptions (){

function Content(){
const snap = useSnapState()
const {t} = useI18N()
return h(FlexV, { gap: '1.5em' },
snap.adminUrl && h(MenuLink, {
icon: 'admin',
Expand Down Expand Up @@ -74,7 +75,14 @@ export function showOptions (){
onChange(v) {
state.theme = v
}
})
}),

h(Checkbox, {
value: snap.disableTranslation,
onChange(v) {
i18n.state.disabled = state.disableTranslation = v
}
}, "English"),
)
}
}
1 change: 1 addition & 0 deletions src/cross.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const FRONTEND_OPTIONS = {
sort_numerics: false,
theme: '',
auto_play_seconds: 5,
disableTranslation: false,
}
export const SORT_BY_OPTIONS = ['name', 'extension', 'size', 'time']
export const THEME_OPTIONS = { auto: '', light: 'light', dark: 'dark' }
Expand Down
12 changes: 6 additions & 6 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import { proxy, useSnapshot } from 'valtio'
import { watch } from 'valtio/utils'

export function i18nFromTranslations(translations: Record<string, any>) {
export function i18nFromTranslations(translations: Record<string, any>, embedded='en') {
const state = proxy({
embedded: '',
embedded,
disabled: false,
translations, // all dictionaries
})
const searchLangs: string[] = []
Expand Down Expand Up @@ -35,7 +36,7 @@ export function i18nFromTranslations(translations: Record<string, any>) {
let selectedLang = '' // keep track of where we find the translation
const { embedded } = state
const langs = Object.keys(state.translations)
for (const key of keys) {
if (!state.disabled) for (const key of keys) {
for (const lang of searchLangs)
if (found = state.translations[selectedLang=lang]?.translate?.[key]) break
if (!warns.has(key) && langs.length && langs[0] !== embedded) {
Expand Down Expand Up @@ -113,10 +114,9 @@ export function i18nFromTranslations(translations: Record<string, any>) {

return {
t,
state,
getLangs,
i18nWrapperProps(embedded='en') {
return { lang: getLangs()[0] || (state.embedded = embedded) }
},
i18nWrapperProps: () => ({ lang: getLangs()[0] || state.embedded }),
useI18N() { // the hook ensures translation is refreshed when language changes
useSnapshot(state)
return { t }
Expand Down

0 comments on commit 15685c3

Please sign in to comment.