Name

PGXN::Site::Locale - Localization for PGXN::Site

Synopsis

use PGXN::Site::Locale;
my $mt = PGXN::Site::Locale->accept($env->{HTTP_ACCEPT_LANGUAGE});

Description

This class provides localization support for PGXN::Site. Each locale must create a subclass named for the locale and put its translations in the %Lexicon hash. It is further designed to support easy creation of a handle from an HTTP_ACCEPT_LANGUAGE header.

Interface

The interface inherits from Locale::Maketext and adds the following method.

Constructor Methods

accept

my $mt = PGXN::Site::Locale->accept($env->{HTTP_ACCEPT_LANGUAGE});

Returns a PGXN::Site::Locale handle appropriate for the specified argument, which must take the form of the HTTP_ACCEPT_LANGUAGE string typically created in web server environments and specified in RFC 3282. The parsing of this header is handled by I18N::LangTags::Detect.

Instance Methods

list

# "Missing these keys: foo, bar, and baz"
say $mt->maketext(
    'Missing these keys: [list,_1])'
    [qw(foo bar baz)],
);

Formats a list of items. The list of items to be formatted should be passed as an array reference. If there is only one item, it will be returned. If there are two, they will be joined with " and ". If there are more, there will be a comma-separated list with the final item joined on ", and ".

Note that locales can control the localization of the comma and "and" via the listcomma and listand entries in their %Lexicons.

qlist

# "Missing these keys: “foo”, “bar”, and “baz”
say $mt->maketext(
    'Missing these keys: [qlist,_1]'
    [qw(foo bar baz)],
);

Like list() but quotes each item in the list. Locales can specify the quotation characters to be used via the openquote and shutquote entries in their %Lexicons.

from_file

my $text = $mt->from_file('foo/bar.html');
my $msg  = $mt->from_file('feedback.html', 'pgxn@example.com');

Returns the contents of a localized file. The file argument should be specified with Unix semantics, regardless of operating system. Whereas subclasses contain short strings that need translating, the files can contain complete documents. As with maketext(), the support the full range variable substitution, such as [_1] and friends.

If a file doesn't exist for the current language, from_file() will fall back on the same file path for any of its parent classes. If none has the file, it will fall back on the English file.

Localized files are maintained in Text::MultiMarkdown format by translators and converted to HTML at build time. The live in a subdirectory named for the last part of a subclass's package name. For example, the PGXN::Site::Locale::fr class lives in PGXN/Site/Locale/fr.pm. Localized files will live in PGXN/Site/Locale/fr/. So for the argument feedback.html, the localized file will be PGXN/Site/Locale/fr/foo/bar.mmd, and the HTML file (created at build time) will be PGXN/Site/Locale/fr/foo/bar.html.

Author

David E. Wheeler <david@justatheory.com>

Copyright and License

Copyright (c) 2010-2024 David E. Wheeler.

This module is free software; you can redistribute it and/or modify it under the PostgreSQL License.

Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.

In no event shall David E. Wheeler be liable to any party for direct, indirect, special, incidental, or consequential damages, including lost profits, arising out of the use of this software and its documentation, even if David E. Wheeler has been advised of the possibility of such damage.

David E. Wheeler specifically disclaims any warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The software provided hereunder is on an "as is" basis, and David E. Wheeler has no obligations to provide maintenance, support, updates, enhancements, or modifications.