markfinger/python-react

django's gettext problem

sillygod opened this issue · 2 comments

how to resolve the problem gettext is not defined.

an example base template file

<html lang='zh-TW'>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="content-type" content="text/html;charset=UTF-8">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1.0, width=device-width, height=device-height " />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <script type="text/javascript" src="{% url 'django.views.i18n.javascript_catalog' %}"></script>
    {% block extra_content %} {% endblock %} {% block js %} {% endblock %}
</head>
<body>
    <div id="now_lang" style="display: none">{{ LANG }}</div>
    {% csrf_token %}
    {% block content %}{% endblock %}
</body>
</html>

by add this <script type="text/javascript" src="{% url 'django.views.i18n.javascript_catalog' %}"></script>, I can call gettext in my jsx file. However, the render server will not know what gettext is.

Is there any solution for this situation ?

You'll need a way to pass that data to the renderer.

I'm not too familiar with internationalisation in django, but I assume you can interrogate the request for a lang and then dump all the data used for gettext, so you can pass it to renderer. If you're not too familiar with how to implement the interrogation, I'd recommend looking at the implementation details for django.views.i18n.javascript_catalog.

Note that once you're handling data like this, you'll probably need different entry points for your code so that you can handle the different contexts. It's a bit of a pain sometimes, but it does have the benefit that your system will end up less coupled to django's front-end.

I've linked this question from the README. https://github.com/markfinger/python-react/blob/master/README.md#how-do-i-handle-djangos-translation-and-gettext-with-react-components

Everyone should feel free to continue the discussion if they're so inclined