Skip to content

Add support for binding SSR props in Vue 3 directive #869

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 2 commits into from
Apr 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add support for SSR props in Vue 3 directive
  • Loading branch information
Demivan committed Apr 4, 2024
commit 4b7fab3c0541160c8572bc14739b5ca2d2185a81
26 changes: 26 additions & 0 deletions src/vue/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function translate(el: HTMLElement, fluent: TranslationContext, binding: VueDire
for (const [attr, attrValue] of Object.entries(translation.attributes)) {
if (isAttrNameLocalizable(attr, el, allowedAttrs))
el.setAttribute(attr, attrValue)
else
warn(`Attribute '${attr}' on element <${el.tagName.toLowerCase()}> is not localizable. Remove it from the translation. Translation key: ${key}`)
}
}

Expand All @@ -39,6 +41,30 @@ export function createVue3Directive(rootContext: TranslationContext): Vue3Direct
const context = getContext(rootContext, binding.instance)
translate(el, context, binding)
},

getSSRProps(binding) {
const context = getContext(rootContext, binding.instance)
const key = binding.arg
if (key === void 0) {
warn('v-t directive is missing arg with translation key')
return {}
}
const translation = context.formatWithAttrs(key, binding.value)
const allowedAttrs = Object.keys(binding.modifiers)
const attrs: Record<string, string> = {}
for (const [attr, attrValue] of Object.entries(translation.attributes)) {
// Vue 3 does not expose the element in the binding object
// so we can't check if the attribute is allowed
// we assume that all attributes are allowed
// this could lead to SSR hydration mismatches if translation
// contains attributes that are not allowed
// There is a runtime warning in the browser console in case translation contains not allowed attributes
if (isAttrNameLocalizable(attr, {} as HTMLElement, allowedAttrs))
attrs[attr] = attrValue
}

return attrs
},
}
}

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy