Skip to content

Commit

Permalink
fix: add locale in user object
Browse files Browse the repository at this point in the history
  • Loading branch information
gciotola committed Jan 2, 2025
1 parent 481650d commit 3ee0d3b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
12 changes: 11 additions & 1 deletion packages/app-elements/src/providers/I18NProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import resourcesToBackend from 'i18next-resources-to-backend'
import React, { type ReactNode, useEffect, useState } from 'react'
import { I18nextProvider, initReactI18next } from 'react-i18next'
import type en from '../locales/en'
import { useTokenProvider } from './TokenProvider'

export { t } from 'i18next'
export { Trans, useTranslation } from 'react-i18next'
Expand All @@ -22,14 +23,19 @@ declare module 'i18next' {
}

interface I18NProviderProps {
localeCode?: I18NLocale
localeCode: I18NLocale
enforceLocale?: boolean
children: ReactNode
}

export const I18NProvider: React.FC<I18NProviderProps> = ({
localeCode = i18nLocales[0],
children
}) => {
const { user } = useTokenProvider()
const userLocale = user?.locale.split('-')[0]
localeCode = isValidLocaleCode(userLocale) ? userLocale : localeCode

const [i18nInstance, setI18nInstance] = useState<I18nInstance | undefined>()

useEffect(() => {
Expand Down Expand Up @@ -84,3 +90,7 @@ const initI18n = async (localeCode: I18NLocale): Promise<I18nInstance> => {
})
return i18n
}

function isValidLocaleCode(localeCode?: string): localeCode is I18NLocale {
return i18nLocales.includes(localeCode as I18NLocale)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function MockTokenProvider({
fullName: 'John Doe',
id: '1234',
lastName: 'Doe',
timezone: 'Europe/Rome'
timezone: 'Europe/Rome',
locale: 'en-US'
},
organization: {
id: '1234',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ describe('isValidUser', () => {
firstName: 'John',
lastName: 'Doe',
fullName: 'John Doe',
timezone: 'UTC'
}
timezone: 'UTC',
locale: 'en-US'
} as const
expect(isValidUser(user)).toBe(true)
})

Expand Down
3 changes: 2 additions & 1 deletion packages/app-elements/src/providers/TokenProvider/extras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export function isValidUser(
lastName: '',
displayName: '',
fullName: '',
timezone: ''
timezone: '',
locale: 'en-US'
} satisfies TokenProviderAuthUser).sort()

if (user == null || isEmpty(user)) {
Expand Down
1 change: 1 addition & 0 deletions packages/app-elements/src/providers/TokenProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface TokenProviderAuthUser {
displayName: string
fullName: string
timezone: string
locale: 'en-US' | 'it-IT'
}

export interface TokenProviderExtras {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ describe('isValidTokenForCurrentApp', () => {
email: '[email protected]',
timezone: 'Europe/Rome',
displayName: 'R. Starr',
fullName: 'Ringo Starr'
fullName: 'Ringo Starr',
locale: 'en-US'
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export async function isValidTokenForCurrentApp({
fullName: computeFullname(
tokenInfo.owner.first_name,
tokenInfo.owner.last_name
)
),
locale: 'en-US' // setting a default for now, then this will probably arrive from tokenInfo.owner.locale
}
: null,
scopes: jwtInfo.scopes
Expand Down

0 comments on commit 3ee0d3b

Please sign in to comment.