9

The javascript is printing out the HTML onto the page example below, is it possible to call a C function on it for example in C to convert something to another language there is a function LANG_Str("text") which converts the text into the specified language. Would it be possible to use this function on the below text inside Javascript?.

"<tr><th>Service</th><th>Target Allocation (%)</th><th></th>"

EDIT:

I'm basically wanting to do a human language translation. The site already supports multi-language, the problem is on the custom screen like the one shown above which gets generated in Javascript, cannot use the function used to translate text the way its done normally in C.

6
  • From inside a web browser? No. Commented Jun 21, 2012 at 13:13
  • How is this string generated? If you're generating it from C, then yes, you can do it. If you're generating it from JavaScript, then no. Commented Jun 21, 2012 at 13:13
  • Do you need to implement the i18n of your app? It should definitely be done in another way... Commented Jun 21, 2012 at 13:15
  • The script is ran in C using HTML_WriteToCgi Commented Jun 21, 2012 at 13:16
  • Is the root of your question "how can I create a multi-language (English, Spanish, Mandarin) site?" Commented Jun 21, 2012 at 13:17

6 Answers 6

9

If it's running in the browser: no. Sorry.

You might be able to do it in server-side code beforehand (e.g. Python or PHP which can call C) when putting together the page content. Alternatively you can make an AJAX request to a server which exposes the C function as an HTTP API/Endpoint (via, GCI, FCGI or Python/PHP/Perl). But not in the browser.

This is because the JS runs in a sandboxed virtual environment which has no access to system calls or anything outside the runtime.

EDIT

In response to your comment "The script is ran in the C using HTML_WriteToCgi", this suggests that you are putting together the HTML in C on your server. If this is correct, go for my option 1 above, by injecting the values directly into the JS source code if all values come out of some data known by the server.

You might consider moving some functionality out of browser JS and back into server-side code to solve your problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Would it be a good idea to move all the JS code out and instead just WriteToCgi the javascript code?, this should allow me to call the function inside.
That is certainly possible, yes. Alternatively, if you are calling a localisation function in JS, and you know all the values that will be used, you can build a JavaScript map of strings to translated values and simulate the input/output of the C function.
This answer isn't entirely correct: it's possible to compile C to client-side JavaScript using EngScripten.
3

You can make a special request, so the webserver can use that request and send it to the webpage.

Comments

2

JavaScript can't access any other processes directly, but it can make a server request for the information. The server can call a C function if need be.

In the end, it's not JavaScript calling the C function, it's the server (and whatever language it's using: Python, PHP, ASP.NET, JSP, etc) that would be calling the C function.

Comments

1

My interpretation is that your goal is to call a C function within HTML / Javascript and capture the output.

What you could do is make a VM. Basically, you have a huge array "memory", a couple of "registers", etc... The hardest part is to make sure that they instruction set and the bytecodes of your VM mirrors some common instruction set that there is a C compiler for. You compile the C code that VM on your computer, save it to a file, and run it on the VM. If doing that is too hard, you could just get a C to assembly converter, and just define a couple of Assembly instructions instead. There is a Linux emulator in pure javascript with no server calls that does precisely that.

Comments

1

I’m not an expert on web development. But isn’t it possible for javascript to invoke c using webassembly? Not sure of it’s limitation/constraints though - such as memory

Something like this? https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm

Comments

0

You might consider creating a RESTful web service on your server that will receive the source text and target language id, then return the translated text. You could then access it from your webpage via an ajax call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.