I successfully managed to get trans and blocktrans to translate text.
However…
I have a function defined in utils.py that returns a dictionary containing strings of which some of them I need converted to the current language.
EDIT: *This is a utils.py that I’ve created in my project directory that are called by views to perform certain auxillary functions on a dict and then return the updated dict
I have done something like this:
try:
path = default_storage.save(customercode + '/' + file.name, ContentFile(file.read()))
results['status'] = 'success'
results['message'] = _(u'Your file has been successfully uploaded')
except Exception as e:
results['status'] = 'error'
results['message'] = _(u'There was an error uploading your file: ') + str(e)
return results
I have also added from django.utils.translation import ugettext_lazy as _ to the top of this utils.py file..
And this “results” dictionary is used in one of my views wherein the whole dictionary after some further processing is passed as a context variable to the template.
I have correctly set the translation in the .po file. All other template tags translate perfectly. Only the above mentioned strings do not translate.
Any help would be greatly appreciated.
UPDATE: I tried the same process in a forms filed label and it translated just fine. It’s only the aforementioned areas where it won’t translate!
PS: This is my first question on stackoverflow. I apologize in advance if I’ve made mistakes asking this question.
Just as I expected,
I was making a very silly mistake. I accessed the incorrect variable in the template while trying to print the translated value! >_<
But I guess one thing I’ve learnt is staying away from a problem and then coming back to it after a long time helps. You’ve got to learn the problem again and that sometimes helps you find silly bugs like these!