Dancer2::Plugin::Locale - Localize your Dancer2 application
This document describes Dancer2::Plugin::Locale version 0.07
In your app:
locale->maketext('You are [numf,_1] of [numf,_2].', 42, 99);
and from template
<div id="req_msg">[% locale.maketext('You have [quant,_1,request,requests].', req_count) %]</div>
Adds a locale keyword for your code and templates.
A lazy façade to get a locale handle suitable to the request. The locale object is a CLDR aware maketext format object.
It will be based on the session’s locale value if possible, then a configured default if possible, then 'en'.
The object is Locale::Maketext::Utils based.
Locale::Maketext::Utils extends Locale::Maketext a number of ways including:
It shifts toward CLDR based functionality which means you no longer have to create locale specific variants of code in each locale’s class.
Because of that and other utils it has, creating and dealing with the locale subclasses classes is much easier.
The object is a multiton (AKA an argument based singleton): "Argument based singleton" in Locale::Maketext::Utils
Adds a number of helpful methods: "METHODS" in Locale::Maketext::Utils
More sane fallback and lookup failure hooks: "Automatically _AUTO'd Failure Handling with hooks" in Locale::Maketext::Utils
Adds a very handy set of bracket notation methods (CLDR when possible) "Bracket Notation"
The locales available are determined by the lexicon files found in the appdir’s locale/ directory.
The name of each file must be a normalized version (See Locales::normalize_tag() in Locales) of an acceptable ISO tag. and end in with the extension .json.
Tip: To make them available to your UI you could simply symlink …/locale/ to …/public/locale.
A lexicon is a simple key/value hash where the key is the phrase and the value is the translation.
For example,in pseudo code:
# source phrase => target phrase
'Hello World' => 'Bonjour Monde'
Each locale will have a lexicon hash in a file as desrcibed in "Available Locales".
The hash must be written in JSON format and be utf8 encoded.
Don’t be afraid of non-ASCII characters, just put them in the file as the character and it will work fine (if it doesn’t then it can help you track down bugs faster, win win!).
"I ♥︎ Dancer2!" : "私は♥︎ダンサー2"
Bracket notation is described a bit more at "BRACKET NOTATION" in Locale::Maketext but is essentially a format to allow you to notate–within left and right square brackets, hence the name–dynamic portions of a phrase.
For example, include a non-translatable ever-changing name:
locale->maketext('Your email address, [_1], has been unsubscribed.', $email)
or a number formatted according to the object’s locale’s CLDR data:
locale->maketext('You are user [numf,_1] of [numf,_2].', $place, $count)
The bracket notation improvements over the core Locale::Maketext can be categoraized as follows:
improved core Locale::Maketext bracket notation methods
additional bracket notation methods
output() bracket notation methods
… TODO, sorry I needed to get this on CPAN for a consumer but will finish it ASAP …
TODO items in the POD obviously, will list more if there are any left after the next version.
If the locale/ directory exists but can not be opened:
<Could not read locale directory (%s): $!>
<Could not read locale directory (%s): $!
If you have a mis-named file in your locale/ directory:
<Skipping un-normalized locale named lexicon (%s.json) …>
<Skipping un-normalized locale named lexicon (%s.json) …
If you have a properly named lexicon that is not a file in your locale/ directory:
<Skipping non-file lexicon (%s.json) …>
<Skipping non-file lexicon (%s.json) …
If you have a lexicon that can’t be loaded (e.g. broken JSON):
<Ignoring lexicon, %s, since it containes invalid JSON:\n\t$@>
<Ignoring lexicon, %s, since it containes invalid JSON:\n\t$@
The default locale is “en”, you can change it via Dancer2 configuration like so:
Please report any bugs or feature requests (and a pull request for bonus points) through the issue tracker at https://github.com/drmuey/p5-Dancer2-Plugin-Locale/issues.
Daniel Muey <http://drmuey.com/cpan_contact.pl>
Copyright (c) 2015, Daniel Muey <http://drmuey.com/cpan_contact.pl>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install Dancer2::Plugin::Locale, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell
For more information on module installation, please visit the detailed CPAN module installation guide.