-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
feat: allow to consume translations inside web-components #57
Conversation
This file contains 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
aralroca
commented
Jan 16, 2024
@@ -1,4 +1,4 @@ | |||
import { createContext } from "@/../out/core"; | |||
import { createContext } from "brisa"; |
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.
Probably I should use @/core
directly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #54
After this PR:
href
andsrc
for i18n, trailingSlash, assetPrefix... #55i18nKeys
for dynamic keys (allow regex) #56In this PR: it is possible to consume translations also in web-components (in server components was already done), in web-components only is loading the messages that are consumed inside! If some component is consuming only the
locale
then any message is loaded. All the messages are loaded in a separated chunk, this way the code of the page is cached by the browser when the user changes the language and only needs to load the messages.The hardest part of this PR has been the work on the build to create the chunks with only the messages that are consumed inside, this way the developers don't have to specify which pages which namespaces have to be consumed, it already does it intelligently, improving DX. The only downside is that it only accepts literal keys, if there are variables then it shows a warning to specify these keys, which will be done in this other issue (the good thing is that it will support Glob).
Consume translations
Brisa supports to consume translations inspired by libraries such as i18next and next-translate.
Tip
Good to know: It only occupies 400B of client code if you consume translations in the web-components, if you only use it in server-components, pages, layout, api, middleware, it is 0B of client code.
In order to consume translations, you need first to define the
messages
property insrc/i18n.js
file:After this, you can consume translations in every part of your app through the request context:
middleware
,api
routes,page
routes, all page components,responseHeaders
,layout
,Head
of each page...Important
Important in TypeScript: The generic type
<typeof en>
inI18nConfig
enables type-safe consumption of translations with thet
function by resolving the keys, keys with plurals and nested keys from the preferred locale. This allows IDE autocompletion and type checking of translation keys throughout the codebase, improving productivity and avoiding translation bugs due to typos or invalid keys.The generic
I18nConfig<typeof en>
allows you to activate type-safe consuming translations with thet
function. Displaying to you all the keys from the preferred locale messages, resolving plurals and nested values.Example in a component:
More docs here: https://github.com/aralroca/brisa/blob/995a9cbfb61d3e98ecf0b5397fba936d90ebbafc/docs/02-building-your-application/01-routing/08-internationalization.md