Newer Older
65 lines | 1.432kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojolicious::Plugin::Charset;
2
use Mojo::Base 'Mojolicious::Plugin';
3

            
4
sub register {
5
  my ($self, $app, $conf) = @_;
6

            
7
  return unless my $c = $conf->{charset};
8
  $app->types->type(html => "text/html;charset=$c");
9
  $app->renderer->encoding($c);
10
  $app->hook(before_dispatch =>
11
      sub { shift->req->default_charset($c)->url->query->charset($c) });
12
}
13

            
14
1;
15

            
16
=encoding utf8
17

            
18
=head1 NAME
19

            
20
Mojolicious::Plugin::Charset - Charset plugin
21

            
22
=head1 SYNOPSIS
23

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

            
27
  # Mojolicious::Lite
28
  plugin Charset => {charset => 'Shift_JIS'};
29

            
30
=head1 DESCRIPTION
31

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

            
35
The code of this plugin is a good example for learning to build new plugins,
36
you're welcome to fork it.
37

            
38
=head1 OPTIONS
39

            
40
L<Mojolicious::Plugin::Charset> supports the following options.
41

            
42
=head2 charset
43

            
44
  # Mojolicious::Lite
45
  plugin Charset => {charset => 'Shift_JIS'};
46

            
47
Application charset.
48

            
49
=head1 METHODS
50

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

            
54
=head2 register
55

            
56
  $plugin->register(Mojolicious->new, {charset => 'Shift_JIS'});
57

            
58
Register hook L<Mojolicious/"before_dispatch"> in application and change a few
59
defaults.
60

            
61
=head1 SEE ALSO
62

            
63
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
64

            
65
=cut