Accessing electron stuff outside of main / renderer process


#1

Hi folks,

so I have small utility classes, like one that does format some strings formating for me. Then i my main I do:

const Utils = require('./utils/utils')

....

Utils.formatForMe(1000)

formatForMe can return some arbitrary string. (I have few more similar classes)

So now I am adding i18n. On my main I create i18n object that I can then use in main, but I don’t know how to “have it available” inside my utils classes.

Obviously (at least as I understand it), I can’t use global. (With global I have it available in my renderer stuff).

Many thanks for any ideas ;]


#2

I feel like you should be able to export the i18n object from main and require it in the utils modules.


#3

agreed with @DamnedScholar.

or you can try to use simple dependency injection pattern to set i18n object.
e.g.

const i18nObj = require('./utils/i18n');
Utils.setI18n(i18nObj);

then you can have i18n Object to your utils class.


#4

Thanks guys, I will need to read more about it as my knowledge does not go that far.

I’ll be more specific here in what I do so maybe you can point me in the best direction:

in my main.js I do: (i use i18next)

const i18next = require('i18next')
const Backend = require('i18next-node-fs-backend')
startI18next()

function startI18next () {
  i18next
    .use(Backend)
    .init({
      lng: 'en',
      fallbackLng: 'en',
      debug: true,
      backend: {
        loadPath: `${__dirname}/locales/{{lng}}.json`,
        jsonIndent: 2
      }
    }, function(err, t) {
      global.shared.i18next = i18next
    })
}

Then in renderer I use remote.getGlobal('shared').i18next.

So what I don’t know how to do is using it in files where remote is not available.

I’ll read about dependency injection patter and exports and try it out.


#5

so I’m trying export now, but no success:

in main:

module.exports.t = i18next.t

in utils:

const t = require('../main.js')

let formatRemaining = function (seconds) {
  if (seconds < 60) {
    return `${seconds + 1} ${t('secondsLeft')}`
  } else {
    return `${Math.trunc((seconds / 60) + 1)} ${t('minutesLeft')}`
  }
}

But I’m getting: typeError: t is not a function


#6

not sure you do you refer ‘t’ with mainProcess.t ?

you can potentially access t() directly if you export it properly.


#7

Sorry, I pasted wrong code. I’ve edited the post with correct code. I’m using t() directly with no success.


#8

So I partially got it to work:

In my utils I only do const i18next = require('i18next'). This is fine if I use Utils.formatForMe(1000) in main.

It is not working if I try to use Utils.formatForMe(1000) in renderer.

I guess I want too much.


#9

how do you use in renderer process?
you can see how is the error on renderer process with Developer Tools window as in Google Chrome.
this will help you to know more detail of reason why it did not work.


#10

There is no error, the renderer gives undefined.

I use it like this:

in window I reference js:

    <script>
      require('./break.js')
    </script>

than in break.js:

const Utils = require('./utils/utils')
someElement.innerHTML= Utils.formatForMe(1000) // undefined


#11

Interesting is that if I print i18next in my Utils class, while called via main.js, I am getting: I18n { // a lot of stuff}
But when I call Utils function in renderer, it prints [17957:1130/205124.176849:INFO:CONSOLE(7)] "[object Object]", source: /home/conta/workspace/hovancik/stretchly/app/utils/utils.js (7)


#12

solution:

const {remote} = require('electron')
const Utils = remote.require('./utils/utils')