Skip to content

Commit 76500b9

Browse files
committed
fix(app): detect default navigator language in start hook
1 parent abbda52 commit 76500b9

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

packages/better-write-app/src/use/start.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ import { useUtils } from './utils'
3434
import i18n from '@/lang'
3535
import { useI18n } from 'vue-i18n'
3636
import useEmitter from './emitter'
37-
import { useMouse, usePageLeave, useTextSelection } from '@vueuse/core'
37+
import {
38+
useMouse,
39+
useNavigatorLanguage,
40+
usePageLeave,
41+
useTextSelection,
42+
} from '@vueuse/core'
3843
import { watch } from 'vue'
3944

4045
export const useStart = () => {
@@ -53,6 +58,7 @@ export const useStart = () => {
5358
const { x, y } = useMouse({ type: 'page' })
5459
const selection = useTextSelection()
5560
const isLeft = usePageLeave()
61+
const utils = useUtils()
5662
const { t } = i18n.global
5763

5864
// set global mouse tracking
@@ -122,19 +128,19 @@ export const useStart = () => {
122128

123129
const lang = () => {
124130
const { locale } = useI18n()
125-
const lang = localStorage.getItem('lang')
131+
132+
const lang =
133+
localStorage.getItem('lang') ||
134+
utils
135+
.language()
136+
.isoToCode(useNavigatorLanguage().language.value as string)
126137

127138
if (!lang) return
128139

129140
locale.value = lang
130-
131-
const iso =
132-
{
133-
en: 'en-US',
134-
br: 'pt-BR',
135-
}[lang] || 'en-US'
136-
137-
;(document.querySelector('html') as HTMLElement).lang = iso
141+
;(document.querySelector('html') as HTMLElement).lang = utils
142+
.language()
143+
.codeToIso(lang)
138144
}
139145

140146
const initial = () => {

packages/better-write-app/src/use/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,28 @@ export const useUtils = () => {
236236
return { assign, getMemorySizeOfObject }
237237
}
238238

239+
const language = () => {
240+
const isoToCode = (iso: string) => {
241+
return (
242+
{
243+
'pt-BR': 'br',
244+
'en-US': 'en',
245+
}[iso] || 'en'
246+
)
247+
}
248+
249+
const codeToIso = (code: string) => {
250+
return (
251+
{
252+
en: 'en-US',
253+
br: 'pt-BR',
254+
}[code] || 'en-US'
255+
)
256+
}
257+
258+
return { isoToCode, codeToIso }
259+
}
260+
239261
return {
240262
position,
241263
delay,
@@ -248,5 +270,6 @@ export const useUtils = () => {
248270
support,
249271
path,
250272
object,
273+
language,
251274
}
252275
}

0 commit comments

Comments
 (0)