Catalyst-Plugin-I18N-PathPrefixGeoIP

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/I18N/PathPrefixGeoIP.pm  view on Meta::CPAN


=cut

after prepare_path => sub {
  my ($c) = (shift, @_);

  $c->prepare_path_prefix;
};

=head2 prepare_path_prefix

  $c->prepare_path_prefix()

Returns: N/A

If C<< $c->req->path >> is matched by the L</language_independent_paths>
configuration option then calls C<< $c->set_languages_from_language_prefix >>
with the value of the L</fallback_language> configuration option and
returns.

Otherwise, if C<< $c->req->path >> starts with a language code listed in the
L</valid_languages> configuration option, then splits language prefix from C<<
$c->req->path >> then appends it to C<< $c->req->base >> and calls C<<
$c->set_languages_from_language_prefix >> with this language prefix.

Otherwise, it tries to select an appropriate language code:

=over

=item *

It picks the first language code C<< $c->languages >> that is also present in
the L</valid_languages> configuration option.

=item *

If no such language code, uses the value of the L</fallback_language>
configuration option.

=back

Then appends this language code to C<< $c->req->base >> and the path part of
C<< $c->req->uri >>, finally calls C<< $c->set_languages_from_language_prefix >>
with that language code.

=cut

sub prepare_path_prefix
{
  my ($c) = (shift, @_);

  my $config = $c->config->{'Plugin::I18N::PathPrefixGeoIP'};

  my $language_code = $config->{fallback_language};

  my $valid_language_codes = $config->{_valid_language_codes};

  my $req_path = $c->req->path;

  if ($req_path !~ $config->{language_independent_paths}) {
    my ($prefix, $path) = split m{/}, $req_path, 2;
    $prefix = lc $prefix if defined $prefix;
    $path   = '' if !defined $path;

    if (defined $prefix && exists $valid_language_codes->{$prefix}) {
      $language_code = $prefix;

      $c->_language_prefix_debug("found language prefix '$language_code' "
        . "in path '$req_path'");

      # can be a language independent path with surplus language prefix
      if ($path =~ $config->{language_independent_paths}) {
        $c->_language_prefix_debug("path '$path' is language independent");

        # bust the language prefix completely
        $c->req->uri->path($path);

        $language_code = $config->{fallback_language};
      }
      else {
        # replace the language prefix with the known lowercase one in $c->req->uri
        $c->req->uri->path($language_code . '/' . $path);

        # since $c->req->path returns such a string that satisfies
        # << $c->req->uri->path eq $c->req->base->path . $c->req->path >>
        # this strips the language code prefix from $c->req->path
        my $req_base = $c->req->base;
        $req_base->path($req_base->path . $language_code . '/');
      }
    }
    else {
      my $detected_language_code;

      my $geocountry = _ip2contry($config->{geoip}, $c->req->address);

      if ($geocountry && exists $valid_language_codes->{$geocountry}) {
        $detected_language_code = $geocountry;
        $c->_language_prefix_debug("Detected valid language by GeoIP. Ip: " . $c->req->address . " -> Country: '$detected_language_code'");
      }
      else {
        $c->_language_prefix_debug("Did not find valid language by GeoIP. Failing over to languages request header. Ip Address: " . $c->req->address);
         $detected_language_code =
        first { exists $valid_language_codes->{$_} }
          map { lc $_ }
            @{ $c->languages };
      }

      $c->_language_prefix_debug("detected language: "
        . ($detected_language_code ? "'$detected_language_code'" : "N/A"));

      $language_code = $detected_language_code if $detected_language_code;

      # fake that the request path already contained the language code prefix
      my $req_uri = $c->req->uri;
      $req_uri->path($language_code . $req_uri->path);

      # so that it strips the language code prefix from $c->req->path
      my $req_base = $c->req->base;
      $req_base->path($req_base->path . $language_code . '/');

      if ($config->{redirect_to_language_url}) {



( run in 1.022 second using v1.01-cache-2.11-cpan-483215c6ad5 )