Skip to content

Commit

Permalink
fix(app): detect default navigator language in start hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Jan 30, 2022
1 parent abbda52 commit 76500b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/better-write-app/src/use/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ import { useUtils } from './utils'
import i18n from '@/lang'
import { useI18n } from 'vue-i18n'
import useEmitter from './emitter'
import { useMouse, usePageLeave, useTextSelection } from '@vueuse/core'
import {
useMouse,
useNavigatorLanguage,
usePageLeave,
useTextSelection,
} from '@vueuse/core'
import { watch } from 'vue'

export const useStart = () => {
Expand All @@ -53,6 +58,7 @@ export const useStart = () => {
const { x, y } = useMouse({ type: 'page' })
const selection = useTextSelection()
const isLeft = usePageLeave()
const utils = useUtils()
const { t } = i18n.global

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

const lang = () => {
const { locale } = useI18n()
const lang = localStorage.getItem('lang')

const lang =
localStorage.getItem('lang') ||
utils
.language()
.isoToCode(useNavigatorLanguage().language.value as string)

if (!lang) return

locale.value = lang

const iso =
{
en: 'en-US',
br: 'pt-BR',
}[lang] || 'en-US'

;(document.querySelector('html') as HTMLElement).lang = iso
;(document.querySelector('html') as HTMLElement).lang = utils
.language()
.codeToIso(lang)
}

const initial = () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/better-write-app/src/use/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,28 @@ export const useUtils = () => {
return { assign, getMemorySizeOfObject }
}

const language = () => {
const isoToCode = (iso: string) => {
return (
{
'pt-BR': 'br',
'en-US': 'en',
}[iso] || 'en'
)
}

const codeToIso = (code: string) => {
return (
{
en: 'en-US',
br: 'pt-BR',
}[code] || 'en-US'
)
}

return { isoToCode, codeToIso }
}

return {
position,
delay,
Expand All @@ -248,5 +270,6 @@ export const useUtils = () => {
support,
path,
object,
language,
}
}

0 comments on commit 76500b9

Please sign in to comment.