-
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: implement createLoadTranslations factory function #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d48da00
feat: implement getFromMessages method
lunarW1TCH 85d2e3c
chore: revert all changes except for exposing the component name on m…
lunarW1TCH 3e644a8
feat: implement createLoadTranslations factory function
lunarW1TCH db1155b
chore: update to latest changes
lunarW1TCH 8bb1879
feat: simplified create-load-translation logic
lunarW1TCH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { | ||
| I18n, | ||
| Messages, | ||
| TranslationLoader, | ||
| Translations | ||
| } from '../create-i18n/index.js' | ||
| import type { LocaleStore } from '../locale-from/index.js' | ||
|
|
||
| export interface LoadTranslations { | ||
| <Body extends Translations>(messages: Messages<Body>): Promise<Body> | ||
| } | ||
|
|
||
| /** | ||
| * Create a function to asynchronously load translations from i18n components. | ||
| * | ||
| * ```js | ||
| * import { createI18n, localeFrom, createLoadTranslations } from '@nanostores/i18n' | ||
| * | ||
| * function get(code, components) { | ||
| * // your fetching logic | ||
| * } | ||
| * | ||
| * export const locale = localeFrom(…) | ||
| * export const i18n = createI18n(locale, { get }) | ||
| * export const loadTranslations = createLoadTranslations(i18n) | ||
| * ``` | ||
| * | ||
| * @param i18n Component definition function. | ||
| * @returns Asynchronous translations loader. | ||
| */ | ||
| export function createLoadTranslations(i18n: I18n): LoadTranslations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { translationsLoading } from '../translations-loading/index.js' | ||
|
|
||
| export function createLoadTranslations(i18n) { | ||
| async function loadTranslations(messages) { | ||
| void messages.get() | ||
| await translationsLoading(i18n) | ||
|
|
||
| return messages.get() | ||
| } | ||
|
|
||
| return loadTranslations | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { atom } from 'nanostores' | ||
| import { deepStrictEqual, equal } from 'node:assert' | ||
| import { test } from 'node:test' | ||
|
|
||
| import { createI18n, createLoadTranslations } from '../index.js' | ||
|
|
||
| test('waits for translations to load', async () => { | ||
| let locale = atom('pl') | ||
|
|
||
| let i18n = createI18n(locale, { | ||
| get() { | ||
| return Promise.resolve({ | ||
| component: { title: 'Tytuł' } | ||
| }) | ||
| } | ||
| }) | ||
| let loadTranslations = createLoadTranslations(i18n) | ||
|
|
||
| equal(i18n.loading.get(), false) | ||
|
|
||
| let messages = i18n('component', { title: 'Title' }) | ||
|
|
||
| deepStrictEqual(await loadTranslations(messages), { title: 'Tytuł' }) | ||
| equal(i18n.loading.get(), false) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
It's different from pre-rendered client components which are later hydrated with loaded translation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://react.dev/reference/rsc/server-components
There was a problem hiding this comment.
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.