, default: options.componentTag },
+ args: { type: Object, default: () => ({}) },
+ html: { type: Boolean, default: false },
+ noTag: { type: Boolean, default: false },
+ },
+ setup(props, { slots, attrs }) {
+ const instance = getCurrentInstance()
+ const parent = getParentWithFluent(instance?.proxy)
+ const fluent = getContext(rootContext, parent)
+
+ const translation = computed(() => {
+ const fluentParams = Object.assign(
+ {},
+ props.args,
+ // Create fake translation parameters for each slot.
+ // Later, we'll replace the parameters with the actual slot
+ ...Object.keys(slots).map(key => ({
+ [key]: `\uFFFF\uFFFE${key}\uFFFF`,
+ })),
+ )
+
+ const result = fluent.formatWithAttrs(props.path, fluentParams)
+
+ const camelizedAttrs = Object.fromEntries(
+ Object.entries(result.attributes).map(([key, value]) => [camelize(key), value]),
)
- }
- // No way to type this properly, so we'll just use `any` for now.
- const processNode = (node: SimpleNode): any => {
- if (node.nodeType === 3) { // Node.TEXT_NODE
- return insertSlots(node.nodeValue)
+ return {
+ value: result.value,
+ attributes: camelizedAttrs,
+ }
+ })
+
+ const insertSlots = (text: string | null) => {
+ return text?.split('\uFFFF')
+ .map(text =>
+ text.startsWith('\uFFFE')
+ ? slots[text.replace('\uFFFE', '')]!(translation.value.attributes)
+ : text,
+ )
}
- else if (node.nodeType === 1) { // Node.ELEMENT_NODE
- const el = node as Element
-
- return h(
- el.nodeName.toLowerCase(),
- {
- ...Object.fromEntries(
- Array.from(el.attributes).map(attr => [attr.name, attr.value]),
- ),
- },
- Array.from(el.childNodes).map(node => processNode(node)))
+
+ // No way to type this properly, so we'll just use `any` for now.
+ const processNode = (node: SimpleNode): any => {
+ if (node.nodeType === 3) { // Node.TEXT_NODE
+ return insertSlots(node.nodeValue)
+ }
+ else if (node.nodeType === 1) { // Node.ELEMENT_NODE
+ const el = node as Element
+
+ return h(
+ el.nodeName.toLowerCase(),
+ {
+ ...Object.fromEntries(
+ Array.from(el.attributes).map(attr => [attr.name, attr.value]),
+ ),
+ },
+ Array.from(el.childNodes).map(node => processNode(node)))
+ }
+
+ // Ignore other node types for now.
+ warn(`Unsupported node type: ${(node as any).nodeType}. If you need support for it, please, create an issue in fluent-vue repository.`)
+ return []
}
- // Ignore other node types for now.
- warn(`Unsupported node type: ${(node as any).nodeType}. If you need support for it, please, create an issue in fluent-vue repository.`)
- return []
- }
+ const children = computed(() => {
+ // If the message value doesn't contain any markup nor any HTML entities, return it as-is.
+ if (!props.html || !reMarkup.test(translation.value.value))
+ return insertSlots(translation.value.value)
- const children = computed(() => {
- // If the message value doesn't contain any markup nor any HTML entities, return it as-is.
- if (!props.html || !reMarkup.test(translation.value.value))
- return insertSlots(translation.value.value)
+ // Otherwise, parse the message value as HTML and convert it to an array of VNodes.
+ const nodes = fluent.options.parseMarkup(translation.value.value)
+ return nodes.map(processNode)
+ })
- // Otherwise, parse the message value as HTML and convert it to an array of VNodes.
- const nodes = fluent.options.parseMarkup(translation.value.value)
- return nodes.map(processNode)
- })
+ if (isVue2 && (props.tag === false || props.noTag))
+ warn('Vue 2 requires a root element when rendering components. Please, use `tag` prop to specify the root element.')
- return () => h(props.tag, { ...attrs }, children.value)
- },
-})
+ return () => props.tag === false || props.noTag ? children.value : h(props.tag, { ...attrs }, children.value)
+ },
+ })
+}
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