Translate language (Json / Web Request)

Can you use Json / Web Request to get a translation from, in example, Google translate?
Similar to this topic, were this is used to get the name of a color from ARGB.

In short: yes, you can - Cloud Translation

1 Like

And when bit longer? Link didn’t help me much.

So far i ended up here.

I have like a general idea but have trouble piecing stuff together.

Also my thinking might be a bit to simple and it is not possible getting a translation in the same way
you can get the name of a color as in that other topic.

I have one idea.

  1. Export data do Excel using Dynamo.
  2. Use translate function.
    How to translate a full excel sheet to another language with the Microsoft Translator? - Super User
    or use Google Sheets
    How To Translate a Google Spreadsheet with Google Translate (alphr.com)
  3. Check if everything is correct.
  4. Import back
1 Like

Thanks for your input (suggestion) so far, but i hope someone can answer my original question first.

Hi,

Generally query (web requests) results on translation sites are encoded/encrypted, try to use libraries

example with deep-translator

4 Likes

There have been several of these threads over the years.

One good wxample with step good solutions is here: https://forum.dynamobim.com/t/dual-language-deliverable-can-dynamo-help/44357/u=jacob.small

One thing to keep in mind, web translation services utilize ‘common’ language, not the industry specific language. So if given the phrase Center light on roof soffit and converting to Swedish you’d get Mittljus på taket which translates to Central light on the roof, or perhaps center light on the roof both of which aren’t what is intended. Perhaps the person reading the text will get what you mean, but also it could get you some really odd results.

IMO it’s best to utilize the Google spreadsheet method so that someone familiar with industry terms in both languages can do the human review of everything, before finally pulling the translation back into the file.

also the use of OpenAI can be a solution (no library needed just an OpenAI account)

import sys
import clr
import System
from System.Text import *
from System.Net import *
clr.AddReference("System.Net.Http")
from System.Net.Http import *
import json

def translate_OpenAI(text="maison", source="french", target="english"):
	input = "Hello translate this {0} word '{2}' in {1}".format(source, target, text)
	client = HttpClient()
	client.DefaultRequestHeaders.Add("Authorization", r"Bearer YOUR_API_KEY")
	content = StringContent("{\"prompt\": \"" \
								+ input \
								+ "\", \"model\": \"text-davinci-003\", \"max_tokens\":100,\"temperature\":0.5}", 
								Encoding.UTF8, 
								"application/json")
	#
	response = client.PostAsync("https://api.openai.com/v1/completions", content).Result
	responseString = response.Content.ReadAsStringAsync().Result
	dict_result = json.loads(responseString)
	return dict_result["choices"][0]["text"].strip("\n")

OUT = translate_OpenAI(IN[0])
4 Likes

Yes i know, but in this case I didn’t want to know how, or what is the best way, to translate text using Dynamo, but IF the Json / Web Request approach could be used like in this topic. The translation not being 100% accurate wouldn’t matter for now (read; my question). I could have been more clear about this.
I liked this ‘simplicity’ of it (the Json / Web Request approach).

Bit off-topic;
What other things the Json / Web Request approach could be used for?
Maybe this was a better question to begin with. :upside_down_face:

Certainly can be used. See post 4 in the topic I linked above.

Other uses I have done in the past 7 years or so:

  1. Get standards from the webpage of a industry group, code, or regulation so you know you’re compliant
  2. Get unit pricing for a material to ensure your budget is accurate to current data
  3. Pull the weather data for a site prior to a site visit to populate your field report
  4. Get the list of current Autodesk Security Advisories so you know which users need a software update

Basically any time you’d use a website to look up data you can have Dynamo do that for you.

One more question (i think :sweat_smile:).

Are there any good resources for when you want to use Json / Web Request in Dynamo?
I Googled and looked up a bunch of websites about this topic, but I was a bit overwhelmed / didn’t know where too look or what to look for

Like how this works…

…in relationship to the actual website

image

The colorapi has some (good) documentation but i guess this isn’t the case for every website (which supports this). Also their documentation doesn’t tell you how to use it in Dynamo(?).

The data on the web is effectively the wild west; each site can have it’s own completely unique formatting/configuration if it wanted. So beyond documentation (which may not be present as you noted), the only way to utilize such data is a degree of trial and error.

More or less each JSON object can be reviewed in terms of what keys it has, and then you pull those keys until you find the data you’re looking for. The means of doing so are documented in a few resources online. Effectively to utilize the nodes you need to know what key you’re after. This can be difficult to pin down in really big JSON structures, but it is certainly doable. In the example above if I was after the name and didn’t know the key needed was “name.value” I would return the object directly like this (using a modern Dynamo build because I’d prefer to have new tools in new environments instead of ones which are in the last half of their support cycle):

From there I can review the JSON object to pull any value I might want using standard Dynamo dictionary work - Dictionary.ValueAtKey node or dictionaryObject["Key"]; in DesignScript. Note that often JSONs are nested structures as in the case here. In this case to pull the name value it would be two ValueAtKey nodes or colorObject["name"]["value"]; in design script.
image

Fortunately most web developers want people to be able to utilize the tools they build and as such the response JSON is usually fairly easy to use (at least when developers want people using their service as an API, which is what we are discussing here). As such things are usually straight forward once you have the object.

Here’s another response for weather data:

Without having any prior knowledge of this API I can parse the objects and get what I am after by pulling any individual key and looking at the data it requires.

Hope this helps. :slight_smile:

3 Likes

Thanks for the explanation / your insight so far. Much appreciated!

2 Likes

If I have extracted mtext and mleader contents to excel to be translated to spanish. How can I use dynamo to update the existing mtext and mleaders with the new spanish wording? Are there any dynamo examples of this? Export xls has handle, contents, and type. Looking to edit the exported xls and update the same objects with spanish.

Try by matching text values or use Id.

Hi, you mean no need to pip install openai? no need to import it? @c.poupin

If you use http request you don’t need the python open AI library

See the api doc

https://platform.openai.com/docs/api-reference

2 Likes

can I use function calling too?

for http requests open ai has changed the format of api requests since the last time I wrote this post I will look to update the python code later

1 Like

yes I tried to edit your code but failed
it will be glad you can edit it :slight_smile: and maybe demonstrate a little bit of function calling. I can make it work in python (using pip install openai) but not in dynamo

1 Like