# Cache function for google translate node

**URL:** <https://discourse.nodered.org/t/cache-function-for-google-translate-node/54596>\
**Category:** General\
**Created:** [1 December 2021 18:51 UTC](https://discourse.nodered.org/t/cache-function-for-google-translate-node/54596 "2021-12-01T18:51:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xjust](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xjust/32/39096_2.png) [@xjust](https://discourse.nodered.org/u/xjust)\
**Post date:** [1 December 2021 18:51 UTC](https://discourse.nodered.org/t/cache-function-for-google-translate-node/54596/1 "2021-12-01T18:51:26Z")

</div>

I am using google translate node for some json messages I receive from an API.  
They are usually the same messages that repeats that needs the translation.  
(msg.payload in -\> msg.payload out).  
This function can work on any other function that repeats to avoid cpu or traffic.

How would you cache these translations and bypass google translate in case the translation request already occured.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [1 December 2021 19:59 UTC](https://discourse.nodered.org/t/cache-function-for-google-translate-node/54596/2 "2021-12-01T19:59:05Z")

</div>

You could use flow context storage to build up a object using the input as a key and the output as the value, and then use a function to check if the key exists before making the call out to translate... of course you may have to then manage memory if you an awful lot of phrases as they will keep adding to the space but it should work fine for most use cases I would think.

---

<div class="post-metadata">

**Author:** ![xjust](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xjust/32/39096_2.png) [@xjust](https://discourse.nodered.org/u/xjust)\
**Post date:** [1 December 2021 21:42 UTC](https://discourse.nodered.org/t/cache-function-for-google-translate-node/54596/3 "2021-12-01T21:42:18Z")

</div>

That was my thought. I will check out it works.

The contrib-cache node seems to do it that way as well.

---

<div class="post-metadata">

**Author:** ![xjust](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xjust/32/39096_2.png) [@xjust](https://discourse.nodered.org/u/xjust)\
**Post date:** [2 December 2021 16:53 UTC](https://discourse.nodered.org/t/cache-function-for-google-translate-node/54596/4 "2021-12-02T16:53:16Z")

</div>

in case anyone needs this

```auto
[{"id":"36382ba1.b5d444","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"742d4b4e.101684","type":"inject","z":"36382ba1.b5d444","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test de tradus","payloadType":"str","x":190,"y":120,"wires":[["97dc3508.481708"]]},{"id":"9453fd8f.28212","type":"inject","z":"36382ba1.b5d444","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"test tradus","payloadType":"str","x":180,"y":160,"wires":[["97dc3508.481708"]]},{"id":"2729d227.ae7abe","type":"google-translate","z":"36382ba1.b5d444","name":"","to":"en","x":520,"y":180,"wires":[["23aa9296.2c323e"]]},{"id":"97dc3508.481708","type":"function","z":"36382ba1.b5d444","name":"","func":"var traducere = flow.get(\"traducere\") || {};\nvar tradus = traducere[msg.payload];\nif(tradus){\n msg.payload = tradus;\n return [msg,null];\n}\nelse {\n msg.original = msg.payload;\n return [null,msg];\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","x":360,"y":140,"wires":[["fabb6a1e.7586b8"],["2729d227.ae7abe"]]},{"id":"23aa9296.2c323e","type":"function","z":"36382ba1.b5d444","name":"","func":"var traducere = flow.get(\"traducere\") || {};\ntraducere[msg.original] = msg.payload;\nflow.set(\"traducere\",traducere);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":180,"wires":[["fabb6a1e.7586b8"]]},{"id":"fabb6a1e.7586b8","type":"debug","z":"36382ba1.b5d444","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":140,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 December 2021 16:53 UTC](https://discourse.nodered.org/t/cache-function-for-google-translate-node/54596/5 "2021-12-16T16:53:51Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
