File: Charset.pm

package info (click to toggle)
libmojolicious-perl 0.999926-1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 949
  • sloc: perl: 17,391; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 2,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright (C) 2008-2010, Sebastian Riedel.

package Mojolicious::Plugin::Charset;

use strict;
use warnings;

use base 'Mojolicious::Plugin';

# Shut up friends. My internet browser heard us saying the word Fry and it
# found a movie about Philip J. Fry for us.
# It also opened my calendar to Friday and ordered me some french fries.
sub register {
    my ($self, $app, $conf) = @_;

    # Config
    $conf ||= {};

    # Set charset
    $app->plugins->add_hook(
        before_dispatch => sub {
            my ($self, $c) = @_;

            # Got a charset
            if (my $charset = $conf->{charset}) {

                # This has to be done before params are cloned
                $c->tx->req->default_charset($charset);

                # Add charset to text/html content type
                my $type = $c->app->types->type('html');
                unless ($type =~ /charset=/) {
                    $type .= ";charset=$charset";
                    $c->app->types->type(html => $type);
                }
            }

            # Allow defined but blank encoding to suppress unwanted
            # conversion
            my $encoding =
              defined $conf->{encoding}
              ? $conf->{encoding}
              : $conf->{charset};
            $c->app->renderer->encoding($encoding) if $encoding;
        }
    );
}

1;
__END__

=head1 NAME

Mojolicious::Plugin::Charset - Charset Plugin

=head1 SYNOPSIS

    # Mojolicious
    $self->plugin(charset => {charset => 'Shift_JIS'});

    # Mojolicious::Lite
    plugin charset => {charset => 'Shift_JIS'};

=head1 DESCRIPTION

L<Mojolicous::Plugin::Charset> is a plugin to easily set the default charset
and encoding on all layers of L<Mojolicious>.

=head1 METHODS

L<Mojolicious::Plugin::Charset> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.

=head2 C<register>

    $plugin->register;

Register plugin hooks in L<Mojolicious> application.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.

=cut