shlinkio / shlink

The definitive self-hosted URL shortener
https://shlink.io
MIT License
3.22k stars 259 forks source link

Replace twig by plates #100

Closed acelaya closed 7 years ago

hkulekci commented 7 years ago

hello @acelaya I am following your project and I want to ask that there is any specific reason to do this replacement. I am using twig in most of my projects and your opinion is really important for me. Thank you.

acelaya commented 7 years ago

Hi!

I appreciate the interest :-)

I've been using twig in lots of projects in the past years, and I "was" very happy with it. However, it has one problem. If you want to automatically extract translations from twig templates to use with gettext files, the process is too complicated or too difficult to automate.

Documentation says how to "compile" all templates in oprder to be able to process them with xgettext or poedit. http://twig-extensions.readthedocs.io/en/latest/i18n.html#extracting-template-strings

It also mentions that you can use this utility: https://github.com/umpirsky/Twig-Gettext-Extractor

In the first project I had to deal with this, we ended up using that utility, and I've used it in other projects since then.

The problem is that you end up with different scripts for every project, and poedit doesn't allow to configure the extractors in a per project (or per translation file) basis, so sometimes there are conflicts and you have to manually edit poedit's configuraiton every time.

Also, when poedit fails to run the script, it throws a cryptic error that is very hard to debug.

In my case, almost always, when I return to a project which I haven't touched for a couple months, the first time I try to update translations from poedit, it either throws an error on something that was working or it is not able to detect new translations. Or even worse, it considers all existing translations do not exist anymore and removes them.

This last one is what happened to me in my website last week, which used to be built with twig, but I lost a lot of time trying to get poedit and the twig-gettext-extractor work together. I finally decided that it was not worth it, and decided to stop using twig on any projects that requires translations If I'm going to use gettext.

Plates is the natural substitute for twig, because it is based on it, but it doesn't use a special templating language. Instead, you write templates in plain PHP, which poedit understands out of the box.

I have already migrated my website this weekend, and I really liked it. Take a look at one example of a twig template migrated to plates: https://github.com/acelaya/alejandrocelaya.com/pull/44/files#diff-9305d734b53d9d5dfd4cade7f1bce02f

The only caveat with plates is that you have to remember to escape output yourself.

That said, that doesn't mean I don't like twig. It is just the combination twig+translations+poedit which is problematic.

If you are using any other type of translation system or not using translations at all, you won't probably face this problem.

And that's why I decided to replace twig by plates in shlink :-)

hkulekci commented 7 years ago

I used gettext with twig and I faced similar problem and I did not really force to solve like you. I send translated texts as parameter to view. I appreciate you. :)

Anyway, I asked this question because, in my opinion, the main point of using twig or plate separating view layer from my other logics. And both of them can do this easily. I thought that using PHP in your view layer allows the use of unwanted logics to developers. But, lots of time this feature really help not to wasting your time like your example.

Thank you very much for detailed explanation.