feat: implement createLoadTranslations factory function#45
feat: implement createLoadTranslations factory function#45ai merged 5 commits intonanostores:mainfrom
Conversation
|
It is 10% size increase.
|
|
I wanted to make it a separate method initially but opted for method bound to the i18n object to use the existing logic. const get = async () => {}
const i18n = createI18n(locale, { get })
export const getTranslations = createGetTranslations(i18n, get) // factory function Get translations would still accept |
|
Maybe we can call is |
|
|
|
I rewrote the logic into a separate factory function |
| @@ -0,0 +1,17 @@ | |||
| export function createLoadTranslations(i18n, locale, get) { | |||
There was a problem hiding this comment.
Do we need to pass locale? I think we can use it from i18n.locale
There was a problem hiding this comment.
you are completely right here, i didn't notice that i18n exposes locale
| export { locale, i18n } | ||
| ``` | ||
|
|
||
| ### Loading translations asynchronously |
There was a problem hiding this comment.
What is use cases for that? SSR?
Why do we use it in React components?
There was a problem hiding this comment.
It's for rendering async server components which aren't updated on the client side. The translation must be available before server sends the rendered html to client.
async function Component(): Promise<React.ReactNode> {
// ...
}It's different from pre-rendered client components which are later hydrated with loaded translation.
There was a problem hiding this comment.
There was a problem hiding this comment.
Nice explanation, let’s put it to the docs.
| ) | ||
| } | ||
|
|
||
| i18n.cache[code] = { ...i18n.cache[code], translations } |
There was a problem hiding this comment.
If we use cache do we need to check it (and return cached version is available)?
There was a problem hiding this comment.
I'll add checks for it
|
I have simplified the logic, calling i marked first get call with |
|
Looks good to me. But I am a little busy on this week. I will try to merge and release it a little later. |
|
After thinking about it, I hope to find way to simplify API: c94fd4d |
|
|
I added the
getFromMessagesmethod on i18n object, a ready solution for server-side rendering instead of manually setting the cache or triggering the update and then waiting.