Search code examples
javascripthtmlctranslation

Calling C functions inside javascript


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.


Solution

  • 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.