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.

Is it possible to know when manageTranslations is finished?

See original GitHub issue

On my project I am trying to create a script that does the following:

  • runs babel to extract the messages
  • runs the translations manager to generate the language files
  • removes the extracted messages directory (at least I don’t see any reason to keep these files…)

The problem is that there is no callback in the manageTranslations function and it also doesn’t return a Promise… Without this I cannot know when the function is finished so I can delete the extracted messages directory.

Is there any way to accomplish this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bensampaiocommented, Jan 2, 2017

Hi @LoicUV,

Here is a sample of what I did:

#! /usr/bin/env node
const glob = require('glob');
const manageTranslations = require('react-intl-translations-manager').default;
const path = require('path');
const shell = require('shelljs');

// List of file paths that might contain messages to be translated.
const srcPaths = glob.sync(path.resolve(process.cwd(), 'src', '**', '*.js'));

// Temporary path where to store the generated JSON files.
const tmpPath = path.resolve(process.cwd(), 'tmp');

// Output path for the locale translation files.
const outputPath = path.resolve(process.cwd(), 'trans');

// List of languages to support
const languages = ['nl', 'es'];

let exitCode = 0;

try {

    // Generate the JSON files for every file that defines messages.
    for (let filePath of srcPaths) {
        require('babel-core').transformFileSync(filePath, {
            plugins: [
                ['react-intl', { messagesDir: tmpPath }],
            ],
        });
    }

    // Manage the translation files for every language
    manageTranslations({
        languages,
        messagesDirectory: tmpPath,
        singleMessagesFile: true,
        translationsDirectory: outputPath,
    });

}
catch (e) {
    console.error(e);
    exitCode = 1;
}
finally {

    // Remove temporary folder when finished
    shell.exec(`rm -rf ${tmpPath}`, (code) => {
        shell.exit(exitCode || code);
    });

}

Then if you are using a tool like webpack together with json-loader you can do something like:

require.ensure('trans/nl.json', (require) => {
    const messages = require('trans/nl.json');
    ReactDOM.render(
        <IntlProvider messages={messages}>
            // your components in here
        </IntlProvider>
    );
});

Hope this helps!

1reaction
bensampaiocommented, Dec 16, 2016

I just realised what the problem was. I was using the transformFile function from babel-core instead of transformFileSync to generate the JSON files. So manageTranslations was being called before all the translation files were there. So my bad… Sorry for bothering you and thank you for the fast replies!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Manage Translations For Your Application | Crowdin ...
Hello Today we are looking at how to manage translations using Crowdin and the GitHub integration. This example is using a React app ......
Read more >
How to manage translations in a software project? Try the new ...
Struggling to manage translations in a software project? Here is an open-source translation management tools called Babelsheet from TSH.
Read more >
Manage translations - Ultimate Subscriptions - Shopify App
All text translation will be done from here. There are 3 options are available to make the text translation: Subscription page; Messages ...
Read more >
Manage translations for custom content | Jira Service ...
Manage translations for custom content · From your service project, go to Project settings > Language support. · Select the language of your...
Read more >
Manage Translations for Sphinx projects - Read the Docs
This guide walks through the process needed to manage translations of your documentation. Once this work is done, you can setup your project...
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