Reputation: 3
def mymethod(request):
return_obj = {}
if request.method == 'POST':
info = request.POST
try:
ts_vals, coordinates, name = somefunction(params)
return_obj["values"] = ts_vals
return_obj["msg"] = candy.translated(request, "all_msg") # all_msg is from the excel
return_obj["name"] = name
return_obj["success"] = "success"
except Exception as e:
return_obj["error"] = candy.translated(request, "all_Error")
return JsonResponse(return_obj)
I tried the above process. The returned JSON is parsed in my javascript code and sent back to the interface from javascript code. How do we handle this situation with Candy Translation?
Upvotes: 0
Views: 99
Reputation: 26
There are 2 ways of doing what you need with CandyTranslate. First Option is using *candy.path for your callable function and then candyLink to call correct language version of the function Second Option is using the {{lang}} variable in template and candy.translated(lang, ...)
------ OPTION 1:
views.py:
def test(request):
import json
from django.http import JsonResponse
return_obj = {}
return_obj["msg"] = candy.translated(request, "all_msg")
return JsonResponse(return_obj)
urls.py:
*candy.path('test', views.test, name='test'),
your template:
<button onclick="getData()">TEST</button>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function getData() {
$.ajax({
url: '{% url candyLink.test %}',
success: function(data) {
console.log(data);
}
});
}
</script>
------ OPTION 2:
views.py:
def test(request,lang):
import json
from django.http import JsonResponse
return_obj = {}
return_obj["msg"] = candy.translated(lang , "all_msg")
return JsonResponse(return_obj)
urls.py:
path('test/<lang>', views.test, name='test'),
your template:
<button onclick="getData()">TEST</button>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function getData() {
$.ajax({
url: '{% url 'test' lang%}',
success: function(data) {
console.log(data);
}
});
}
</script>
Upvotes: 0