New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gettext for Xslate #50

Open
wants to merge 20 commits into
base: master
from

Conversation

Projects
None yet
3 participants
@mbethke
Copy link

mbethke commented May 11, 2016

So I was bored again :)

While I usually prefer my software in English, I blog in German and would like the theme boilerplate (New Articles, Search, Posted on etc.) to be in German, too. Nevertheless I wanted to keep publishing my Chronicle theme on Github as an example for using Xslate-based themes. Im far too lazy to maintain two themes in sync for all but the texts though, so that was not an option.
Locale::TextDomain to the rescue!
Using custom functions in Xslate, you can now write stuff like

<h1>[% __("News") %]</h1>
[% body %]
<p>[% __x('Published on {date}', date => date) %],
[% __nx('1 comment', '{cmts} comments', ncmts, cmts => ncmts) %]</p>

and by simply adding a gettext-style catalog, get these strings translated into any language specified by the user's $LANGUAGE variable.

Just as a demo, the above theme is now available in English, German, Spanish and Portuguese :)

Of course the $MONTHS setting is even more awkward now. I'm not sure yet what to do about it, perhaps Date::Language can be replaced with DateTime::Locale or so. TBD.

There is a new test for this stuff included that also shows the essence of how it's meant to be used.

mbe-financial-com added some commits May 9, 2016

add date_epoch variable and strftime template func
While adding the Spanish translation, I noticed there has to be a way to extract
only the hour from a post date as Spanish makes a singular/plural distinction
there: "publicado a la 1:00" vs. "publicado a las 2:00". strftime() allows to
make ad-hoc dates with custom formats directly from the template.
add test for localized templates
* Add dependency on core module FindBin
* Test needs a fairly deep hierarchy under t/themes to find its message catalog
improve docs
Translating a variable didn't really make sense. Replaced with a fixed string.
@skx

This comment has been minimized.

Copy link
Owner

skx commented May 25, 2016

Sorry for the slow reply on this - It took me a while to decide whether this was a feature that was useful, and desirable. I think the core idea of offering i18n in some fashion is good.

But I have to admit that I use Debian almost exclusively, and the Text::xslate module is not packaged for this - meaning if I accept this pull request I cannot use the project any more. I see that Locale::gettext is available, via liblocale-gettext-perl though.

At the moment your code seems to abort on failure:

      die "Failed to load Text::Xslate module - $!"; 

In an ideal world i18n would be supported by all themes, all strings would be translatable, but for me to accept the solution it has to work on a Debian system, with packages that are packed for the stable release - at least.

Now that you've shown how it might work I think I'll go look for examples of other projects that are translatable/translated and see if there is a decent approach we can all be happy with.

PS. As you say dates/times are a mess. If we can come up with a solution for the templates I'm not averse to breaking compatibility with a new major release, if that cleans up inconsistencies..

@mbethke

This comment has been minimized.

Copy link
Author

mbethke commented May 25, 2016

Hi,
no problem, there's no rush :)
The bit where I abort upon failing to load the Xslate module is only used when the user selects --template-engine=Xslate (or XslateTT). I suppose the problem with the current patch is the test that would always fail -- that sucks of course, I just fixed that. I also use Debian on my web server, but with a local lib dir private to the blog user that I use to push to the blog's git repo. But that's just because I really really hate HTML::Template (I mean, it's even or #perl's "deprecated modules" list for being old and ugly and buggy and generally irredeemable ...) and I wouldn't want to ask that of users who just want to install a Debian package either.

Well, that's a pity with Xslate on Debian. I read it's in sid already but won't be in stable until the next release. What could work is to write a Template Toolkit module -- it shouldn't be much different from Xslate and all the XslateTT theme stuff should work on TT as well, plus TT has been in Debian forever.
I'll try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment