question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$t and SFC locales not working in functional components

See original GitHub issue

vue & vue-i18n version

Vue: 2.5.13 Vue-i18n : 7.4.2

Steps to reproduce

  • Create a project with Vue and Vue-i18n
  • Add a locale and some messages
// for example
<i18n>
{
  "fr": {
      "my_message": "Bonjour"
  }
}
  • Create a functional component and user $t function or v-t directive
<template functional>
  <p>{{ $t('my_message') }} <span v-t="'my_message'"></span></p>
</template>

What is Expected?

I’m excpecting to see “Bonjour Bonjour” in a p and a span tag

What is actually happening?

warnings / errors / only keys are rendered


image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Yimiprodcommented, Oct 19, 2018

I’m with @darkylmnx for this issue parent.$t isn’t optimal because it’s working only if it’s the parent component who have the <i18n> translation defined in it.

This mean if i need to make a simple translation component, i can’t do it functional:

<i18n>
{
  "fr": {
    "greet": "Bonjour, {name}"
  },
  "en": {
    "greet": "Hello {name}"
  },
  "de": {
    "greet": "Hallo {name}"
  },
  "es": {
    "greet": "Hola {name}"
  },
  "it": {
    "greet": "Ciao {name}"
  },
  "nl": {
    "greet": "Hallo {name}"
  },
  "pt": {
    "greet": "Ola {name}"
  }
}
</i18n>

<script>
export default {
  name: 'Greeter',
  functional: true,
  props: {
    name: {
      type: String,
      required: true,
    },
  },
  render(h, { parent, props }) {
    const { name } = props;
    return (<span>{parent.$t('greet', { nam })}</span>);
  },
};
</script>

It will not work in two case:

  • If there’s no traduction in the file (no <i18n> tags)
  • If the parent is functional
<template>
  <div><greeter :name="username" /></div>
</template>

<script>
import Greeter from './Greeter.vue';

export default {
  name: 'ParentGreeter',
  components: {
    Greeter,
  },
  data() {
    return {
      username: 'Warudo',
  },
}
</script>

OR

<template functional>
  <div><greeter :name="username" /></div>
</template>

<script>
import Greeter from './Greeter.vue';

export default {
  name: 'ParentGreeter',
  components: {
    Greeter,
  },
  props: {
    username: {
      type: String,
      default: 'Warudo',
    },
  },
}
</script>
1reaction
darkylmnxcommented, Jun 18, 2018

@kimuraz as I said it’s not working for $t and SFC, what you said works for “$t” yes but SFC i18n tag still doesn’t work from what I saw

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to type Stateless Functional Component using styled ...
Typescript says to me that props style does not exist in my type SFC<{}> . Indeed I defined it nowhere. What I tried....
Read more >
React Stateless Functional Components: Nine Wins You Might ...
It's a quick way to hack in a feature. Since stateless functional components don't support local state, you can't easily hack in some...
Read more >
Functional React Components in TypeScript
But React encourages stateless functional components (SFCs) for simple, presentational components. Let's convert our App component to a TypeScript-driven ...
Read more >
How to translate your Vue.js application with vue-i18n
The directory where store localization messages of project. It's stored under src directory: locales; Enable locale messages in Single file components ? y....
Read more >
Airbnb React/JSX Style Guide
And if you don't have state or refs, prefer normal functions (not arrow functions) over classes: // bad class Listing extends React.Component {...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found