server.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { cookies, headers } from 'next/headers'
  2. import Negotiator from 'negotiator'
  3. import { match } from '@formatjs/intl-localematcher'
  4. import { createInstance } from 'i18next'
  5. import resourcesToBackend from 'i18next-resources-to-backend'
  6. import { initReactI18next } from 'react-i18next/initReactI18next'
  7. import { i18n } from '.'
  8. import type { Locale } from '.'
  9. // https://locize.com/blog/next-13-app-dir-i18n/
  10. const initI18next = async (lng: Locale, ns: string) => {
  11. const i18nInstance = createInstance()
  12. await i18nInstance
  13. .use(initReactI18next)
  14. .use(resourcesToBackend((language: string, namespace: string) => import(`./${language}/${namespace}.ts`)))
  15. .init({
  16. lng: lng === 'zh-Hans' ? 'zh-Hans' : lng,
  17. ns,
  18. fallbackLng: 'en-US',
  19. })
  20. return i18nInstance
  21. }
  22. export async function useTranslation(lng: Locale, ns = '', options: Record<string, any> = {}) {
  23. const i18nextInstance = await initI18next(lng, ns)
  24. return {
  25. t: i18nextInstance.getFixedT(lng, ns, options.keyPrefix),
  26. i18n: i18nextInstance,
  27. }
  28. }
  29. export const getLocaleOnServer = (): Locale => {
  30. const locales: string[] = i18n.locales
  31. let languages: string[] | undefined
  32. // get locale from cookie
  33. const localeCookie = cookies().get('locale')
  34. languages = localeCookie?.value ? [localeCookie.value] : []
  35. if (!languages.length) {
  36. // Negotiator expects plain object so we need to transform headers
  37. const negotiatorHeaders: Record<string, string> = {}
  38. headers().forEach((value, key) => (negotiatorHeaders[key] = value))
  39. // Use negotiator and intl-localematcher to get best locale
  40. languages = new Negotiator({ headers: negotiatorHeaders }).languages()
  41. }
  42. // Validate languages
  43. if (!Array.isArray(languages) || languages.length === 0 || !languages.every(lang => typeof lang === 'string' && /^[\w-]+$/.test(lang)))
  44. languages = [i18n.defaultLocale]
  45. // match locale
  46. const matchedLocale = match(languages, locales, i18n.defaultLocale) as Locale
  47. return matchedLocale
  48. }