Hi,
I'm sometimes use Flask for my Toolforge tools. I'd like to localize them, ideally with messages directory in similar format like MediaWiki has in i18n directory.
I know about Intuition for PHP, does anybody know about similar thing for Flask application?
Best, Martin
Hi Martin,
I know of a few approaches, depending on the complexity of your tool:
- You can get pretty far with having your strings in, say, JSON files that you load and format yourself in code based on placeholders. - The Python standard library has a built-in gettext https://docs.python.org/2.7/library/gettext.html module if you prefer a more standard format. - For a more comprehensive solution that adds date formatting, I heard good things about Flask-Babel https://pythonhosted.org/Flask-Babel/, but never used it myself.
Whichever way you choose, I'd definitely recommend managing the translations through TranslateWiki https://translatewiki.net/, which you might already know about.
Em ter, 18 de set de 2018 às 15:14, Martin Urbanec < martin.urbanec@wikimedia.cz> escreveu:
Hi,
I'm sometimes use Flask for my Toolforge tools. I'd like to localize them, ideally with messages directory in similar format like MediaWiki has in i18n directory.
I know about Intuition for PHP, does anybody know about similar thing for Flask application?
Best, Martin _______________________________________________ Wikimedia Cloud Services mailing list Cloud@lists.wikimedia.org (formerly labs-l@lists.wikimedia.org) https://lists.wikimedia.org/mailman/listinfo/cloud
Hi Guilherme,
well, maybe I should have explained what I'm trying to do in the original email. Fixing this mistake now.
I have a tool called Wikinity https://tools.wmflabs.org/wikinity. The tool takes data from Wikidata and displays items with no image uploaded. It became to be used by Czech Commons users in the preparation step for their photo trip. The source https://phabricator.wikimedia.org/source/tool-wikinity/ is awful, to say the most important parts from how its written:
- Frontend is in public/*.php. Those files used to be HTML files while the tool was in one language only. I switched them to PHP only to be able to use Intuition. - Backend logic is in public/*.py. for the main purpose of tool, public/map.py is important, which selects appropriate Wikidata query (stored in queries/*.txt) based on GET parameters and creates a map. - JavaScript just displays the map generated by map.py. - Other files are used for other things like storing queries.
Yeah, I know its awful (but working :D). I plan to rewrite it. As I already use Translatewiki, which generates locales https://phabricator.wikimedia.org/source/tool-wikinity/browse/master/messages/ in certain format, I want to keep it. The fornat is straightforward to me and not like gettext which I managed to understood, but I think this is more clear. Of course, you might not agree, that's my current point of view.
My plan is to convert this into a simple Flask app, which should incorporate several backend CGI scripts written in Python into one file with much more clear structure. This Flask app should ofc do the same things like the current tool does. I know how to do everything from the function side. The only one thing I'm thinking about is the localization - more certainly, what way I should use to use locales in this format https://phabricator.wikimedia.org/source/tool-wikinity/browse/master/messages/cs.json in a Flask app (more precisely, in its Jinja2 templates, I do not have anything to localize in the Python part).
BTW, I tried to use Flask-Babel and I manged to have working localization https://tools.wmflabs.org/commons-mass-description. The only one thing I don't like is that it uses gettext, which I don't like and its not like "modify this json file and the locales will change automatically", but "modify this .po file, then compile it it and maybe it will work".
Martin
út 18. 9. 2018 v 19:10 odesílatel Guilherme Gonçalves < guilherme.p.gonc@gmail.com> napsal:
Hi Martin,
I know of a few approaches, depending on the complexity of your tool:
- You can get pretty far with having your strings in, say, JSON files
that you load and format yourself in code based on placeholders.
- The Python standard library has a built-in gettext
https://docs.python.org/2.7/library/gettext.html module if you prefer a more standard format.
- For a more comprehensive solution that adds date formatting, I heard
good things about Flask-Babel https://pythonhosted.org/Flask-Babel/, but never used it myself.
Whichever way you choose, I'd definitely recommend managing the translations through TranslateWiki https://translatewiki.net/, which you might already know about.
Em ter, 18 de set de 2018 às 15:14, Martin Urbanec < martin.urbanec@wikimedia.cz> escreveu:
Hi,
I'm sometimes use Flask for my Toolforge tools. I'd like to localize them, ideally with messages directory in similar format like MediaWiki has in i18n directory.
I know about Intuition for PHP, does anybody know about similar thing for Flask application?
Best, Martin _______________________________________________ Wikimedia Cloud Services mailing list Cloud@lists.wikimedia.org (formerly labs-l@lists.wikimedia.org) https://lists.wikimedia.org/mailman/listinfo/cloud
-- Guilherme P. Gonçalves _______________________________________________ Wikimedia Cloud Services mailing list Cloud@lists.wikimedia.org (formerly labs-l@lists.wikimedia.org) https://lists.wikimedia.org/mailman/listinfo/cloud
Hey Martin, i guess it would be pretty easy to create a solution using Jinja2 templates and the JSON format you already have. I don't know if you want the user to be able to switch languages, or if it's a configuration option. In both cases you just need a little bit of logic that loads the specific JSON file to memory and then serves it to the template using a context processor with a dict (see here: http://flask.pocoo.org/docs/1.0/templating/). After that you can simply use that dict inside of your templates to display the messages in the configured language.
Kind regards, -- Hay On Tue, Sep 18, 2018 at 10:48 PM Martin Urbanec martin.urbanec@wikimedia.cz wrote:
Hi Guilherme,
well, maybe I should have explained what I'm trying to do in the original email. Fixing this mistake now.
I have a tool called Wikinity. The tool takes data from Wikidata and displays items with no image uploaded. It became to be used by Czech Commons users in the preparation step for their photo trip. The source is awful, to say the most important parts from how its written:
Frontend is in public/*.php. Those files used to be HTML files while the tool was in one language only. I switched them to PHP only to be able to use Intuition. Backend logic is in public/*.py. for the main purpose of tool, public/map.py is important, which selects appropriate Wikidata query (stored in queries/*.txt) based on GET parameters and creates a map. JavaScript just displays the map generated by map.py. Other files are used for other things like storing queries.
Yeah, I know its awful (but working :D). I plan to rewrite it. As I already use Translatewiki, which generates locales in certain format, I want to keep it. The fornat is straightforward to me and not like gettext which I managed to understood, but I think this is more clear. Of course, you might not agree, that's my current point of view.
My plan is to convert this into a simple Flask app, which should incorporate several backend CGI scripts written in Python into one file with much more clear structure. This Flask app should ofc do the same things like the current tool does. I know how to do everything from the function side. The only one thing I'm thinking about is the localization - more certainly, what way I should use to use locales in this format in a Flask app (more precisely, in its Jinja2 templates, I do not have anything to localize in the Python part).
BTW, I tried to use Flask-Babel and I manged to have working localization. The only one thing I don't like is that it uses gettext, which I don't like and its not like "modify this json file and the locales will change automatically", but "modify this .po file, then compile it it and maybe it will work".
Martin
út 18. 9. 2018 v 19:10 odesílatel Guilherme Gonçalves guilherme.p.gonc@gmail.com napsal:
Hi Martin,
I know of a few approaches, depending on the complexity of your tool:
You can get pretty far with having your strings in, say, JSON files that you load and format yourself in code based on placeholders. The Python standard library has a built-in gettext module if you prefer a more standard format. For a more comprehensive solution that adds date formatting, I heard good things about Flask-Babel, but never used it myself.
Whichever way you choose, I'd definitely recommend managing the translations through TranslateWiki, which you might already know about.
Em ter, 18 de set de 2018 às 15:14, Martin Urbanec martin.urbanec@wikimedia.cz escreveu:
Hi,
I'm sometimes use Flask for my Toolforge tools. I'd like to localize them, ideally with messages directory in similar format like MediaWiki has in i18n directory.
I know about Intuition for PHP, does anybody know about similar thing for Flask application?
Best, Martin _______________________________________________ Wikimedia Cloud Services mailing list Cloud@lists.wikimedia.org (formerly labs-l@lists.wikimedia.org) https://lists.wikimedia.org/mailman/listinfo/cloud
-- Guilherme P. Gonçalves _______________________________________________ Wikimedia Cloud Services mailing list Cloud@lists.wikimedia.org (formerly labs-l@lists.wikimedia.org) https://lists.wikimedia.org/mailman/listinfo/cloud
Wikimedia Cloud Services mailing list Cloud@lists.wikimedia.org (formerly labs-l@lists.wikimedia.org) https://lists.wikimedia.org/mailman/listinfo/cloud
Hi,
On 9/18/18 7:13 AM, Martin Urbanec wrote:
I'm sometimes use Flask for my Toolforge tools. I'd like to localize them, ideally with messages directory in similar format like MediaWiki has in i18n directory.
There's the pywikibot.i18n component[1][2], though I've never actually tried using it with a Flask tool.
[1] https://gerrit.wikimedia.org/g/pywikibot/core/+/master/pywikibot/i18n.py [2] https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.html#module-pyw...
-- Legoktm
Hey everyone,
thank you very much for your helpful advices, both at IRC and in this list. I created a little library that's satisfying my current needs. Its sourcecode is on https://github.com/urbanecm/flask-jsonlocale and it can be installed via pip install flask-JSONLocale.
Best,
Martin
st 19. 9. 2018 v 0:08 odesílatel Kunal Mehta legoktm@member.fsf.org napsal:
Hi,
On 9/18/18 7:13 AM, Martin Urbanec wrote:
I'm sometimes use Flask for my Toolforge tools. I'd like to localize them, ideally with messages directory in similar format like MediaWiki has in i18n directory.
There's the pywikibot.i18n component[1][2], though I've never actually tried using it with a Flask tool.
[1] https://gerrit.wikimedia.org/g/pywikibot/core/+/master/pywikibot/i18n.py [2]
https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.html#module-pyw...
-- Legoktm
Wikimedia Cloud Services mailing list Cloud@lists.wikimedia.org (formerly labs-l@lists.wikimedia.org) https://lists.wikimedia.org/mailman/listinfo/cloud