The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Dancer2::Plugin::Locale - Localize your Dancer2 application

VERSION

This document describes Dancer2::Plugin::Locale version 0.07

SYNOPSIS

In your app:

    use Dancer2;
    use Dancer2::Plugin::Locale;

    …
        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>

DESCRIPTION

Adds a locale keyword for your code and templates.

INTERFACE

locale

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, in more detail

The object is Locale::Maketext::Utils based.

Locale::Maketext::Utils extends Locale::Maketext a number of ways including:

Available Locales

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.

Lexicon

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

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:

Misc Info

Only load the lexicon data you need!

… TODO, sorry I needed to get this on CPAN for a consumer but will finish it ASAP …

per app/route loading/unloading of specific lexicon

… TODO, sorry I needed to get this on CPAN for a consumer but will finish it ASAP …

only load the keys you use

Localization Principles

… TODO, sorry I needed to get this on CPAN for a consumer but will finish it ASAP …

Extracting and Vetting phrases from your app

… TODO, sorry I needed to get this on CPAN for a consumer but will finish it ASAP …

TODOs

TODO items in the POD obviously, will list more if there are any left after the next version.

DIAGNOSTICS

Errors

If the locale/ directory exists but can not be opened:

<Could not read locale directory (%s): $!>

Warnings

If you have a mis-named file in your locale/ directory:

<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) …>

If you have a lexicon that can’t be loaded (e.g. broken JSON):

<Ignoring lexicon, %s, since it containes invalid JSON:\n\t$@>

CONFIGURATION AND ENVIRONMENT

The default locale is “en”, you can change it via Dancer2 configuration like so:

    plugins:
      Locales:
        default_locale: ja

DEPENDENCIES

Dancer2::Plugin

Locale::Maketext::Utils

Locales

File::Spec

INCOMPATIBILITIES

None reported.

BUGS AND FEATURES

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.

AUTHOR

Daniel Muey <http://drmuey.com/cpan_contact.pl>

LICENCE AND COPYRIGHT

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.

DISCLAIMER OF WARRANTY

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.