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.

Automatically extract translation keys from templates and place them in location files

See original GitHub issue

Couldn’t find a command for that in this project or in the web.

The workflow I wanted:

  1. Write templates declaring translation keys (like: v-t="'cool_word'").
  2. Run a command that will add all those translation keys to my locale json files.
  3. Check the json files for empty translations and fill them.

This would be much better than adding each key manually to each locale file.

For now, I wrote this Python script as a workaround for some cases: https://gist.github.com/andresmrm/2c2f959a8a5b5d4fd7ab05063f437cc1

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
luckylookecommented, Dec 4, 2019

@rrd108 nice, I didnt know that… I am using vue-i18n-extract, but I will definitely check vue UI if it can be used for my usecase.

here is my script extracting keys from code with vue-i18n-extract

const VueI18NExtract = require('vue-i18n-extract').default;
const path = require('path');
const fs = require('fs');

const vueFilesPath = path.resolve(__dirname, '../src/**/*.?(vue)');
const keysFile = path.resolve(
  __dirname,
  '../src/assets/translations/keys.json'
);

let report = VueI18NExtract.createI18NReport(vueFilesPath, keysFile);

const keysFileContent = fs.readFileSync(keysFile, 'UTF8');
if (report.missingKeys && keysFileContent) {
  const messages = JSON.parse(keysFileContent);
  const missingKeys = report.missingKeys;
  missingKeys.forEach(missingKey => {
    const path = missingKey.path.split('.');
    const lastIndex = path.length - 1;
    let pointer = messages;
    for (let i = 0; i < lastIndex; i++) {
      const pathElement = path[i];
      if (!pointer[pathElement]) {
        pointer[pathElement] = {};
      } else if (typeof pointer[pathElement] !== 'object') {
        throw new Error(
          pathElement + ' in ' + missingKey.path + ' is not an object!'
        );
      }
      pointer = pointer[pathElement];
    }
    pointer[path[lastIndex]] = missingKey.path;
  });
  fs.writeFileSync(keysFile, JSON.stringify(messages, null, 2), 'UTF8');
}

console.log('report', report);

the advantage is that you can run this nodejs script in pipelines or another automated systems… I will check if vue UI can be used same way.

Happy coding! 😃

1reaction
rivaldi8commented, Nov 4, 2022

I’ve been looking for some way to do this with Vue, but the only active project I’ve found is vue3-gettext. The problem is the documentation is a bit sparse and I’m not sure it supports all the features of vue-i18n so as to adopt it in a project. It would be great if vue-i18n supported this kind workflow. Specially being able to directly pass English strings instead of keys to $t()/i18n.t() would be a big improvement.

If it helps as a reference, ngx-translate-extract is a project that does this for Angular’s i18n ngx-translate library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extracting translations - i18next documentation
Static extraction tools can read through your code files to automatically find and export translation keys. i18next-scanner, i18next-parser and ...
Read more >
Localization / Multi-lingual Support — pywb 2.0 documentation
pywb can extract all text from templates and generate CSV files for translation and convert them back into a binary format used for ......
Read more >
Set up continuous translation with Weblate and i18next
Automatically extract translation keys from source code. A last thing to automate a boring step: Copy all translations keys from source code and ......
Read more >
i18next/i18next-scanner: Scan your code, extract ... - GitHub
Scan your code, extract translation keys/values, and merge them into i18n resource files.
Read more >
Automatically extract text and structured data from documents ...
In this post, we show how you can take advantage of Amazon Textract to automatically extract text and data from scanned documents without ......
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