import { useRouter } from 'next/router' ; import { SupportedLanguage } from '~locales/types' ; import { BUILD_VARIANT } from '~ui/whitelabels' ; export type LocaleObject < K extends string , V extends unknown > = { [ lang in SupportedLanguage ] : { [ key in K ] : V ; } ; } ; export type translateOptions = Partial < { default useEnv : boolean ; } > ; const useLocaleObject = < K extends string , V extends unknown > ( localeObject ) => { type Result = LocaleObject < K , V > [ SupportedLanguage ] ; const router = useRouter ( ) ; const language = router . locale as SupportedLanguage ; const currentLocale = ( localeObject [ language ] || { } ) as Result ; function t < K extends keyof Result > ( key , options = { } ) { const placeholder = `[${ key }]` as Result [ K ] ; const value = currentLocale [ key ] ; const valueEnv = currentLocale [ `${ key }_${ BUILD_VARIANT }` as K ] ; const localeResult = options . useEnv && valueEnv != null ? valueEnv : value ; if ( localeResult == null ) { return placeholder ; } return localeResult ; } return { t , lang : language } ; } ; export default useLocaleObject ;