Skip to content

FormattedMessage component for vue-intl #3961

Answered by mediafreakch
mediafreakch asked this question in Q&A
Discussion options

You must be logged in to vote

So finally I got it working. I think this could be incorporated into the vue-intl package. It allows to apply rich text formatting the same way as in react-intl (at least in translation files).

The trick was to use another component to output the content given to the scoped slots:

<script setup>
import { h, ref } from "vue";
import { useIntl } from "vue-intl";

const FormattedMessage = (props, context) => {
  const { id, tag = "div", values = {} } = props;
  const intl = useIntl();
  const slotNames = Object.keys(context.slots);

  const message = intl.formatMessage(
    { id },
    {
      ...values,
      ...slotNames.reduce((slots, name) => {
        slots[name] = (content) => context.s…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@vjee
Comment options

Answer selected by mediafreakch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants