Plugin for Dancer to access libgettext via Locale::TextDomain
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



A plugin to allow Dancer apps to use multilingual support via gettext.


To install this module, run the following commands:

	perl Makefile.PL
	make test
	make install


Currently you need to set the language yourself. It doesn't (yet?) look at the
browser's Accept-Languages: header and find that language / fall back through
other languages.

That information can be found in a number of different ways or you may not want
it to be set for you (for example if it is sent by the application's admin
rather than by the client) and doing so would increase the dependencies. I
don't like modules importing the kitchen sink because they think that their way
of doing some extra feature is good.

For one solution see L<Locale::Util> for details on extracting this information
from UA headers and then possibly L<I18N::LangTags> for parsing it in to
language tags that are superordinate to the tag provided by the client. E.g.
any "en" language tag is considered to be English.

Tricky things in the code are;

1. Locale::TextDomain (L::TD) uses the package of the caller to determine the
textdomain. This is tricky because when you set this up using set_i18n_domain()
the caller is this plugin, Dancer::Plugin::I18N::TextDomain. When you call it
from your script then the caller is the package from which you call it. For
example My::App.

To fix this calls to the gettext functions are routed through a simply sub
called tdcore() which changes into the Dancer::Plugin::I18N::TextDomain package
space before calling the appropriate gettext function.

2. When the __*x() gettext methods are called from inside of TT, e.g.

[% __x('hello {name}', name=>'world'); %]

TT tries to be helpful and pass the parameters as a hashref. This isn't actually
helpful since L::TD expects to receive a hash (or array of key-value pairs)
instead of a hashref. The effect is that you get warnings in Dancer about odd
numbers of elements in the hash:

Warning caught during route execution: Odd number of elements in hash assignment at .../Locale/ line 146.

To stop this tdcore() dereferences a hashref passed as the last element of @_
if the gettext method is a __*x() method. You can avoid it in TT by using

[% __x('hello {name}', 'name', 'world'); %]

But that is more typing and harder to read.

These shouldn't affect usage since the caller should always be the plugin, but
it is worth noting.

Suggestions on better solutions to either of these are welcome.

After installing, you can find documentation for this module with the
perldoc command.

    perldoc Dancer::Plugin::I18N::TextDomain

You can also look for information at:



Copyright (C) 2011 Colin Keith

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See for more information.