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.

Dynamic translation keys

See original GitHub issue

So I’m not sure how to solve this really, but here’s the problem.

It picks this up as a Missing i18n Entry lang.label.${choice}

And in my lang files it picks these up as Unused i18n Entry

lang.label.ar-AE
lang.label.de-DE
lang.label.en-US
lang.label.es-ES
lang.label.fr-CA
lang.label.fr-FR
lang.label.he-IL

My code has this within a loop where choice is one of [ar-AE, de-DE, en-US, etc]:

<label :for="choice">{{ $t(`lang.label.${choice}`) }}</label>

Could your tool treat the ${choice} as a wildcard somehow and therefore not flag these items as missing?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
ghostcommented, Oct 18, 2019

Proposal 1

A quick workaround could be a “classic” ignore comment to tell vue-i18n-extract to ignore some entries we know are highly dynamics.

code:

translateOption(field, opt) {
  return opt === null || opt === '' ? '' : this.$t(`${field}.${opt}`); // vue-i18n-extract: ignore
},

translation:

export default {
  field: {
    example: 'some translation', // vue-i18n-extract: ignore
  }
}

Proposal 2

Building on the previous example we can use the comment to “tag” the dynamic lines with the used strings, as a mapping, to avoid dangling translations in case of refactoring.

code:

translateOption(field, opt) {
  return opt === null || opt === '' ? '' : this.$t(`${field}.${opt}`); // vue-i18n-extract: tag=ABC
},

translation:

export default {
  field: {  // vue-i18n-extract: ABC:field
    example: 'some translation', // vue-i18n-extract: ABC:opt
  }
}
2reactions
Spittalcommented, Apr 12, 2019

Yeah there are several occasions where you want some dynamic content in your paths. I’ve seen this with:

  1. The v-t directive that allows for an object to be passed through, as opposed to a string literal.
  2. A case that I patched out a while ago https://github.com/pixari/vue-i18n-extract/commit/30fb684b2a25fb1e3dcdb7ff32e165dde9e26e6d which allows for using a dynamic :path in a <i18n> component
  3. A template literal variable like in the example the OP posted.

I’ve thought a lot about how we can achieve support for dynamic paths… Let me sort of ‘brain dump’ here and maybe someone smarter than me can chime in:

  1. Recognize via the RegExp matchers that an I18NItem path is dynamic. Dynamic can mean different things (see the list above) so we need ways to know which ‘type’ of dynamic path we have.
  2. Somehow get the component name associated with the dynamic path. This is harder than is sounds, because we can’t assume the template and component script are always in the same file.
  3. Once we have the component name, boot up the Vue app and find the component based on it’s name. This isn’t full proof because we’re just assuming that the component will be in the tree, which isn’t always the case…

This is where I hit a wall, there isn’t some ‘full proof’ way to evaluate the dynamic path without making a lot of assumptions about the users source code.

So, I’m open to hearing some other solutions. Or, possible workarounds we should document to make sure users know that this is a limitation of the library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dynamically construct translation keys for input ...
For this I am trying to generate the translation keys dynamically. My current attempt in html code looks like this:.
Read more >
Dynamic Translation - Product Documentation | ServiceNow
Dynamic Translation enables you to add mapping between the ServiceNow® language codes and the language codes used by the translation service ...
Read more >
Translating Dynamic Content (Strings) - Phrase
Use Phrase to translate dynamic segments, e.g. blog posts or product information ... as keys and values, decide on a key structure for...
Read more >
Translation Keys - BigCommerce Dev Center
Translation keys exist in JSON files and are invoked based on the user's browser language. With a Stencil theme, you can define multiple...
Read more >
Dynamic Translations in Angular - Christian Lüdemann
This post is going to cover how to fix this by introducing dynamic translation capabilities using ngx-translate for getting translations from a NodeJS...
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