Adding Internationalization Support to my xblock - troubleshooting

Hello,

I followed the 7.2.2 documentation about adding translations to my xblock:

  1. Created a simple 1 string translations/‘lang’/LC_MESSAGES/text.po and generated the text.mo file with ‘msgfmt text.po -o text.mo’

  2. Changed the xblock setup.py last line to recognize the local translations directory with
    package_data=package_data(“xblock-name”, [“static”, “public”, “translations”])

  3. Started the lms and studio containers with ‘make lms-up studio-up’

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

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

Regards,
Guy

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:

  1. Add the i18n xblock service with the class preceding decorator
    @XBlock.needs(‘i18n’)

  2. 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 )

This is also explained at
https://openedx.atlassian.net/wiki/spaces/AC/pages/161400730/Open+edX+Runtime+XBlock+API

  1. I did notice in your example a more cautious programming approach that rather defined and used

    @property
    def i18n_service(self):
    “”" Obtains translation service “”"
    i18n_service = self.runtime.service(self, “i18n”)
    if i18n_service:
    return i18n_service
    else:
    return DummyTranslationService()

We may consider its adoption as well

Thanks again
Regards
Guy