Reputation: 1073
I'm new in i18n/l10n, and at the moment I have to add i18n/l10n to an existing app (in Python Flask, for instance).
I have been including i18n translation block in my HTML view, which is fine. But the controllers code with flash messages, error validation messages struck me in the heart. For example, in Flask:
@app.route('/logout')
def logout():
# Logout user code block...
flash("You have been logged out.")
return redirect(url_for("index"))
@app.route('/validate')
def validate():
# Validate code block...
response = jsonify(errors={
"field_name": "Invalid input!"
})
response.status_code = 406
return response
There are two approaches to add i18n to controllers returned messages:
Use direct i18n gettext()
or any equivalent i18n utility in the controller and the controller would then return the whole block. For example:
@app.route('/logout')
def logout():
# Logout user code block...
flash(gettext("You have been logged out."))
return redirect(url_for("index"))
@app.route('/validate')
def validate():
# Validate code block...
response = jsonify(errors={
"field_name": gettext("Invalid input!")
})
response.status_code = 406
return response
The controller returns the original text (this case is in English). The HTML view will wrap the returned text in an i18n utility i.e. gettext
. For instance:
<p>{{ gettext(message) }}</p>
<p>{{ gettext(error_message) }}</p>
I think each approach has its own pros/cons:
Pros: Straightforward. You can use i18n engine to automatically index the whole source code and generate the l10n files for you. This can also work with AJAX validation, since the returned message will be in the correct language and ready to display.
Cons: Too many responsibilities for the controller. The controller should only handle the app logic and return an international language (which is mainly English). Suppose the controller communicates with an API, the API will then have to submit a locale code en
in order to get the correct message.
Pros: Separate controller's logic with i18n. i18n should only be handled in view and that's it.
Cons: Every time you add a new string you have to manually update the l10 files. Very hard to handle the AJAX validation case.
What should be the best practice for i18n/l10n the app and controllers? Please help me as I'm banging my head with this for countless time.
Upvotes: 0
Views: 308
Reputation: 1727
Maybe I'm making a big mistake, but for me it is obvious that you should use it in the controller, since in this case in the view should be just impossible because flash function can return many different things. You can use gettext in some way in your HTML / Javascript for those strings that are "static", but How do gettext know what Jinja2 is going to render in runtime if you have to run gettext before to translating the text strings? The "get flashed messages" function receives what "flash" sends (in many places on your controllers), then How can you translate that string for that line of code if it's at runtime when get flashed messages is executing receiving any string from anywhere in the controller?
Upvotes: 0