How to use translator module?

Technical Support
  • Hello.

    How to translae many words?

    const translator = require.main.require('./src/modules/translator');
    translator.translate('[[myplugin:greeting1]]', function(translated1) {
        translator.translate('[[myplugin:greeting2]]', function(translated2) {
            console.log('Translated string:', translated1, translated2);

    If I have 100 words?

  • Please provide more context on what you're trying to do. It will help to see if there is a way to avoid translating manually.

    It looks like you're trying to use it server-side. In that case, there are some complicating factors around providing the current user's language.

    Anyways, to import translator you want to do require.main.require('./src/translator')

    To translate a bunch of words there are a couple options:

    • Join tokens into a single string and translate it, then split the tokens, like so
    const tokens = [
    // language is only required if server-side here
    translator.translate(tokens, language, (translated) => {
      const [greeting1, greeting2] = translated.split('|||');
      // etc

    This works well, especially when you need to translate from multiple namespaces, but has a fair amount of overhead.

    • Get translations directly and pick the ones you want, like so
    // language is required here
    translator.getTranslations(language, 'myplugin', (translations) => {
      const { greeting1, greeting2 } = translations;
      // etc

    This works great with single namespaces but with multiple namespaces it gets a little more complicated.

Suggested Topics