[NPM] How NPM and use function translate into Formula?

Hi, How NPM and use translate into Formula ? Thanks
Capture d'écran 2023-12-12 174403

I would like to translate the values of a JSON, I tried to send Google Translate a JSON.stringify of my Object but Google Translate sometimes makes errors in the structure of the JSON, do you have an idea for translating a JSON?

Hi @thinkcloud :wave:

Cool use case!

I have a few questions for you:

  1. can you clarify what npm library you installed and are trying to work with?
  2. is the JSON always structured the same way? (if so, can you show us an example of a JSON)
  3. can you show us an example with the input value of the JSON and the output value you would want to send to Google Translate?

Hi @Joyce thanks for answer,

  1. i use translate-json-object
  2. The JSON is always the ‘variantes’ object of a shopify product, but i need JSON.stringify to send it at G Translate
  3. here is an example of input that I give to Google Translate API (stringify)
[
  {
    "id": 47356843262266,
    "title": "A / 15-20W / Blanc Froid | CHINE",
    "option1": "A",
    "option2": "15-20W",
    "option3": "Blanc Froid | CHINE"
  },
  {
    "id": 47356843295034,
    "title": "A / 15-20W / Blanc chaud | CHINE",
    "option1": "A",
    "option2": "15-20W",
    "option3": "Blanc chaud | CHINE"
  },
  {
    "id": 47356843327802,
    "title": "A / 15-20W / Gradation 3 couleurs | CHINE",
    "option1": "A",
    "option2": "15-20W",
    "option3": "Gradation 3 couleurs | CHINE"
  },
  {
    "id": 47356843360570,
    "title": "B / 15-20W / Blanc froid | CHINE",
    "option1": "B",
    "option2": "15-20W",
    "option3": "Blanc froid | CHINE"
  },
  {
    "id": 47356843393338,
    "title": "B / 15-20W / Blanc chaud | CHINE",
    "option1": "B",
    "option2": "15-20W",
    "option3": "Blanc chaud | CHINE"
  },
  {
    "id": 47356843426106,
    "title": "B / 15-20W / Gradation 3 couleurs | CHINE",
    "option1": "B",
    "option2": "15-20W",
    "option3": "Gradation 3 couleurs | CHINE"
  },
  {
    "id": 47356843458874,
    "title": "Marron / 15-20W / Blanc froid | CHINE",
    "option1": "Marron",
    "option2": "15-20W",
    "option3": "Blanc froid | CHINE"
  },
  {
    "id": 47356843491642,
    "title": "Marron / 15-20W / Blanc Chaud | CHINE",
    "option1": "Marron",
    "option2": "15-20W",
    "option3": "Blanc Chaud | CHINE"
  },
  {
    "id": 47356843524410,
    "title": "Marron / 15-20W / Gradation 3 couleurs | CHINE",
    "option1": "Marron",
    "option2": "15-20W",
    "option3": "Gradation 3 couleurs | CHINE"
  },
  {
    "id": 47356843557178,
    "title": "Orange / 15-20W / Blanc froid | CHINE",
    "option1": "Orange",
    "option2": "15-20W",
    "option3": "Blanc froid | CHINE"
  },
  {
    "id": 47356843589946,
    "title": "Orange / 15-20W / Blanc chaud | CHINE",
    "option1": "Orange",
    "option2": "15-20W",
    "option3": "Blanc chaud | CHINE"
  },
  {
    "id": 47356843622714,
    "title": "Orange / 15-20W / Gradation 3 couleurs | CHINE",
    "option1": "Orange",
    "option2": "15-20W",
    "option3": "Gradation 3 couleurs | CHINE"
  }
]

Sometimes GTranslate dont make errors, someting he does.
That’s why i wanna use NPM (translate-json-object)

But I am neither familiar with NPM and new to weweb, I did not understand how to use the function of this NPM to translate my JSON.

Hi, looking at the documentation, you have first to

  1. Instantiate the translator
  2. init the translator with the init method
  3. and then you can use it with the translate method

What you did in your screenshot is the first step. By executing the library with (), you generated a instance (object) containing two functions (init and translate).

Also, another important thing from the documentation is, the translation process (the third step) is an asynchronous process, its means its heavy and you need to wait for the process to finish, it will make a request to a google/other service and return the translated text.

We discourage making asynchronous task inside formulas, you should use a global workflow. Please create a global workflow with a javascript action and you should be able to put this inside =>

const translator = <bind-your-npm-package>()
translator.init({ googleApiKey: 'api_key' });
return translator.translate(<bind-your-data>, 'en');

You can add your data and the chosen lang as parameters so your workflow will be reusable

You can add another action when you use this action to update a variable with the result for example.

Really Thanks for help @Alexis , i begin to understand how weweb work and how use NPM.
I will search for my error, but :

message: "Cannot read properties of undefined (reading 'GOOGLE_NAME')"

That error is coming from Weweb side or NPM ?

image

Thanks