Created a simple 1 string translations/‘lang’/LC_MESSAGES/text.po and generated the text.mo file with ‘msgfmt text.po -o text.mo’
Changed the xblock setup.py last line to recognize the local translations directory with
package_data=package_data(“xblock-name”, [“static”, “public”, “translations”])
Started the lms and studio containers with ‘make lms-up studio-up’
Changed the language with ‘translation.activate(‘lang’)’ in the student_view() method and saw many translated strings other then the one I tested. The string tested showed its msgid rather than its msgstr
But - when manually inserting this string into the end of
edx-platform/conf/locale/‘lang’/LC_MESSAGES/django.po and creating the django.mo file, the
string was translated properly into ‘lang’.
Any hint/suggestion how to “convince” edx-platform to use the xblock translations//text.mo file will be greatly appreciated
If this string is part of an HTML template, Probably you are rendering the template using the render_template method in student_view instead of render_django_template(part of xblock-utils and renders the template using the XBlock i18n service). Changing your code to use render_django_template should fix the issue. You can check this code for example usage xblock-drag-and-drop-v2/drag_and_drop_v2.py at master · edx-solutions/xblock-drag-and-drop-v2 · GitHub
Saqib hi and thanks for your answer + sorry for late response.
We followed your example and what finally worked out in our setup is
that for using translations/‘lang’/LC_MESSAGES/text.mo strings one should:
Add the i18n xblock service with the class preceding decorator @XBlock.needs(‘i18n’)
Use for each translated string either
self.runtime.service(self, “i18n”).ugettext(text with vars).format(…)
or its shortcut (less readable) call
self.ugettext(text with vars).format(…)
for example:
return self.ugettext(“Your recorded (best) score is {old_best} points from {max_score} points.{attempts_message}”).format(
old_best = str(self.best_student_score), max_score = str(self.get_max_score()),
attempts_message = my_attempts_message )