-
Hello, I know this sounds counter intuitive but let me explain. I'm currently trying to implement this library currently using the React adapter and Anyway we would really like to be able to call i18nObject through a LL wrapper (the same way the node adapter works) synchronously. Our use case is that when the app is initiated, the language is loaded and when the customer change the language, the whole app reloads. That means that we only really need to fetch a single language on init. This is why I used the Let me know if any of this make sense and if you have an idea to achieve this. Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @phil714, I think you just have to use the "raw" If you can't make asynchronos calls, you can always import the Your code will look something like this: import { i18nObject } from 'typesafe-i18n'
import type { Translation, TranslationFunctions, Formatters, Locales } from './i18n-types'
i18nObject<Locales, Translation, TranslationFunctions, Formatters>(locale, translations, formatters) Does one of the solutions above fit your needs? |
Beta Was this translation helpful? Give feedback.
-
Hello, Yes I had tried doing what you said but I wasn't using the raw function directly which I guess doesn't make sense. For some reason I thought that this was a singleton so if the react adapter initialized it, I didn't need to initialize the one outside of react context. By using your proposed solution of calling the "raw" // equivalent of App.tsx
// fetch user locale through jwt claims
await init18n(locale);
// finish setting everything up and render for the first time // index.ts in the ./src/i18n directory
import { Locales, TranslationFunctions } from './i18n-types';
import { i18nObject } from './i18n-util';
export async function init18n(locale: Locales): Promise<void> {
LL = await i18nObject(locale);
// init react as well here
}
export let LL: TranslationFunctions; The only downside to this that I can see is that the LL would be undefined if called before the init which make the typing inaccurate and can causes problem. I still think this is an acceptable solution but let me know if you would improve something of this or if that's not exactly what you meant. Thank you |
Beta Was this translation helpful? Give feedback.
Hi @phil714,
if you take a closer look, only the initialization of the
i18nObject
is asynchronos. So you basically would only need to initialize it somewhere and then you could use it in a synchronos way.I think you just have to use the "raw"
i18nObject
function exported fromi18n-util.ts
in addition to the react adapter (you must initialize each version separately).If you can't make asynchronos calls, you can always import the
i18nObject
fromtypesafe-i18n
directly. See here how to initialize it. Just make sure to pass the generated Types as Generics when initializing it to get full typesafety support.Your code will look something like this: