Skip to content

Commit ace0fe9

Browse files
committed
Merge branch 'feature/locale-detect' into develop
2 parents 6cb1009 + e58925e commit ace0fe9

File tree

7 files changed

+40
-16
lines changed

7 files changed

+40
-16
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@headlessui/react": "1.7.16",
1818
"@heroicons/react": "2.0.18",
1919
"@lingui/core": "4.3.0",
20+
"@lingui/detect-locale": "4.2.1",
2021
"@lingui/react": "4.3.0",
2122
"clsx": "2.0.0",
2223
"langs": "2.0.0",

pnpm-lock.yaml

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hooks/use-locale-switcher.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ export function useLocaleSwitcher() {
2222
pathName = pathName.replace(`[${k}]`, <string>router.query[k])
2323
})
2424

25-
// languageDetector.cache?.(nextLocale, ['cookie', 'localStorage'])
26-
2725
await router.replace(pathName)
2826
},
2927
[locale, router],

src/hooks/use-locale.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {defaultLocale, Locale} from '@/utils/locales'
2-
import {useRouter} from 'next/router'
1+
import { getLocale,Locale } from '@/utils/locales'
2+
import { useRouter } from 'next/router'
33

44
export function useLocale(): Locale {
55
const router = useRouter()
6-
return (router.query.locale as Locale) || defaultLocale
6+
return getLocale(router.query)
77
}

src/pages/_document.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import {defaultLocale, getLocaleDirection, Locale} from '@/utils/locales'
2-
import {Head, Html, Main, NextScript} from 'next/document'
3-
import {DocumentProps} from 'postcss'
1+
import { getLocale, getLocaleDirection, Locale } from '@/utils/locales'
2+
import { Head, Html, Main, NextScript } from 'next/document'
3+
import { DocumentProps } from 'postcss'
4+
import { ParsedUrlQuery } from 'querystring'
45

5-
type Props = DocumentProps & { __NEXT_DATA__: { query: { locale: string } } }
6+
type Props = DocumentProps & { __NEXT_DATA__: { query: ParsedUrlQuery } }
67

78
export default function Document(props: Props) {
89
const { __NEXT_DATA__: nextData } = props
9-
const locale = nextData?.query?.locale || defaultLocale
10+
const locale = getLocale(nextData.query)
1011

1112
const language = locale as Locale
1213
const direction = getLocaleDirection(language)

src/pages/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { defaultLocale, loadCatalog } from '@/utils/locales'
1+
import {defaultLocale, getLocale, loadCatalog} from '@/utils/locales'
2+
import { GetStaticPropsContext } from 'next'
23
import { useRouter } from 'next/router'
34
import { useEffect } from 'react'
45

56
export default function Home() {
67
const router = useRouter()
78

89
useEffect(() => {
9-
router.replace(`/${defaultLocale}`)
10+
router.replace(`/${getLocale()}`)
1011
}, [router])
1112

1213
return null
1314
}
1415

1516
export const getStaticProps = async () => {
1617
return {
17-
props: { translation: await loadCatalog(defaultLocale) },
18+
props: { translation: await loadCatalog(getLocale()) },
1819
}
1920
}

src/utils/locales.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import linguiConfig from '@/../lingui.config'
2+
import { detect, fromPath } from '@lingui/detect-locale'
23
import langs from 'langs'
4+
import { ParsedUrlQuery } from 'querystring'
35

46
export type Locale = (typeof linguiConfig.locales)[number]
57

@@ -19,10 +21,19 @@ export async function loadCatalog(locale: Locale): Promise<object> {
1921
}
2022
}
2123

22-
export function getLocaleDirection(locale: Locale) {
23-
return ['ar', 'he', 'fa'].includes(locale) ? 'rtl' : 'ltr'
24+
export function getLocale(query?: ParsedUrlQuery): Locale {
25+
const localFromPath = typeof window !== 'undefined' ? fromPath(0) : undefined
26+
const localeFromQuery = query?.locale
27+
28+
const detectedLocale = detect(localFromPath, localeFromQuery)
29+
30+
return detectedLocale || defaultLocale
2431
}
2532

2633
export function getLocaleName(locale: Locale) {
2734
return langs.where(1, locale)!.local
2835
}
36+
37+
export function getLocaleDirection(locale: Locale) {
38+
return ['ar', 'he', 'fa'].includes(locale) ? 'rtl' : 'ltr'
39+
}

0 commit comments

Comments
 (0)