i18n - usage
Within app/helpers/i18n.ts
, you will find some boilerplate code provided for you which will activate your app's locale configuration. This code uses a util provided by Psychic to provision a curried i18n
function for you, which will carefully absorb the locale shape of your config and use it to provide type completion that will make your life much easier.
// app/helpers/i18n.ts
import { I18nProvider } from '@rvoh/psychic'
import locales from '../../conf/locales'
const i18n = I18nProvider.provide(locales, 'en')
export default i18n
When calling I18nProvider.provide
, the first argument will be the entire payload of locales from your application config, and the second argument will be the specific locale that you are treating as your base locale. This is used to provide type completion as you use the i18n helper.
// elsewhere, in your app
const text = i18n('en-US', 'labels.nutrition.calories')
We recommend that you leverage i18n within your serializer layer, since this can be an incredibly conventient place to make translations before delivering your endpoint results.
See the passthrough setting documentation for details on how to set passthrough data on automatically rendered serializers.
import { DreamSerializer } from '@rvoh/dream'
import { LocalesEnum } from '../../types/db.js'
import i18n from '../../utils/i18n.js'
export const PlaceForGuestsSerializer = (place: Place, passthrough: { locale: LocalesEnum }) =>
PlaceSummaryForGuestsSerializer(place)
.customAttribute('style', () => i18n(passthrough.locale, `places.style.${place.style}`), {
openapi: 'string',
})
.attribute('sleeps')
.rendersMany('rooms', { serializerKey: 'forGuests' })