Skip to content

feat: implement createLoadTranslations factory function#45

Merged
ai merged 5 commits intonanostores:mainfrom
lunarW1TCH:get_messages
Apr 13, 2026
Merged

feat: implement createLoadTranslations factory function#45
ai merged 5 commits intonanostores:mainfrom
lunarW1TCH:get_messages

Conversation

@lunarW1TCH
Copy link
Copy Markdown
Contributor

I added the getFromMessages method on i18n object, a ready solution for server-side rendering instead of manually setting the cache or triggering the update and then waiting.

@ai
Copy link
Copy Markdown
Member

ai commented Mar 30, 2026

It is 10% size increase.

  1. Maybe we should do it as function, not a method (so we will not put it to client-side JS bundle)?
  2. Also, I would like a more clear name, right now it is not clear for me

@lunarW1TCH
Copy link
Copy Markdown
Contributor Author

I wanted to make it a separate method initially but opted for method bound to the i18n object to use the existing logic.
Maybe for a separate function I could do something like:

const get = async () => {}
const i18n = createI18n(locale, { get })

export const getTranslations = createGetTranslations(i18n, get) // factory function 

Get translations would still accept messages store, fetch translations, set them in cache and then return the translations. Is this a better approach? Do you think createGetTranslations is a fitting name?

@ai
Copy link
Copy Markdown
Member

ai commented Mar 30, 2026

Maybe we can call is loadTransaltions/loadAllTranslations or something with load verb, because we already have messages.get() (with get verb) and need to explain the difference.

@lunarW1TCH
Copy link
Copy Markdown
Contributor Author

loadTranslations should be fine, I will work on this new implementation tomorrow then

@lunarW1TCH
Copy link
Copy Markdown
Contributor Author

I rewrote the logic into a separate factory function

Comment thread create-load-translations/index.js Outdated
@@ -0,0 +1,17 @@
export function createLoadTranslations(i18n, locale, get) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to pass locale? I think we can use it from i18n.locale

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are completely right here, i didn't notice that i18n exposes locale

Comment thread README.md
export { locale, i18n }
```

### Loading translations asynchronously
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is use cases for that? SSR?

Why do we use it in React components?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice explanation, let’s put it to the docs.

Comment thread create-load-translations/index.js Outdated
)
}

i18n.cache[code] = { ...i18n.cache[code], translations }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use cache do we need to check it (and return cached version is available)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add checks for it

Comment thread create-load-translations/index.test.ts Outdated
Comment thread create-load-translations/index.test.ts Outdated
@lunarW1TCH
Copy link
Copy Markdown
Contributor Author

I have simplified the logic, calling messages.get() begins the process of loading translations for the specific component, then i am using the built in translationsLoading function to block the return until they load. No need for passing anything else than i18n object.

i marked first get call with void to make it clearer that the return value for that line is not used and only the side-effect matters.

@lunarW1TCH lunarW1TCH changed the title feat: implement getFromMessages method feat: implement createLoadTranslations factory function Apr 5, 2026
@ai
Copy link
Copy Markdown
Member

ai commented Apr 7, 2026

Looks good to me.

But I am a little busy on this week. I will try to merge and release it a little later.

@ai ai merged commit 830e288 into nanostores:main Apr 13, 2026
4 checks passed
@ai
Copy link
Copy Markdown
Member

ai commented Apr 13, 2026

After thinking about it, I hope to find way to simplify API: c94fd4d

@ai
Copy link
Copy Markdown
Member

ai commented Apr 13, 2026

loadTransaltions() was released in 1.3.0. Thanks for the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants