I am using jquery-localize plugin for translating my page. I wonder is it possible to load dynamically created JSON data (sent from server) instead of using pre-made files. It would be a lot easier to maintain since my web and desktop application share about 95% of text (so I am using same .resx file for both).
So, i loaded translations using ajax:
$.ajax({
type: "POST",
url: "Helper.aspx/LocalizePage",
data: '{"lang":"' + lang + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d != "error") {
console.log(msg.d);
translationData = msg.d;
var translationTable = jQuery.parseJSON(msg.d);
}
},
error: function (response) {
var responseTextObject = jQuery.parseJSON(response.responseText);
console.log(responseTextObject);
}
});
and i have parsed (translationTable) and unparsed (translationData) text. Unparsed is same as jquery-localize requires. So, how to use this data as source for it?
You can have your ajax/jquery reference a server side page that's written in php/asp or whatever preferred server side language. And then have that page return text in JSON format based on the criteria you pass to the page.
So, here is the solution. Bryant gave me idea what to do (load translation file via separate page) but it requires some modifications. First, we need to modify jquery-localize.js file. In localize function, alter switch so it looks like this:
switch (level) {
case 1:
intermediateLangData = {};
if (options.loadBase) {
//file = pkg + ("." + fileExtension);
file = pkg;
return jsonCall(file, pkg, lang, level);
} else {
return loadLanguage(pkg, lang, 2);
}
break;
case 2:
if (lang.length >= 2) {
//file = "" + pkg + "-" + (lang.substring(0, 2)) + "." + fileExtension;
file = pkg;
return jsonCall(file, pkg, lang, level);
}
break;
case 3:
if (lang.length >= 5) {
//file = "" + pkg + "-" + (lang.substring(0, 5)) + "." + fileExtension;
file = pkg;
return jsonCall(file, pkg, lang, level);
}
Notice commented original lines. We won't do any alterations of "filename" in my case, so be careful if you are doing your implementation (you might need to check requested language).
Other part of job is change how localization is initialized. Instead of standard $("[data-localize]").localize("filename"); we are going to use following line:
$("[data-localize]").localize("Localization.aspx?lang=en-US");
This Localize.aspx is dummy page which has following PageLoad event:
protected void Page_Load(object sender, EventArgs e)
{
string json = "";
var lang = Request.QueryString["lang"];
if (lang.Length > 0)
{
json = WebHelper.GetAllTranslations(lang);
}
else
{
json = WebHelper.GetAllTranslations("en-US");
}
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();
}
This WebHelper.GetAllTranslations(lang) returns JSON-formatted string with translations, just like you would have them in single file for regular use.
To sum up - instead of using file with translations, we modified jquery-localize.js so it loads translations directly from string, returned from page. This is simple and bit crude modification but it does it job.
Of course, you can use AJAX call or something else - important part is to modify jquery-localize that it doesn't search for files, but loads translation directly.
I hope this will help someone with similar problem.
Related
Let me explain: My purpose is to create moodle users from a web app.
I am implementing a web app on Tomcat 8.0.15.0. That is, I use java servlets on the server side. Many JSP files and javascript, with much of it in jQuery, resides on the client side.
On the other hand, on another server, I have a test moodle installation. Via site-administration> plugins> web services> external services, I created a ws that enables the core_user_create_users function. Also created a token to access this ws, and put the admin user as authorized user.
And then, typed the following URL on Chrome:
https://mysyte.com/webservice/rest/server.php?wstoken=780f8b3a1164163d4dc00a757071194e&wsfunction=core_user_create_users&moodlewsrestformat=json&users[0][username]=testuser&usr[ [email] =john#smith.com&users [0] [password] = XXXXXX
And it worked. It returned a blank page, with the text
[{"id": 1, "username": "testuser"}]
Thus creating a user in moodle.
My question is: How can I do this from java?, or from javascript?, or from jQuery even better.
And if not, from PHP, I guess I would have no problem calling it from java, or javascript, or jQuery.
My Wrong Hint: In another part of the application I used, in javascript, the call $.getJSON() successfully. That's why I thought would also serve me in this case. But no success now, when the mozilla debugger reaches the call, it hangs.
Any feedback will be most welcome.
The call looks like
function create_moodle_user(username,firstname,lastname,email,password) {
var url = "https://mysyte.com/webservice/rest/server.php?"
+ "wstoken=780f8b3a1164163d4dc00a757071194e" + "&"
+ "wsfunction=core_user_create_users" + "&"
+ "moodlewsrestformat=json" + "&"
+ "users[0][username]=" + username + "&"
+ "users[0][firstname]=" + firstname + "&"
+ "users[0][lastname]=" + lastname + "&"
+ "users[0][email]=" + email + "&"
+ "users[0][password]=" + password;
$.getJSON(url, function(data) { // should return [{"id":4,"username":"testuser"}]
// this point is never reached
if (data.length < 64) {
}
else {
}
});
}
Finally, it worked by changing the call and the way how parameters were passed.
function create_moodle_user(u,f,l,e,fn) {
var domainname = 'https://my.moodle.site.com';
var data = {
wstoken: '780f8b3a1164163d4dc00a757071194e'
wsfunction: 'core_user_create_users'
moodlewsrestformat: 'json',
users: [
{
username:u,
password:'xxxxxxxx',
email:e,
firstname:f,
lastname:l
}
]
};
var response = $.ajax({
type: 'GET',
data: data,
url: domainname + '/webservice/rest/server.php'
});
// pass the function parameter
response.done(fn);
}
And this worked!
My problem now is to get user info, since I don't know how to get the response from core_user_get_users_by_field.
This is what I have:
function get_moodle_user(u,fn) {
var domainname = 'https://my.moodle.site.com';
var data = {
wstoken: '780f8b3a1164163d4dc00a757071194e'
wsfunction: 'core_user_get_users_by_field'
moodlewsrestformat: 'json',
field: 'username',
username:u
};
var response = $.ajax({
type: 'GET',
data: data,
url: domainname + '/webservice/rest/server.php'
});
console.log(response); // this does not show the result data
// pass the function parameter
response.done(fn);
}
Any ideas, please?
I am not a java developer, but my company purchase a product to handle their accounting stuff based on java. Now I am facing a problem because they want to prevent repeated invoices on the system and the software allows the user to do it. I called support and they suggested me to create a suppressed field on the client side, copy on that field the message I want to show and read that field when the user tab to the next field. those are a lot of steps and totally inefficient. Below is my code based on what they suggested. It currently showed me the invoice exist message twice.
server side
CSServer.log (Step)
if ((CSEvent.getTarget().getName() == "InvoiceNumber") && (CSEvent.getAction() == "Tabout" ) && (Step == 0))
{
if (!cnn)
{
CSServer.log ("GPCONNECT Lookup::CSForm_OnValidateLookup Connection to the database failed");
}
else
{
Sql = "SELECT COUNT (*) as Result FROM [DYNAMICS].[dbo].[AP_Invoice_Table] WHERE [VendorID] = '" + CSForm.getField("VendorID").getValue() + "' and [DocumentNumber] = '" + CSForm.getField("InvoiceNumber").getValue()+"'";
resultInvSet = cnn.executeSQL(Sql);
var x =null;
x = resultInvSet.getValue("Result");
}
if (x > 0)
{
CSForm.getField("msg").setValue("Invoice number already exist, please check your entry!");
return false;
}
else
{
CSForm.getField("msg").setValue("");
}
}
client side
function InvoiceAmount_OnFocus()
{
var m =CSForm.getField('msg').getValue();
if (m != "")
{
$("#InvoiceNumber").focus();
CSClient.alert(m);
CSForm.getField("InvoiceNumber").setFillColor("FF0000");
}
else
{
CSForm.getField("InvoiceNumber").setFillColor("FFFFFF");
}
return true;
}
Could someone please showed me the right way to handle this?
Update:
Client and server use SOAP and HTTP call to communicate.
Create a webmethod that you call via AJAX and pop the javascript alert based on the result of that function.
example (in your .aspx page):
function doSomething(id) {
$.ajax({
type: "POST",
url: "Do_Something.aspx/DoSomething?id=" + id,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
In Do_Something.aspx.cs:
[WebMethod]
public static string DoSomething(string id)
{
var docs = SqlHelper.SelectAllByInvoiceId(id);
if (docs.Count > 0)
{
return "exists";
}
return "does not exist";
}
Step 1: Create AJAX function to talk to server side function.
Step 2: Return message from server side function and handle that in AJAX function (success or done).
Step 3: Alert message if ajax function catches any result.
For ajax implementation you can refer: http://www.w3schools.com/jquery/ajax_ajax.asp
Or
http://api.jquery.com/jquery.ajax/
I am working on a UIWebView application and obtain some information from a server in the javascript code. I wish to write the JSON text to a file in the documents directory. The beginning part of my javascript file is:
WW.FS = {
init: function(animation, back, fn) {
var me = window.ww ? ww.fs : null;
if (me != null) {
PL.tellNative('pl:closeMore');
}
if (!me || !back) {
Ext.Ajax.request({
url: WW.getBS(),
method: 'POST',
params: {ajax: true, source: 'Touch', ID: 'INT'},
success: function(response) {
try {
data = JSON.parse(response.responseText); <--- want to write this to the documents directory!!
} catch(e) {
PL.indicatorOff();
return false;
}
I need to somehow get the variable "data" back to either the .m file from which I called the javascript file or write it to the documents directory so I can read it later. Would anyone know how to write the variable to the disk? I have been looking for a method to the data to the documents directory with no avail. Any help would be greatly appreciated.
The only way I know to get data from javascript in a UIWebView back to your native code is through the UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType. Basically this call lets you filter URL load calls made within your UIWebView. You override this method and return YES if the load should proceed. The trick is to embed some string in the URL that would make the URL invalid and that you know means you are trying to pass data. Here's an example:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestString = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSArray *requestArray = [requestString componentsSeparatedByString:#":##key_val##"];
if ([requestArray count] > 1)
{
//...do your custom stuff here, all the values sent from javascript are in 'requestArray'
return NO;//abort loading since this is an invalid URL anyway
}
else
{
return YES;
}
}
Within your javascript add something like this:
function sendToApp(_key, _val)
{
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", _key + ":##key_val##" + _val);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
And so to send data from javascript to the native code you do something like this:
sendToApp('state', event.data);
I'd like to call a Python function from JavaScript code, because there isn't an alternative in JavaScript for doing what I want. Is this possible? Could you adjust the below snippet to work?
JavaScript code:
var tag = document.getElementsByTagName("p")[0];
text = tag.innerHTML;
// Here I would like to call the Python interpreter with Python function
arrOfStrings = openSomehowPythonInterpreter("~/pythoncode.py", "processParagraph(text)");
~/pythoncode.py contains functions using advanced libraries that don't have an easy to write equivalent in JavaScript:
import nltk # is not in JavaScript
def processParagraph(text):
...
nltk calls
...
return lst # returns a list of strings (will be converted to JavaScript array)
All you need is to make an ajax request to your pythoncode.
You can do this with jquery http://api.jquery.com/jQuery.ajax/, or use just javascript
$.ajax({
type: "POST",
url: "~/pythoncode.py",
data: { param: text}
}).done(function( o ) {
// do something
});
From the document.getElementsByTagName I guess you are running the javascript in a browser.
The traditional way to expose functionality to javascript running in the browser is calling a remote URL using AJAX. The X in AJAX is for XML, but nowadays everybody uses JSON instead of XML.
For example, using jQuery you can do something like:
$.getJSON('http://example.com/your/webservice?param1=x¶m2=y',
function(data, textStatus, jqXHR) {
alert(data);
}
)
You will need to implement a python webservice on the server side. For simple webservices I like to use Flask.
A typical implementation looks like:
#app.route("/your/webservice")
def my_webservice():
return jsonify(result=some_function(**request.args))
You can run IronPython (kind of Python.Net) in the browser with silverlight, but I don't know if NLTK is available for IronPython.
Communicating through processes
Example:
Python: This python code block should return random temperatures.
# sensor.py
import random, time
while True:
time.sleep(random.random() * 5) # wait 0 to 5 seconds
temperature = (random.random() * 20) - 5 # -5 to 15
print(temperature, flush=True, end='')
Javascript (Nodejs): Here we will need to spawn a new child process to run our python code and then get the printed output.
// temperature-listener.js
const { spawn } = require('child_process');
const temperatures = []; // Store readings
const sensor = spawn('python', ['sensor.py']);
sensor.stdout.on('data', function(data) {
// convert Buffer object to Float
temperatures.push(parseFloat(data));
console.log(temperatures);
});
Typically you would accomplish this using an ajax request that looks like
var xhr = new XMLHttpRequest();
xhr.open("GET", "pythoncode.py?text=" + text, true);
xhr.responseType = "JSON";
xhr.onload = function(e) {
var arrOfStrings = JSON.parse(xhr.response);
}
xhr.send();
You cannot run .py files from JavaScript without the Python program like you cannot open .txt files without a text editor. But the whole thing becomes a breath with a help of a Web API Server (IIS in the example below).
Install python and create a sample file test.py
import sys
# print sys.argv[0] prints test.py
# print sys.argv[1] prints your_var_1
def hello():
print "Hi" + " " + sys.argv[1]
if __name__ == "__main__":
hello()
Create a method in your Web API Server
[HttpGet]
public string SayHi(string id)
{
string fileName = HostingEnvironment.MapPath("~/Pyphon") + "\\" + "test.py";
Process p = new Process();
p.StartInfo = new ProcessStartInfo(#"C:\Python27\python.exe", fileName + " " + id)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
return p.StandardOutput.ReadToEnd();
}
And now for your JavaScript:
function processSayingHi() {
var your_param = 'abc';
$.ajax({
url: '/api/your_controller_name/SayHi/' + your_param,
type: 'GET',
success: function (response) {
console.log(response);
},
error: function (error) {
console.log(error);
}
});
}
Remember that your .py file won't run on your user's computer, but instead on the server.
Despite what some replies and comments suggest, there are a number of ways for using Python on the front-end. For your question in particular, see this reply.
I have a data fetching method that uses jQuery.ajax() to fetch xml files.
/* */data: function() {
/* debug */try {
var url = arguments[0] ;
var type = arguments[1] ;
var scope = arguments[2] ;
var callback = arguments[3] ;
var self = this ;
if(this.cache[url]) {
callback(this.cache[url]) ;
} else if(!this.cache[url]) {
$.ajax({
type: "GET" ,
url: url ,
dataType: type ,
cache: false ,
success: function(data) {
if(type == "text/xml") {
var myJson = AUX.json ;
var jsonString = myJson.build(data,scope,null) ;
var jsonObject = $.parseJSON(jsonString) ;
self.cache[url] = jsonObject ;
callback(url) ;
} else if(type == "json") {
self.cache[url] = data ;
callback(url) ;
}
} ,
error: function() {
throw "Ajax call failed." ;
}
}) ;
}
/* debug */} catch(e) {
/* debug */ alert("- caller: signTutor.data\n- " + e) ;
/* debug */}
} ,
My problem is: jQuery somehow adds a parameter (?_=1272708280072) to the url if there are escaped (hexadecimal notation) or unescaped utf-8 characters outside of the ASCII range -- i believe -- in the file name. It all works well if the file name does not contain characters in that range.
Type is set to xml so there should not be a confusion of types. Headers of the xml files are also set adequately.
I can see from the console that jQuery throws an error, but I'm not sure as to where the problem really is.
Probably a problem with file name formatting, but I did not find any resources on the web as to AJAX file name specifications. Any ideas?
Thanks for you help!
That is a 'cache-buster' and is ignored.
The added parameter changes the url just enough to bypass most all caches that are between you and the source.
If the Url was not modified, it is likely that data would be served from any one of the caches between you and the resource, including your browser, any proxies, and perhaps the server itself.
You can find a lot of explanations on the net. Here is one.
it should be ignored.
Just to make a test, if you are using rails, don't use the javascript_include_tag but pass the JavaScript as
<script src="/path/for/the/script/script.js" type="text/javascript"></script>
It won't enable the cache-buster and with that you can see if your problem is where you think that it is.